【问题标题】:.NET Core Unit tests not shown in AppVeyor Tests window (and badge).NET Core 单元测试未显示在 AppVeyor 测试窗口(和徽章)中
【发布时间】:2018-06-22 10:53:35
【问题描述】:

跟进这个问题,我目前正在为我的项目 (here) 设置 AppVeyor,我的 .NET Core 测试仅显示在控制台输出中,而不显示在“测试”窗口中。

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

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

这是控制台输出:

这是测试窗口:

为了在控制台窗口之外正确地报告它们,我还需要设置什么吗?

【问题讨论】:

    标签: c# .net unit-testing appveyor


    【解决方案1】:

    在测试项目中添加其他未使用的引用的更简洁的替代方法是在测试脚本中执行此操作:

    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
    

    【讨论】:

    • 你可以在没有 'cd' 的情况下做到这一点,只需 nuget install Appveyor.TestLogger 并运行 dotnet test ...
    【解决方案2】:

    您可以将 AppVeyor.TestLogger 包添加到您的项目中,但无需更改代码即可完成。您需要将测试结果输出为 AppVeyor 可以理解的 xml 文件格式,然后将其上传到他们的 HTTP API。下面的 powershell sn-p 将遍历你的解决方案并找到每个测试项目,在 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
    }
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 我在测试项目中添加了这个包,但是执行的测试仍然没有反映在 AppVeyor UI (ci.appveyor.com/project/Viir/kalmit/build/1.0.7)
      • 您需要更新调用 dotnet test 的行。 here 带有以下内容:dotnet test --test-adapter-path:. --logger:Appveyor 参考:github.com/spekt/appveyor.testlogger
      • 感谢@Adriang 的指点。确认,添加`--test-adapter-path:。 --logger:Appveyor` 导致测试结果显示在 AppVeyor UI 中,包括文本输出。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 2016-12-21
      相关资源
      最近更新 更多