How to print a maven-like test summary on the console

Hi.

I’m pretty happy with Gradle’s default test reporting for both JUnit and TestNG.

But my users are asking, in the specific case of TestNG, for that default reporting plus some sort of simple console output that summarizes the test results in toto.

Maven prints this:

Tests run: 2, Failures: 0, Errors: 0, Skipped: 0

Is there some elegant way for me to produce that sort of output with Gradle? I envision some sort of

test.doLast {
   // print short summary here
}

construct, but I don’t know what goes inside the block.

Thanks much. Mark

See ‘samples/java/testListener’ in the full Gradle distribution.

Great, thank you.

I notice that the afterSuite() method is getting called twice the number of test classes I actually have. Once with the suite name which is the name of the test class, and again with the suite name of “Gradle Worker…”.

Why is that?

Mark

Suites are hierarchical. The whole test run is a suite, whose children are the test workers, whose children are the test classes (or user-defined suites).

Got it. Thanks.

I have this test project

https://github.com/ae6rt/gradle-test-listener.git

that is producing good results for JUnit test suites, but not so good results for TestNG suites.

JUnit:

$
gradle -q -p a clean test
 Test=com.example.TestA/com.example.TestA, tests passed=2, tests failed=0, test skipped=1
Test=com.example.TestB/com.example.TestB, tests passed=1, tests failed=0, test skipped=1
Test=Gradle Worker 1/null, tests passed=1, tests failed=0, test skipped=1
Test=Test Run/null, tests passed=1, tests failed=0, test skipped=1

TestNG:

gradle -q -p b clean test
Test=Gradle test/null, tests passed=3, tests failed=0, test skipped=0
Test=Gradle Worker 1/null, tests passed=3, tests failed=0, test skipped=0
Test=Test Run/null, tests passed=3, tests failed=0, test skipped=0

You can see that the numbers are there for the TestNG results, but the suite and classname in afterSuite() are not available to display which test produced which results.

This makes the custom listener less than useful for a multiproject build where some projects use JUnit and some use TestNG.

As less important point, I also note that the TestNG afterTest() method is not picking up on the disabled test com.example.TestNGA#testA4 and reporting it as ‘skipped’. Perhaps that test does not appear at all in the scanned list of tests?

Any suggestions on how to associate the TestNG actual results with the test class that produced them?

Thanks much.

I can’t spot any difference between the JUnit and TestNG outputs that you posted. Anyway, in TestNG classes aren’t part of the test execution hierarchy. (TestNG might run ‘A.foo()’, ‘B.bar()’, ‘A.baz()’, in that order.) Hence there aren’t any class-level suites.

Woops. I pasted a’s results into b’s code block. I corrected it above.

Thanks for the insight.

Incidentally, is there any way within one of the callback methods of the listener to ascertain under which testing framework (JUnit, TestNG) I’m running?