如何解决 .net 测试挂起 "Starting test execution, please wait..."

Howto resolve .net test hangs on "Starting test execution, please wait..."

我正尝试在我们的 Jenkins 构建服务器上从命令行执行 dotnet test,但它只是挂起:

Starting test execution, please wait...

在本地运行这个命令

时工作正常

如果我切换到使用 dotnet xunit,它会失败并显示以下内容:

15:42:57 Locating binaries for framework netcoreapp2.1...
15:42:58 Running .NET Core 2.1 tests for framework netcoreapp2.1...
15:42:58 The specified framework version '2.1' could not be parsed
15:42:58 The specified framework 'Microsoft.NETCore.App', version '2.1' was not found.
15:42:58   - Check application dependencies and target a framework version installed at:
15:42:58       C:\Program Files\dotnet\
15:42:58   - Installing .NET Core prerequisites might help resolve this problem:
15:42:58       http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
15:42:58   - The .NET Core framework and SDK can be installed from:
15:42:58       https://aka.ms/dotnet-download
15:42:58   - The following versions are installed:
15:42:58       2.0.6 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.0.9 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.2 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.3 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
15:42:58       2.1.4 at [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

根据错误消息,我们已经在服务器上安装了 dotnet core SDK,但我们似乎缺少一些东西。

我的测试项目如下所示:

<Project Sdk="Microsoft.NET.Sdk">    
  <PropertyGroup>
    <TargetFramework>netcoreapp2.1</TargetFramework>    
    <IsPackable>false</IsPackable>
  </PropertyGroup>    
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.1.1" />
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.0" />
    <PackageReference Include="Moq" Version="4.10.0" />
    <PackageReference Include="RichardSzalay.MockHttp" Version="5.0.0" />
    <PackageReference Include="xunit" Version="2.3.1" />
    <PackageReference Include="xunit.runner.console" Version="2.4.0" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
    <DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
  </ItemGroup>        
</Project>

版本控制是一个转移注意力的问题,结果证明这是一个简单得多的问题。我的测试正在测试异步控制器方法,而我的非异步测试正在做:

var result = controller.PostAsync(request).Result;

一旦我将测试本身更改为使用 async/await 模式,它们就可以正常工作:

var result = await controller.PostAsync(request);

帮助我诊断问题的是使用 dotnet test --verbosity d 参数。使用它时,它输出一些通过的测试,而不仅仅是“开始测试执行,请稍候”。有趣的是,每次我 运行 命令它都会在出现卡住之前执行不同数量的测试。这表明可能存在某种线程死锁问题,这让我找到了解决方案。我仍然不确定为什么命令 运行 在我的本地机器上正常,但在我们的 Jenkins 代理上却不正常。

有时 xUnit 测试会永远挂起,因为测试运行程序管道无法处理并行线程。

要检查这是否是问题所在,请尝试将 xunit.runner.json 文件添加到测试项目根

  <ItemGroup>
    <None Update="xunit.runner.json">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

并放入1个线程配置。

{
  "parallelizeTestCollections": true,
  "maxParallelThreads": -1
}

对我有用的解决方案是添加一个包含以下内容的 AssemblyInfo.cs 文件:

using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

一旦我这样做了,测试就在我的 CI 构建中再次开始 运行(在我的例子中,Azure Pipelines)。

将 Microsoft.NET.Test.Sdk 从 15.9 更新到 16.3 有助于 csproj 文件。

我的问题是构造函数中的异步方法:

MyAsyncMethod().GetAwaiter().GetResult();

我通过将异步方法移出构造函数并实现 xunits IAsyncLifetime 接口解决了这个问题 (Await Tasks in Test Setup Code in xUnit.net?)

public async Task InitializeAsync()
{
    await MyAsyncMethod();
}