Integrationtest IHost TestServer won't shutdown

Viewed 755

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.

2 Answers
  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

I had the same problem. Since i was working with NUnit the suggested change was not an option (it is based on xunit). So i was digging into it and the root cause of it was quite simple: I created a long running task in the Startup.cs which was doing some monitoring throughout all the hosting's lifetime. So i had to stop this task, and then also the instance of the TestHost was disposed properly.

Related