使用HTML输出&在Maven中运行Surefire测试失败的非零退出代码
我正在为各种Dockerized Java应用程序制作Gitlab CI/CD管道,并使用Maven来构建它们。管道的第一步将构建应用程序并将其推向我们的注册表。第二步应该运行单位测试,并确保它们在允许管道继续到部署阶段之前通过。
我遇到的问题是找到一种方法来调用Maven,可以做我想要的。我需要测试步骤要做的是:
- 通过SureFire Print执行测试一次
- ,以stdout或stderr测试结果(因此在工作日志中可见)
- 输出XML测试报告,该报告将被上传到Gitlab作为Job trifacts(将会通过GitLab解析以填充Web GUI中的测试结果)
- 输出HTML测试报告,该报告将作为工作伪像,将其上传到GitLab(以提供可下载的,可读的,可读的,多合一的测试结果摘要)
- 当且仅当任何测试失败时,使用非零返回代码退出(以便将管道作业标记为Gitlab Pipeline中的失败)
1-3由SureFire自动处理。问题是4& 5;我正在使用SureFire-Report插件来生成HTML报告,但是我找不到一种调用它可以执行我想要的方法的方法:
- 如果我调用
mvn'test''surefire-report:reportfire-Report:report'< /code>,然后,如果任何测试失败,则使用非零退出代码退出,但未生成HTML报告。
- 我认为这是因为,在测试失败的情况下,Maven考虑
test
目标失败并在不运行surefire-Report
目标的情况下退出目标 - 如果没有失败的测试,则
surefire-Report
目标确实运行并生成报告,但是只有在通过的所有内容都有有限使用时才生成的报告。
- 我认为这是因为,在测试失败的情况下,Maven考虑
- 如果我调用
mvn'surefire-Report:report'
或mvn'surefire-Report:报告'test',那么,如果任何测试失败,HTML报告仍然是生成,但返回代码为0。
- 我认为这是因为Maven认为
surefire-Report
只要成功生成报告,然后考虑test> test
目标是多余的因为它已经进行了测试以生成报告
- 我认为这是因为Maven认为
- 我当前的解决方法是
mvn'Surefire-Report:Report'&amp;&amp;有效的MVN“ test'
也可以运行两次测试(或至少将其结果打印到两次),还需要运行测试以使用shell脚本而不是运行的Docker容器,而不是运行一个单个maven命令 - 有人建议
mvn'test';结果= $? ; Mvn“ surefire-报告:仅报告”&amp;&amp;退出$ {结果}
,它似乎有效,但是我宁愿有一个命令而不是shell脚本
I'm working on a Gitlab CI/CD pipeline for various Dockerized Java apps, using Maven to build them. The first step of the pipeline builds the app and pushes it to our registry. The second step is supposed to run unit tests and ensure they all pass before allowing the pipeline to continue to the deployment stage.
The problem I'm having is finding a way to invoke Maven which does what I want. What I need the test step to do is:
- Execute the tests once with Surefire
- Print to STDOUT or STDERR the test results (so they are visible in the job log)
- Output XML test reports which will be uploaded to Gitlab as job artifacts (which will be parsed by Gitlab to populate the test results in the web GUI)
- Output an HTML test report which will be uploaded to Gitlab as a job artifact (to give a downloadable, human-readable, all-in-one summary of the test results)
- Exit with a nonzero return code if and only if any tests failed (so that the pipeline job will be marked as failed in the Gitlab pipeline)
1-3 are handled automatically by Surefire. The problem is 4 & 5; I'm using the Surefire-report plugin to generate the HTML report, but I can't find a way to invoke it which does what I want:
- If I invoke
mvn 'test' 'surefire-report:report'
, then, if any tests fail, Maven exits with a nonzero exit code but the HTML report is not generated.- I think this is because, in the case of a failed test, Maven considers the
test
goal to have failed and exits without running thesurefire-report
goal at all - If there are no failed tests, then the
surefire-report
goal does run and the report is generated, but a report that's only generated when everything passed is of limited use.
- I think this is because, in the case of a failed test, Maven considers the
- If I invoke
mvn 'surefire-report:report'
ormvn 'surefire-report:report' 'test'
, then, if any tests fail, an HTML report is still generated, but the return code is 0.- I think this is because Maven considers the
surefire-report
goal to be successful as long as it successfully generates a report, and then considers thetest
goal to be redundant because it already ran the tests in order to generate the report
- I think this is because Maven considers the
- My current workaround is
mvn 'surefire-report:report' && mvn 'test'
, which works, but also runs the tests twice (or at least prints their results to STDOUT twice), and also requires the Docker container in which the tests are run to use a shell script instead of running a single Maven command - Someone suggested
mvn 'test' ; result=$? ; mvn 'surefire-report:report-only' && exit ${result}
, which seems to work, but I'd much rather have a single command rather than a shell script
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论