Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

During gradle test show standard out and err only for failed tests

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.

like image 885
Dr. Simon Harrer Avatar asked Apr 11 '16 20:04

Dr. Simon Harrer


People also ask

How do I run a specific test in Gradle?

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.

Where does Gradle store test results?

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.

Why does Gradle skip tests?

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.

How does Gradle find tests?

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.


2 Answers

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

like image 176
Marcel Avatar answered Oct 26 '22 23:10

Marcel


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.

like image 38
JJD Avatar answered Oct 27 '22 01:10

JJD