How to capture stdout/stderr in junit 5 gradle tes

2019-06-25 00:31发布

问题:

My gradle project uses junit 5 and I'm trying to get the test reports to show up on my build server. The XML report basically looks fine – it contains all the test classes and methods, but it is missing stdout/stderr printed in test methods. There is only some CDATA containing test metadata.

@Test
void testToString() {
    System.out.println("Hello world");
    ...
}

XML report:

<testcase name="testToString()" classname="com.my.company.PairsTest" time="0.008">
<system-out><![CDATA[
unique-id: [engine:junit-jupiter]/[class:com.my.company.PairsTest]/[method:testToString()]
display-name: testToString()
]]></system-out>
</testcase>

Is there a setting to tell the gradle plugin to capture stdout/stderr? I looked around http://junit.org/junit5/docs/current/user-guide/#running-tests-build but couldn't find any.

I am using org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3 and org.junit.jupiter:junit-jupiter-{api,engine}:5.0.0-M3.

回答1:

As Marc Philipp mentioned, there is currently no support for capturing output to STDERR or STDOUT in JUnit 5, neither in the Platform nor in Jupiter.

If you would like such a feature, please raise an issue here: https://github.com/junit-team/junit5/issues

Update (2017.11.01)

FYI: as a proof of concept, I ported the JUnit 4 based OutputCapture rule from Spring Boot to JUnit Jupiter here: https://github.com/sbrannen/junit5-demo/blob/master/src/test/java/extensions/CaptureSystemOutput.java