tapizquent
tapizquent

Reputation: 909

Solution root could not be located using application root - WebApplicationFactory

I am trying to get some tests to properly run in CI using the WebApplicationFactory

These tests pass locally when we run them through the IDE but they fail with

System.InvalidOperationException: Solution root could not be located using application root

When we run them in CI.

In CI, we have a directory called integration_tests which contains multiple dll files for the tests and we run a vstest command to test these dlls, but we do not have the .sln file there.

Our WebApplicationFactory goes something like this:

factory = new WebApplicationFactory<Startup>()
                .WithWebHostBuilder(b =>
                {
                    b.ConfigureTestServices(s => {});
                });
client = factory.CreateDefaultClient();

I have tried using the WebApplicationFactoryContentRoot and placing it inside the AssemblyInfo.cs but I am unsure that we are using it the right way and I can not find any examples on the docs that actually use it with existing Integration tests.

I have also tried setting the b.UseSolutionRelativeContentRoot() for the WebHostBuilder, but again, works only locally.

How can we properly use the WebApplicationFactoryContentRoot to allow the WebApplicationFactory to not only work locally but also in CI for the published dlls?

Upvotes: 5

Views: 1112

Answers (1)

weichch
weichch

Reputation: 10035

WebApplicationFactory ignores custom content root path set in WithWebHostBuilder, so UseSolutionRelativeContentRoot and UseContentRoot won't work.

There are a few ways you could set the content root for test server:

  1. Use the WebApplicationFactoryContentRootAttribute

on the assembly containing the integration tests with a key equal to the TEntryPoint assembly System.Reflection.Assembly.FullName

and you need to make sure the marker file exists in the directory.

For example, the code below sets content root to AppContext.BaseDirectory:

// In your test assembly:
[assembly: WebApplicationFactoryContentRoot(
    key: "WebApplication1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null",
    contentRootPath: "",
    contentRootTest: "WebApplication1.dll",
    priority: "-1000")]
  1. Copy sln file to directory.

  2. Set ASPNETCORE_TEST_CONTENTROOT_APPNAME as per the source code.

Upvotes: 2

Related Questions