Aspnetcore: .net Core 3.0, DirectoryNotFoundException with Xunit Integration test

Created on 9 Dec 2019  路  7Comments  路  Source: dotnet/aspnetcore

I have a new issue working on migrating my Xunit integration test on .net core 3.0. I moved the project in a subfolder and now I have a DirectoryNotFoundException.

TestServerFixture:

public class TestServerFixture : WebApplicationFactory<TestStartup>
{
    public HttpClient Client { get; }
    public ITestOutputHelper Output { get; set; }

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<TestStartup>()
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

    public TestServerFixture SetOutPut(ITestOutputHelper output)
    {
        Output = output;
        return this;
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
        Output = null;
    }
}

Error message:

Message:
System.IO.DirectoryNotFoundException : *C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\MyIntegrationTests*
Stack Trace:
PhysicalFileProvider.ctor(String root, ExclusionFilters filters)
PhysicalFileProvider.ctor(String root)
HostBuilder.CreateHostingEnvironment()
HostBuilder.Build()
WebApplicationFactory1.CreateHost(IHostBuilder builder) WebApplicationFactory1.EnsureServer()
WebApplicationFactory1.CreateDefaultClient(DelegatingHandler[] handlers) WebApplicationFactory1.CreateDefaultClient(Uri baseAddress, DelegatingHandler[] handlers)
WebApplicationFactory1.CreateClient(WebApplicationFactoryClientOptions options) WebApplicationFactory1.CreateClient()
WeatherForecastController_Tests.ctor(TestServerFixture testServerFixture, ITestOutputHelper output) line 25

The project is not in:

*C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\MyIntegrationTests*

In TestServerFixture.CreateHostBuilder, the value returned by Directory.GetCurrentDirectory() is

"C:\Users\Me\source\repos\tests\TestingWithDotNetCore3_0\src\tests\AllInOne.Integration.Tests\bin\Debug\netcoreapp3.0"

I tried:

    protected override IHostBuilder CreateHostBuilder()
    {
        var builder = Host.CreateDefaultBuilder()
            **.UseContentRoot(Directory.GetCurrentDirectory())**
            .ConfigureLogging(logging =>
            {
                logging.ClearProviders();
                logging.AddXunit(Output);
            })
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder
                    .UseStartup<TestStartup>()
                    **.UseContentRoot(Directory.GetCurrentDirectory())**
                    .ConfigureTestServices((services) =>
                    {
                        services
                            .AddControllers()
                            .AddApplicationPart(typeof(Startup).Assembly);
                    });
            });

        return builder;
    }

But It doesnt work.

I did a repo to show you the issue:
https://github.com/ranouf/TestingWithDotNetCore3_0

Do you have any suggestion to configure it correctly?

External area-mvc investigate

Most helpful comment

I had an Exception with completely the same stack trace. The problem was that my test project is in additional subfolder "Tests":
SolutionFolder\Tests\MyTestProject\MyTestProject.csproj (actual content root)
SolutionFolder\MyTestProject\MyTestProject.csproj (WebApplicationFactory expected content root)

I tried to change that content root with the following code:

 protected override IHost CreateHost(IHostBuilder builder)
 {
     builder.UseContentRoot("Tests");
     return base.CreateHost(builder);
 }

But then I was getting Exception that it can not find folder:
SolutionFolder\Tests\MyTestProject\bin\Debug\netcoreapp3.1\Tests
Actual folder with dlls is:
SolutionFolder\Tests\MyTestProject\bin\Debug\netcoreapp3.1

Finally, it worked for me with this solution:

protected override IHost CreateHost(IHostBuilder builder)
 {
     builder.UseContentRoot(Directory.GetCurrentDirectory());
     return base.CreateHost(builder);
 }

All 7 comments

/cc @javiercn

I found a hack to fix the issue, but it's a hack, It could be interesting to find a correct way to execute the code, so I let the issue opened

My Hack:
https://github.com/ranouf/TestingWithDotNetCore3_0/commit/7ef98f17e557c9d727c547f01ce43eed9e614e59

I had an Exception with completely the same stack trace. The problem was that my test project is in additional subfolder "Tests":
SolutionFolder\Tests\MyTestProject\MyTestProject.csproj (actual content root)
SolutionFolder\MyTestProject\MyTestProject.csproj (WebApplicationFactory expected content root)

I tried to change that content root with the following code:

 protected override IHost CreateHost(IHostBuilder builder)
 {
     builder.UseContentRoot("Tests");
     return base.CreateHost(builder);
 }

But then I was getting Exception that it can not find folder:
SolutionFolder\Tests\MyTestProject\bin\Debug\netcoreapp3.1\Tests
Actual folder with dlls is:
SolutionFolder\Tests\MyTestProject\bin\Debug\netcoreapp3.1

Finally, it worked for me with this solution:

protected override IHost CreateHost(IHostBuilder builder)
 {
     builder.UseContentRoot(Directory.GetCurrentDirectory());
     return base.CreateHost(builder);
 }

I just hit this today. Weird.

Project was working fine and xcopied it to a new drive and started seeing this out of nowhere. No changes.

dotnet test as does the VS 2019 test runner. This is maybe tied to R# and Rider?

Just wanted to add that the latest R# and Rider release seems to have resolved this issue.

Closing since it seems to be external.

Was this page helpful?
0 / 5 - 0 ratings