RFR: 7903324: Improve per-class reporting of JUnit tests, in .jtr file

Christian Stein cstein at openjdk.org
Thu Oct 27 18:22:07 UTC 2022


On Tue, 27 Sep 2022 13:09:59 GMT, Alan Bateman <alanb at openjdk.org> wrote:

>> Improve per-class reporting of JUnit tests by using a custom `TestExecutionListener` in `JUnitRunner`.
>
> src/share/classes/com/sun/javatest/regtest/agent/JUnitRunner.java line 136:
> 
>> 134:                 Launcher launcher = session.getLauncher();
>> 135:                 launcher.registerTestExecutionListeners(summaryGeneratingListener);
>> 136:                 launcher.registerTestExecutionListeners(new PrintingListener(System.err));
> 
> Is System.err correct? If my test prints messages to System.out them how will I know which test they came from?

`System.err` seems to be more correct, looking at other printing code in jtreg. I used `System.out` first, which was remarked by @jonathan-gibbons in a previous note.

> If my test prints messages to System.out them how will I know which test they came from?

The canonical way is Jupiter's `TestReporter` as described in [Writing Tests - Dependency Injection for Constructors and Methods](https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection)

> The `TestReporter` can be used to publish additional data about the current test run. The data can be consumed via the `reportingEntryPublished()` method in a [TestExecutionListener](https://junit.org/junit5/docs/current/api/org.junit.platform.launcher/org/junit/platform/launcher/TestExecutionListener.html), allowing it to be viewed in IDEs or included in reports.
> ...
> In JUnit Jupiter you should use TestReporter where you used to print information to stdout or stderr in JUnit 4.


class TestReporterDemo {

    @Test
    void reportSingleValue(TestReporter testReporter) {
        testReporter.publishEntry("a status message");
    }

    @Test
    void reportKeyValuePair(TestReporter testReporter) {
        testReporter.publishEntry("a key", "a value");
    }

    @Test
    void reportMultipleKeyValuePairs(TestReporter testReporter) {
        var values = Map.of("user name", "dk38", "award year", "1974");
        testReporter.publishEntry(values);
    }

}

-------------

PR: https://git.openjdk.org/jtreg/pull/127


More information about the jtreg-dev mailing list