【问题标题】:Integrationtest IHost TestServer won't shutdownIntegrationtest IHost TestServer 不会关闭
【发布时间】:2021-07-22 16:59:02
【问题描述】:

我使用 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();
        }

如果我添加 await host.StopAsync()...

我在 Ubuntu 18.04 机器上。

【问题讨论】:

    标签: asp.net-core integration-testing xunit


    【解决方案1】:
    1. 尝试在测试结束时处理主机。很可能,错误是因为您没有处理一次性资源。
    2. 我建议您使用 WebApplicationFactory 而不是 HostBuilder 进行测试。您可以在docs 中找到更多信息

    【讨论】:

    • 哇,这很容易,处理主机工作;)谢谢!我将研究 WebApplicationFactory。
    • 处理也对我有用。我正在使用 XUnit 测试,所以我制作了一个夹具/集合,并使任何依赖于正在运行的服务器的测试都使用该集合。集合最后处理服务器。
    猜你喜欢
    • 1970-01-01
    • 2015-07-02
    • 2018-10-25
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多