【问题标题】:AppVeyor + OpenCover + xUnit - Build doesn't fail when tests failAppVeyor + OpenCover + xUnit - 测试失败时构建不会失败
【发布时间】:2018-02-08 22:24:32
【问题描述】:

我创建了一个AppVeyor Build Script,它使用 OpenCover 和 Coveralls.Net 来运行我的 xUnit 测试并将代码覆盖率发布到 Coveralls.io。

但是当我的测试失败时 - AppVeyor 报告build successful如果 OpenCover + xUnit 报告测试失败,我如何配置 AppVeyor 失败?

脚本基于csMACnz's sample:

.\src\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe 
   -register:user 
   -target:"xunit.console.clr4.exe"  
   "-targetargs:""src\HttpWebRequestWrapper.Tests\bin\$env:CONFIGURATION\HttpWebRequestWrapper.Tests.dll"" 
   /noshadow 
   /appveyor" 
   -filter:"+[HttpWebRequestWrapper*]*" 
   -output:opencoverCoverage.xml

    $coveralls = (Resolve-Path "src/packages/coveralls.net.*/tools/csmacnz.coveralls.exe").ToString()
    & $coveralls --opencover -i opencoverCoverage.xml --repoToken $env:COVERALLS_REPO_TOKEN --commitId $env:APPVEYOR_REPO_COMMIT --commitBranch $env:APPVEYOR_REPO_BRANCH --commitAuthor $env:APPVEYOR_REPO_COMMIT_AUTHOR --commitEmail $env:APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL --commitMessage $env:APPVEYOR_REPO_COMMIT_MESSAGE --jobId $env:APPVEYOR_JOB_ID

我已尝试将 -returntargetcode 标志添加到 OpenCover.Console.exe 代码中,但这似乎并不表示 AppVeyor 构建失败。

【问题讨论】:

    标签: c# xunit opencover appveyor coveralls


    【解决方案1】:

    我认为原因很简单。按照您编写 YML 文件的方式,AppVeyor 期望整个 test_script 提供非零返回码,而您留下的 PowerShell 脚本部分中的故障不会导致这种情况。

    您必须单独运行每个命令,然后任何失败都会导致整个构建失败,如my AppVeyor script shows

    test_script:
      - dotnet test Tests\CSharpCore\Tests.NetStandard.csproj --configuration Release --no-build --filter "FullyQualifiedName~Lextm.SharpSnmpLib.Integration.TrapDaemonTestFixture"
      - dotnet test Tests\CSharpCore\Tests.NetStandard.csproj --configuration Release --no-build --filter "FullyQualifiedName~Lextm.SharpSnmpLib.Integration.DaemonTestFixture"
      - dotnet test Tests\CSharpCore\Tests.NetStandard.csproj --configuration Release --no-build --filter "FullyQualifiedName~Lextm.SharpSnmpLib.Unit"
    

    如果我将这三个命令包装在批处理文件或 PowerShell 脚本中,我可以重现您遇到的相同问题。

    【讨论】:

    • 有趣 - 这是否意味着如果第一次测试运行失败 (TrapDaemonTestFixture) 其他测试不会运行? (即DaemonTestFixtureUnit)?
    • @PhilipPittle 我的案例很特别,因为测试用例很消耗资源,所以我必须将它们分成小批量。但是是的,如果一个命令失败,那么接下来的几个命令将根本不会执行,因为 AppVeyor 将从那里中断。
    【解决方案2】:

    同意 Lee,但更简单的选择是在脚本开头使用 $ErrorActionPreference = "Stop"。

    查看您对下面帖子的评论,即使出现故障,您也需要执行所有命令。我这个案例看ErrorVariable。好的讨论和示例是here

    【讨论】:

      【解决方案3】:

      看起来有几种方法可以从 AppVeyor 中获得所需的行为。我最终使用了AppVeyor Discussion Thread @ilyaf 发布的$LastExitCode 中的引用。

      这种方法最终给了我更多的灵活性 - 我可以继续运行整个测试脚本,但如果任何特定部分失败,测试步骤仍然失败:

      .\src\packages\OpenCover.4.6.519\tools\OpenCover.Console.exe 
        -register:user 
        -target:"xunit.console.clr4.exe"  
        "-targetargs:""src\HttpWebRequestWrapper.Tests\bin\$env:CONFIGURATION\HttpWebRequestWrapper.Tests.dll"" 
        /noshadow 
        /appveyor" 
        -filter:"+[HttpWebRequestWrapper*]*" 
        -output:opencoverCoverage.xml
      
      $testRunnerErrorCode = $LastExitCode
      
      #can move this to end of script if I want to still publish code coverage
      #for failed tests
      if ($testRunnerErrorCode -ne 0){
         throw "xUnit failed with code $testRunnerErrorCode "
      }
      
      
      $coveralls = (Resolve-Path "src/packages/coveralls.net.*/tools/csmacnz.coveralls.exe").ToString()
      & $coveralls --opencover -i opencoverCoverage.xml --repoToken  $env:COVERALLS_REPO_TOKEN --commitId $env:APPVEYOR_REPO_COMMIT --commitBranch $env:APPVEYOR_REPO_BRANCH --commitAuthor $env:APPVEYOR_REPO_COMMIT_AUTHOR --commitEmail $env:APPVEYOR_REPO_COMMIT_AUTHOR_EMAIL --commitMessage $env:APPVEYOR_REPO_COMMIT_MESSAGE --jobId $env:APPVEYOR_JOB_ID
      

      我将标记 Lex 的答案正确,因为它是对问题最直接和直接的答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-10-17
        • 1970-01-01
        • 2018-12-17
        • 2012-03-25
        • 2018-12-17
        • 2020-03-23
        • 1970-01-01
        • 2013-12-12
        相关资源
        最近更新 更多