AppVeyor 测试中未显示 .NET Core 单元测试 window(和徽章)

.NET Core Unit tests not shown in AppVeyor Tests window (and badge)

根据这个问题跟进,我目前正在为我的项目设置 AppVeyor (here),我的 .NET Core 测试仅显示在控制台输出中,但不显示在测试中 window.

这是 AppVeyor 项目的 link:ci.appveyor.com/project/Sergio0694/neuralnetwork-net

如果某些测试失败,控制台会正确显示错误并将构建标记为失败,但测试 window 无论如何都是空的。来自 shields.io 的徽章也是如此,它显示了 0 个总测试,即使我可以看到其中许多测试正在从控制台输出中执行。

控制台输出如下:

这是测试 window:

我还需要设置什么才能在控制台外正确报告它们吗window?

请将 https://www.nuget.org/packages/Appveyor.TestLogger 添加到您的测试项目。

您可以将 AppVeyor.TestLogger 包添加到您的项目中,但无需更改代码即可完成。您需要将测试结果输出为 AppVeyor 理解的 xml 文件格式,然后将其上传到他们的 HTTP API。以下 powershell 代码段将遍历您的解决方案并找到每个测试项目,在 csproj 上调用 dotnet test 并将输出记录到 test-result.trx,然后将文件上传到 AppVeyor。

$config = "release"

# Find each test project and run tests and upload results to AppVeyor
Get-ChildItem .\**\*.csproj -Recurse | 
    Where-Object { $_.Name -match ".*Test(s)?.csproj$"} | 
    ForEach-Object { 

        # Run dotnet test on the project and output the results in mstest format (also works for other frameworks like nunit)
        & dotnet test $_.FullName --configuration $config --no-build --no-restore --logger "trx;LogFileName=..\..\test-result.trx" 

        # if on build server upload results to AppVeyor
        if ("${ENV:APPVEYOR_JOB_ID}" -ne "") {
            $wc = New-Object 'System.Net.WebClient'
            $wc.UploadFile("https://ci.appveyor.com/api/testresults/mstest/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\test-result.trx)) 
        }

        # don't leave the test results lying around
        Remove-Item .\test-result.trx -ErrorAction SilentlyContinue
}

在测试脚本中添加未使用的引用的一个可以说更简洁的替代方法是在测试脚本中执行此操作:

cd <test_project_dir>
nuget install Appveyor.TestLogger -Version 2.0.0
cd ..
dotnet test --no-build --no-restore --test-adapter-path:. --logger:Appveyor <test_project_dir>

这与添加引用具有相同的效果,因为它使测试框架可以使用 testlogger 二进制文件,但实际上并没有更改测试项目,因此不需要不使用 Appveyor 的人在他们克隆和构建您的存储库时安装软件包。

此解决方案相对于输出和随后上传 .trx 文件(如上面的 PS 脚本)的轻微优势在于您应该实时获得测试结果,而不是在最后全部获得.

示例appveyor.yml:

version: 0.0.{build}
build_script:
- cmd: dotnet build MySolution.sln
test_script:
- cmd: cd Test
- cmd: nuget install Appveyor.TestLogger -Version 2.0.0
- cmd: cd ..
- cmd: dotnet test --no-build --no-restore --test-adapter-path:. --logger:Appveyor Test