GitLab v14.1.1流水线JUnit测试失败未中止,如何调整配置?
核心原因
- 测试命令使用管道符
|,Shell默认仅返回管道最后一个命令的退出码。即使前端cucumber测试执行失败(退出码非0),只要后续cucumber-junit执行成功,整个npm run test的退出码就为0,GitLab默认按脚本退出码判定作业成功。 - 未配置GitLab CI根据JUnit报告结果判定作业状态的规则,仅配置JUnit报告上传不会自动触发失败判定。
具体修改方案
1. 调整package.json的测试命令
在test命令开头添加set -o pipefail &&,让管道中任意命令失败时,整个命令的退出码为非0:
"test": "set -o pipefail && node ./node_modules/nyc/bin/nyc.js --reporter=cobertura --reporter=html node_modules/cucumber/bin/cucumber-js src/use-cases --parallel 5 --format=json --fail-fast --require \"src/use-cases/**/!(index).js\" | cucumber-junit > junit.xml"
注:如果使用Windows runner,需替换为PowerShell对应的管道失败检测配置,Linux/macOS runner直接使用上述配置即可。
2. 调整.gitlab-ci.yml配置
在unit-test作业中添加fails_on_test_failure: true配置(GitLab v13.2+支持该参数),即使脚本退出码为0,只要JUnit报告存在失败用例,也会将作业标记为失败:
unit-test: stage: Test script: - npm run test needs: - lint # 新增以下配置,JUnit报告存在失败用例时强制作业失败 fails_on_test_failure: true artifacts: when: always paths: - coverage reports: junit: - junit.xml cobertura: - coverage/cobertura-coverage.xml expire_in: 4 days only: - test-case-testing - merge_requests
以上配置修改完成后,只要存在测试失败用例,作业就会被标记为失败,流水线会自动中止。
内容的提问来源于stack exchange,提问作者Mitesh Gangaramani
相关产品推荐
相关产品推荐

