【问题标题】:Howto resolve .net test hangs on "Starting test execution, please wait..."如何解决 .net 测试挂起“开始测试执行,请稍候...”
【发布时间】:2021-07-01 22:45:45
【问题描述】:

我正在尝试从我们的 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>

【问题讨论】:

标签: unit-testing jenkins .net-core xunit.net


【解决方案1】:

版本控制是一个红鲱鱼,结果证明是一个简单得多的问题。我的测试正在测试异步控制器方法,而我的非异步测试正在执行:

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

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

var result = await controller.PostAsync(request);

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

【讨论】:

  • 就我而言,它是 .Wait() 。谢谢你的提示。
【解决方案2】:

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

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

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

并放入 1 个线程配置中。

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

【讨论】:

  • 我使用 `"parallelizeTestCollections": false` 来不并行运行测试,这是我的问题。
  • 如何解决这个问题以便并行运行测试?这正是我在 Rider 和 Azure 发布管道中执行 xUnit 测试时在本地观察到的。
【解决方案3】:

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

using Xunit;

[assembly: CollectionBehavior(DisableTestParallelization = true)]

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

【讨论】:

    【解决方案4】:

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

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

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

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        猜你喜欢
        • 2023-03-14
        • 1970-01-01
        • 2013-04-05
        • 2020-10-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-15
        • 2013-04-12
        相关资源
        最近更新 更多