I have a large test suite running on travis-ci, which has a textual output. I would like to configure gradle in a way that only for failed tests, the standard output and standard error streams are displayed. For all other tests which have been executed correctly, this should not happen so that the console is not polluted with that noise.
I am aware how to enable or disable the standard output/error logging, but I am unsure how to make this dependend on the test result.
single system property can be used to specify a single test. You can do gradle -Dtest. single=ClassUnderTestTest test if you want to test single class or use regexp like gradle -Dtest. single=ClassName*Test test you can find more examples of filtering classes for tests under this link.
By default, gradle test displays only the test summary. P.S Gradle test generates the detailed tests' result at the build/reports/tests/test/index. html page.
For instance, let's consider that we're developing a new feature, and we want to see a result within the intermediate builds. In this case, we might skip the tests temporarily to reduce the overhead of compiling and running them. Undoubtedly, ignoring the tests can cause many serious issues.
Test detection By default, Gradle will run all tests that it detects, which it does by inspecting the compiled test classes. This detection uses different criteria depending on the test framework used. For JUnit, Gradle scans for both JUnit 3 and 4 test classes.
This can be archived with following gradle config
project.test {
def outputCache = new LinkedList<String>()
beforeTest { TestDescriptor td -> outputCache.clear() } // clear everything right before the test starts
onOutput { TestDescriptor td, TestOutputEvent toe -> // when output is coming put it in the cache
outputCache.add(toe.getMessage())
while (outputCache.size() > 1000) outputCache.remove() // if we have more than 1000 lines -> drop first
}
/** after test -> decide what to print */
afterTest { TestDescriptor td, TestResult tr ->
if (tr.resultType == TestResult.ResultType.FAILURE && outputCache.size() > 0) {
println()
println(" Output of ${td.className}.${td.name}:")
outputCache.each { print(" > $it") }
}
}
}
Git repo: https://github.com/calliduslynx/gradle-log-on-failure
Original found here: https://discuss.gradle.org/t/show-stderr-for-failed-tests/8463/7
Add the following configuration block to your build.gradle file:
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
tasks.withType(Test) {
testLogging {
events TestLogEvent.FAILED,
TestLogEvent.SKIPPED,
TestLogEvent.STANDARD_ERROR,
TestLogEvent.STANDARD_OUT
exceptionFormat TestExceptionFormat.FULL
showCauses true
showExceptions true
showStackTraces true
showStandardStreams true
}
}
Documentation can be found here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With