如何通过执行JAR文件创建XML报告

huangapple go评论74阅读模式
英文:

How to create a XML report by executing a JAR

问题

以下是翻译好的部分:

我想开发一个独立的测试解决方案,以 jar 形式交付,可以在 CI/CD 环境中使用,而无需一直重新编译。因此,我从一个包含几个库、一个 Spring Boot 应用程序以及一个名为 test-runner 的子模块的多模块 Maven 项目中打包了一个 fat-jar。

在 GitLab CI/CD 中执行这个 fat-jar 是可以的,但我认为这只是完成了一半。我想生成一个 JUnit XML 报告来输出测试结果。根据我的研究,我理解我需要实现自己的报告生成器。是否有更完整的示例呢?

测试运行器代码如下:

public class Runner {
    SummaryGeneratingListener listener = new SummaryGeneratingListener();

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(MyTest.class)).build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(testPlan);
    }

    public static void resultReport(Result result) {
        System.out.println("Finished. Result: Failures: " + result.getFailureCount() + ". Ignored: "
                        + result.getIgnoreCount() + ". Tests run: " + result.getRunCount() + ". Time: "
                        + result.getRunTime() + "ms.");
    }

    public static void main(String[] args) {
        Runner runner = new Runner();
        runner.runOne();
        TestExecutionSummary summary = runner.listener.getSummary();

        summary.printTo(new PrintWriter(System.out));
    }
}

背景:
我的测试解决方案是通用的,使用配置文件对测试进行参数化。所有的测试并行运行,针对一个被测试的系统。因此,在这之前,所有的 GitLab 作业都调用 mvn test 来执行测试并生成报告,但每次运行都重新编译了所有内容。我在考虑加快速度。

英文:

I want to develop a stand-alone test-solution delivered as a jar that can be used in a CI/CD environment without being recompiled all the time. Therefore I packed a fat-jar from a multi-maven-module containing a few libaries, a Spring Boot application and a submodule called test-runner.

Executing the fat-jar from within GitLab CI/CD works, but I think that was only the first half of it. I want to produce a JUnit XML report to output the test-results. What I understood from my research is that I would have to implement my own reporter. Is there a more complete example out there?

The test runner

public class Runner {
    SummaryGeneratingListener listener = new SummaryGeneratingListener();

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(MyTest.class)).build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.execute(testPlan);
    }

    public static void resultReport(Result result) {
        System.out.println("Finished. Result: Failures: " + result.getFailureCount() + ". Ignored: "
                        + result.getIgnoreCount() + ". Tests run: " + result.getRunCount() + ". Time: "
                        + result.getRunTime() + "ms.");
    }

    public static void main(String[] args) {
        Runner runner = new Runner();
        runner.runOne();
        TestExecutionSummary summary = runner.listener.getSummary();

        summary.printTo(new PrintWriter(System.out));
    }
}

Background:
My test-solution is generic and uses a configuration file to parameterize the tests. All tests run in parallel versus a system-under-test. So before this attempt all gitlab-jobs called mvn test to execute the tests and generate the reports, but it recompiled everything every run. I thought about speeding things up.

答案1

得分: 1

生成XML报告,您可以使用LegacyXmlReportGeneratingListener,并将保存报告的路径作为第一个参数:

LegacyXmlReportGeneratingListener xmlListener = new LegacyXmlReportGeneratingListener(Paths.get("reports"), new PrintWriter(System.out));

在您的runOne()方法中,您需要相应地注册您的监听器:

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(MyTest.class)).build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.registerTestExecutionListeners(xmlListener);
        launcher.execute(testPlan);
    }

这将在您在初始化期间传递给监听器的文件夹中为每个测试根生成一个XML文件。有关更多信息,请参阅JavaDoc

英文:

To generate XML reports, you can use the LegacyXmlReportGeneratingListener with a path to save your reports to as first argument:

LegacyXmlReportGeneratingListener xmlListener = new LegacyXmlReportGeneratingListener(Paths.get("reports"), new PrintWriter(System.out));

In your runOne() method, you need to register your listener accordingly:

    public void runOne() {
        LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
                        .selectors(selectClass(MyTest.class)).build();
        Launcher launcher = LauncherFactory.create();
        TestPlan testPlan = launcher.discover(request);
        launcher.registerTestExecutionListeners(listener);
        launcher.registerTestExecutionListeners(xmlListener);
        launcher.execute(testPlan);
    }

This will generate one XML file per test root in the folder you passed to the listener during initialization.
More information can be found in the JavaDoc

答案2

得分: 0

你可以使用控制台启动器生成 JUnit5 的 XML 报告。

java -jar junit-platform-console-standalone-1.6.2.jar @junitArgs.txt --reports-dir=reports

junitArgs.txt 文件包含以下信息:

-classpath 适用于胖 JAR 的路径
--scan-classpath
英文:

You can use Console launcher to geneate Junit5 xml reports

java -jar junit-platform-console-standalone-1.6.2.jar @junitArgs.txt --reports-dir=reports

junitArgs.txt file has following info:

-classpath fat jar path
--scan-classpath

huangapple
  • 本文由 发表于 2020年10月3日 02:08:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64176371.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定