I’m using Gradle 2.3.9 and TestNG. When a test fails, I’d like to see its stdout and stderr printed on the console. Is there a simple way to do this?
Wouldn’t this show the streams whether the test failed or not?
There’s currently no way to show it on the console for only failed tests, as we don’t know ahead of time if the test will fail and we don’t hold all of the output in memory.
You’ll have to use the HTML report to get the output for the test.
I had to deal with this when I wrote something similar for Ant, to print a summary of failed unit tests at the end of the build. The reality is that an enhancement in this area will simply have to detect that one or more tests have failed and simply print out a detailed summary of what went wrong. Printing standard out/error is probably not going to be practical.
I love Gradle’s ability to hide unimportant information. If a test succeeds, don’t tell me about it. But it falls down here; if the test fails, I actually want to see its output.
I’ll write something to do this and post it back here.
I wrote this, and it’s working for me. It only saves the last 100 lines of output. I’d love to see something like this get added to Gradle:
// If a test fails, print its stdout/stderr to the console.
project.test {
def stdout = new LinkedList<String>()
beforeTest { TestDescriptor td ->
stdout.clear()
}
onOutput { TestDescriptor td, TestOutputEvent toe ->
stdout.addAll(toe.getMessage().split('(?m)$'))
while (stdout.size() > 100) {
stdout.remove()
}
}
afterTest { TestDescriptor td, TestResult tr ->
if (tr.resultType == TestResult.ResultType.FAILURE && stdout.size() > 0) {
println()
println("${td.className}.${td.name} failed, output:")
stdout.each { print(it) }
}
}
}
Do you have solution for gradle 4.10.2?