question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.net Core 3.0, DirectoryNotFoundException with Xunit Integration test

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
Smiciuscommented, Apr 4, 2020

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);
 }
1reaction
jbogardcommented, Jul 2, 2020

I just hit this today. Weird.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - .net Core 3.0, DirectoryNotFoundException with Xunit ...
I have a new issue working on migrating my Xunit integration test on .net core 3.0. I moved the project in a subfolder...
Read more >
Integration tests in ASP.NET Core
NET Core supports integration tests using a unit test framework with a test ... This article assumes a basic understanding of unit tests....
Read more >
Converting integration tests to .NET Core 3.0
In this post I discuss the changes required to upgrade integration tests that use WebApplicationFactory or TestServer to ASP.NET Core 3.0.
Read more >
[Solved]-.NET Core stop HostedService in the Integration test-C#
I found a solution to put the background service register in a condition as below. Edit the Program.cs file as below in the...
Read more >
Theodore Zographos
.net Core 3.0, DirectoryNotFoundException with Xunit Integration test. 11. Mousewheel Delta value always 120. 11.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found