Search code examples
asp.net-coreintegration-testingxunit

Integrationtest IHost TestServer won't shutdown


I wrote some integration tests for an aspnetcore 3.1 application using xunit. Tests show successful, but process is still running. After some time I get:

The process dotnet:1234 has finished running tests assigned to it, but is still running. Possible reasons are incorrect asynchronous code or lengthy test resource disposal [...]

This behavior does even show with boilerplate code like:

        [Fact]
        public async Task TestServer()
        {
            var hostBuilder = new HostBuilder()
                .ConfigureWebHost(webHost =>
                {
                    // Add TestServer
                    webHost.UseTestServer();
                    webHost.Configure(app => app.Run(async ctx =>
                        await ctx.Response.WriteAsync("Hello World!")));
                    
                });

            // Build and start the IHost
            var host = await hostBuilder.StartAsync();
        }

Same if I add await host.StopAsync()...

I am on an Ubuntu 18.04 machine.


Solution

    1. Try to dispose the host at the end of the test. Most likely, the error is caused just because you don't dispose disposable resource.
    2. I would recommend you to use WebApplicationFactory for testing instead of HostBuilder. You may find more in the docs