一些单元测试无限期地运行

Some unit tests runs indefinitely

在一个解决方案中,我将 600 个单元测试拆分到各个库中,其中 450 个存在于单个库中。当 运行 使用 Visual Studio(最新版本)在我的计算机上对特定库进行测试时,一切顺利。但是,在我们的构建服务器上,一些测试永远不会使用 dotnet.exe 测试命令结束。

dotnet.exe test C:\agent\_work7\s\tests\XXX.Commercial.Infrastructure.Tests\XXX.Commercial.Infrastructure.Tests.csproj --logger trx --results-directory C:\agent\_work\_temp --configuration release -l "console;verbosity=detailed"

我在本地计算机上测试了该命令,一切正常。我直接在我们的构建服务器上使用 Visual Studio 执行了测试,我可以在 Visual Studio 上重现这个问题。我必须取消那些永远不会结束的,然后 运行 第二次让它们成功。

Image of the Test Explorer on my local desktop

Image of the Test Explorer on the build server (test never end)

Image of Azure DevOps output after waiting 10 minutes after the last test and cancellation

出现问题的测试库的.csproj

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <IsPackable>false</IsPackable>
        <ProjectGuid>{7BFDD481-8C96-41BD-9B8E-B7E1ECE9A6AC}</ProjectGuid>
    </PropertyGroup>

    <ItemGroup>
        <PackageReference Include="AutoFixture.AutoMoq" Version="4.11.0" />
        <PackageReference Include="AutoFixture.Xunit2" Version="4.11.0" />
        <PackageReference Include="coverlet.collector" Version="3.0.2">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="coverlet.msbuild" Version="3.0.2">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
        <PackageReference Include="FluentAssertions" Version="5.10.3" />
        <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
        <PackageReference Include="Moq" Version="4.16.0" />
        <PackageReference Include="xunit" Version="2.4.1" />
        <PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
            <PrivateAssets>all</PrivateAssets>
            <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
        </PackageReference>
    </ItemGroup>

我在网上找到了这个,但related/doesn不能解决我的问题:

https://github.com/microsoft/vstest/issues/224

https://github.com/xunit/xunit/issues/1910

编辑

这是一个单元测试,例如:

[Fact]
public async Task SendSuccessReservationEmailConfirmationTest()
{
    Reservation reservation;
    using (var merchantContext = this._merchantContextFactory.Invoke())
    using (var reservationContext = this._reservationContextFactory.Invoke())
    {
        var merchant = await merchantContext.Merchants.FirstOrDefaultAsync();
        reservation = await reservationContext.Reservations
            .FirstOrDefaultAsync(x => x.MerchantId.Equals(merchant.Id));

        Assert.NotNull(reservation);
        Assert.NotNull(merchant);

        reservation.MerchantId = merchant.Id;
        reservationContext.SaveChanges();
    }

    await this._reservationNotificationService.SendReservationConfirmationAsync(reservation.Id);
}

编辑 2

Visual Studio : 16.9.5

点网 CLI:5.0.203

有什么想法吗?感谢您的帮助

我终于找到问题了。

使用 Visual Studio 的“并行堆栈”工具(调试 -> Windows -> 并行堆栈),我能够找到发生死锁的 class/method。

多次测试调用此方法:

/// <summary>
/// Builds this instance and starts the bus.
/// </summary>
/// <returns></returns>
public async Task<MassTransitHelper> BuildAsync()
{
    var bus = Bus.Factory.CreateUsingInMemory(configure =>
    {
        foreach (var (queue, configurator) in this._endpointConfigurators)
        {
            configure.ReceiveEndpoint(queue, configurator);
        }
    });

    await bus.StartAsync();

    return new MassTransitHelper(bus, new MessageBrokerSettings
    {
        MessageBrokerHostAddress = "loopback://localhost:5672/",
        MessageBrokerPassword = "guest",
        MessageBrokerUserName = "guest"
    });
}

但是,对于使用“.Result()”而不是 await 调用此方法的服务的多个单元测试,它们都是同步测试。通过使它们异步并删除“.Result()”来解决这个问题。

谢谢大家的帮助。