Asp.net core Integrationtest IHost TestServer获胜';t关机

Asp.net core Integrationtest IHost TestServer获胜';t关机,asp.net-core,integration-testing,xunit,Asp.net Core,Integration Testing,Xunit,我使用xunit为aspnetcore 3.1应用程序编写了一些集成测试。 测试显示成功,但进程仍在运行。一段时间后,我得到: 进程dotnet:1234已完成运行分配给它的测试,但仍在运行。可能的原因是不正确的异步代码或冗长的测试资源处理[…] 这种行为甚至在样板代码中也会出现,如: [Fact] public async Task TestServer() { var hostBuilder = new HostBuil

我使用xunit为aspnetcore 3.1应用程序编写了一些集成测试。 测试显示成功,但进程仍在运行。一段时间后,我得到:

进程dotnet:1234已完成运行分配给它的测试,但仍在运行。可能的原因是不正确的异步代码或冗长的测试资源处理[…]

这种行为甚至在样板代码中也会出现,如:

        [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();
        }
如果添加wait host.StopAsync(),则相同

我在Ubuntu 18.04机器上

  • 尝试在测试结束时释放主机。最有可能的是,该错误只是因为您没有处理一次性资源而导致的
  • 我建议您使用WebApplicationFactory而不是HostBuilder进行测试。您可以在

  • 哇,这很简单,很有效;)谢谢我将调查WebApplicationFactory。