在 azure devops 管道中强制 newman 任务失败,测试结果不成功

时间:2021-02-12 21:29:06

标签: npm azure-devops postman azure-pipelines-yaml

我在 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 Pipeline

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

2 个答案:

答案 0 :(得分:0)

我使用“Newman the cli Companion for Postman”扩展而不是脚本,它对我来说效果很好:

默认情况下,如果有任何测试失败,任务将使您的管道失败。

以下是我在 YAML 管道中使用它配置的任务示例:

- task: carlowaklstedt.NewmanPostman.NewmanPostman.NewmanPostman@4
  displayname: "Run Newman Tests"
  inputs:
    collectionFileSource: "$(System.DefaultWorkingDirectory)"
    contents: "**/path-to-postman-collection.json"
    environment: "**/path-to-environment.json"
    pathToNewman: "(Optional) C:\path\to\newman\installation"
    ignoreRedirect: false
    bail: false
    sslInsecure: false
    reporters: "htmlextra,junit"
    htmlExtraDarkTheme: false
    htmlExtraLogs: false
    htmlExtraTestPaging: false

答案 1 :(得分:0)

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

尝试类似:

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"
}