简体   繁体   English

测试失败时,Jenkins MSTest + MSTestPublisher不会失败

[英]Jenkins MSTest + MSTestPublisher don't fail build when tests fail

I am working on a Jenkins multibranch pipeline project and one of the steps runs unit tests. 我正在研究Jenkins multibranch管道项目,其中一个步骤运行单元测试。 When some unit tests fail Jenkins marks the build as unstable instead of fail the build. 当某些单元测试失败时,Jenkins将构建标记为不稳定而不是构建失败。

stage('UnitTests') {
  steps {
    bat(returnStatus: true, script: """
      "MSTest" /resultsfile:"testresults-module1-Tests_${env.BUILD_ID}.trx" /testcontainer:bin/module1.Tests.dll"
      "MSTest" /resultsfile:"testresults-module2-Tests_${env.BUILD_ID}.trx" /testcontainer:bin/module2.Tests.dll"
    """)
    step([$class: 'MSTestPublisher', testResultsFile:"testresults*.trx", failOnError: true, keepLongStdio: true])
  }
}

I need this job to fail when any test fails. 当任何测试失败时,我需要这个工作失败。

I used a workaround to solve this problem. 我使用了一种解决方法来解决这个问题。

stage('UnitTests') {
  steps {
    script {
      resultcmd = 0

      resultcmd += bat(returnStatus: true, script: """
        "MSTest" /resultsfile:"testresults-module1-Tests_${env.BUILD_ID}.trx" /testcontainer:bin/module1.Tests.dll"
        exit /b %errorlevel%
      """)

      resultcmd += bat(returnStatus: true, script: """
        "MSTest" /resultsfile:"testresults-module2-Tests_${env.BUILD_ID}.trx" /testcontainer:bin/module2.Tests.dll"
        exit /b %errorlevel%
      """)

      if (resultcmd > 0) {
        error("Tests failed")
      }
    }
  }
}

I execute each command in a separated bat block and add the result in a variable called resultcmd. 我在一个单独的bat块中执行每个命令,并将结果添加到一个名为resultcmd的变量中。 Then, at the end, I check the value of resultcmd and force an error if the value is greater then 0. 然后,最后,我检查resultcmd的值,如果值大于0则强制错误。

I'm sure this isn't the best solution, but this is the only fix I found. 我确信这不是最好的解决方案,但这是我找到的唯一解决办法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM