使用HTML输出&在Maven中运行Surefire测试失败的非零退出代码

发布于 2025-02-04 19:35:05 字数 1437 浏览 1 评论 0原文

我正在为各种Dockerized Java应用程序制作Gitlab CI/CD管道,并使用Maven来构建它们。管道的第一步将构建应用程序并将其推向我们的注册表。第二步应该运行单位测试,并确保它们在允许管道继续到部署阶段之前通过。

我遇到的问题是找到一种方法来调用Maven,可以做我想要的。我需要测试步骤要做的是:

  1. 通过SureFire Print执行测试一次
  2. ,以stdout或stderr测试结果(因此在工作日志中可见)
  3. 输出XML测试报告,该报告将被上传到Gitlab作为Job trifacts(将会通过GitLab解析以填充Web GUI中的测试结果)
  4. 输出HTML测试报告,该报告将作为工作伪像,将其上传到GitLab(以提供可下载的,可读的,可读的,多合一的测试结果摘要)
  5. 当且仅当任何测试失败时,使用非零返回代码退出(以便将管道作业标记为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目标确实运行并生成报告,但是只有在通过的所有内容都有有限使用时才生成的报告。
  • 如果我调用mvn'surefire-Report:report'mvn'surefire-Report:报告'test',那么,如果任何测试失败,HTML报告仍然是生成,但返回代码为0。
    • 我认为这是因为Maven认为surefire-Report只要成功生成报告,然后考虑test> test目标是多余的因为它已经进行了测试以生成报告
  • 我当前的解决方法是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:

  1. Execute the tests once with Surefire
  2. Print to STDOUT or STDERR the test results (so they are visible in the job log)
  3. 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)
  4. 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)
  5. 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 the surefire-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.
  • If I invoke mvn 'surefire-report:report' or mvn '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 the test goal to be redundant because it already ran the tests in order to generate the report
  • 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文