首页 > 解决方案 > 在 azure devops 管道中,由于测试结果不成功,强制 newman 任务失败

问题描述

我在 Azure DevOps 管道 YAML 中有一个简单的脚本,使用 newman 执行邮递员收集测试。

- script: $(Agent.WorkFolder)/node_modules/newman/bin/newman.js run "$(Build.SourcesDirectory)/postman/a.postman_collection.json" -e "$(Build.SourcesDirectory)/postman/b.postman_environment.json" -x -r junit --reporter-junit-export $(Build.ArtifactStagingDirectory)/PostmanResults/JunitResults.xml
  displayName: Run Postman tests

基本上,它有效。但是无论测试是否通过,这一步总是绿色的

Azure 管道

如果有一些不成功的测试,我怎么能强制这个任务/阶段失败?或者以某种方式确定是否存在测试失败?有谁知道任何聪明的解决方法?

标签: npmazure-devopspostmanazure-pipelines-yaml

解决方案


您必须捕获退出代码并将其作为写入错误抛出:

尝试类似:

if ($exitCode -ne 0)
{
    Write-Output ("[Error] Failing task since return code was {0} while expected 0." -f $exitCode)
    Write-Error "##vso[task.complete result=Failed;]Failed"
}

推荐阅读