Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
WebApplicationFactory
tries setting the content root based on a number of sources (a builder setting, MvcTestingAppManifest.json
, and the assembly metadata) before falling back to setting a solution-relative content root. This looks specifically for a .sln
file, which means any test that relies on it breaks when migrating to the new SLNX solution format.
Expected Behavior
If ASP.NET Core supports falling back to searching for a solution file and setting the content root relative to it, it should also be able to locate slnx solution files.
Steps To Reproduce
The simplest way to trigger this is with a WebApplicationFactory
that uses an entry point in the test assembly, instead of in a separate web API project. Create a solution with a solution file, a project file, and a test:
SlnxReproduction.slnx
<Solution>
<Project Path="Tests/Tests.csproj" />
</Solution>
Tests/Tests.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- Can also be Microsoft.NET.Sdk with a reference to an ASP.NET Core project. -->
<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.3" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>
Tests/SlnxTest.cs
using Microsoft.AspNetCore.Mvc.Testing;
namespace Tests;
public class SlnxTest(WebApplicationFactory<Startup> factory) : IClassFixture<WebApplicationFactory<Startup>>
{
[Fact]
public void CreateClient_DoesNotThrow() => factory.CreateClient();
}
public class Startup;
Then run the test with dotnet test
.
Exceptions (if any)
System.InvalidOperationException : Solution root could not be located using application root C:\Project\Path\bin\Debug\net9.0\
Stack Trace:
at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(IWebHostBuilder builder, String solutionRelativePath, String applicationBasePath, String solutionName)
at Microsoft.AspNetCore.TestHost.WebHostBuilderExtensions.UseSolutionRelativeContentRoot(IWebHostBuilder builder, String solutionRelativePath, String solutionName)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.SetContentRoot(IWebHostBuilder builder)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.<ConfigureHostBuilder>b__22_0(IWebHostBuilder webHostBuilder)
at Microsoft.Extensions.Hosting.GenericHostWebHostBuilderExtensions.ConfigureWebHost(IHostBuilder builder, Func`3 createWebHostBuilder, Action`1 configure, Action`1 configureWebHostBuilder)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.ConfigureHostBuilder(IHostBuilder hostBuilder)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.EnsureServer()
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient(WebApplicationFactoryClientOptions options)
at Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactory`1.CreateClient()```
.NET Version
9.0.202
Anything else?
ASP.NET Core 9.0.3