How to create a XML report by executing a JAR

Viewed 690

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.

2 Answers

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

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
Related