如果JUnit报告中的测试失败,如何使Gitlab管道失败

时间:2020-04-30 13:40:11

标签: junit gitlab maven-surefire-plugin

我的项目(移动应用程序)具有Gitlab管道,该阶段具有用于运行基于TestNG和maven的UI测试(Appium)的阶段。与surefire插件。 我设置将管道结果中的JUnit报告显示为工件 工件:报告:junit:-surefire-reports / TEST-*。xml 并正确显示在管道页面上。 但是,即使报告中测试失败,作业仍会标记为成功,并且管道为绿色。 如果报告包含失败的测试,管道是否有可能失败?

1 个答案:

答案 0 :(得分:0)

Gitlab似乎只有在测试过程以非零代码退出时才会使管道失败,并且它不会解析JUnit xml文件来确定管道是否成功(https://forum.gitlab.com/t/how-to-fail-job-and-pipeline-if-there-are-failed-tests-in-junit-report/37039/3

这是我为解决此问题所做的事情:

  1. 运行测试并将结果收集到文件中
# the echo command will only be run when the test process exits with a non zero code,
# and the entire step will still return 0 as the exit code
run_your_test_for_example_mvn_verify || echo "there is Maven test failure" >> failures.log 
run_your_test_for_example_poetry_pytest || echo "there is pytest failure" >> failures.log 
  1. 稍后再测试此结果文件的存在
# if the failures.log file exists, 
# the command below will return false and will fail the pipeline
test ! -f failures.log