84

(Gradle 3.2.1) I run some java tests, which logs output in Stderr/Stdout. I can see that output, if I start

gradle test --info

but in that case, much of unwanted output from 3-rd party libraries is there too.

Documentation suggests using logging.caputureStandardError / logging.caputureStandardError (loglevel), but it doesn't seem to have any effect.

tasks.withType(Test) {
   logging.captureStandardOutput LogLevel.QUIET
   logging.captureStandardError LogLevel.QUIET
}

Then if running gradle test, not STDERR/STDOUT is output in console.

How can I get just the output from the tests classes in console?

Alexei Vinogradov
  • 1,548
  • 3
  • 15
  • 34

5 Answers5

115

Add these lines to build.gradle :

apply plugin: 'java'

test {
    dependsOn cleanTest
    testLogging.showStandardStreams = true
}

Notice: dependsOn cleanTest is not necessary but if not used, you need to run cleanTest or clean task before test task.


Edit:

A better approach:

apply plugin: 'java'

test {
    testLogging {
        outputs.upToDateWhen {false}
        showStandardStreams = true
    }
}

Notice: outputs.upToDateWhen {false} is not necessary but if not used, you need to run cleanTest or clean task before test task.

For more info and options see the documentation.

Omid
  • 5,823
  • 4
  • 41
  • 50
  • 14
    @Omid Is there a way to do this on the command line. I may not want to edit my `build.gradle` file, and temporarily want to see the standard streams. Can I do something like `-Dtest.testLogging.showStandardStreams=true`? – Kilokahn Nov 24 '17 at 22:58
  • 3
    @Kilokahn I thinks based on [this quesion](https://stackoverflow.com/questions/21442642/how-do-i-set-test-testlogging-showstandardstreams-to-true-from-the-command-line), the answer is no. – Omid Nov 25 '17 at 11:47
  • Actually, one solution to change Gradle behavior while keeping `build.gradle` unchanged are initialization scripts. You can even check for project or system properties there to check whether the configuration of a test task should be changed. – Lukas Körfer May 08 '19 at 09:41
  • 1
    Where did you find info about the `outputs` option? I don't see it in the documentation you linked. – Big McLargeHuge Aug 07 '20 at 23:05
  • This doesn't do anything in IntelliJ (JUnitPlatform) – Daniel Mills Mar 20 '21 at 21:23
  • Using `outputs.upToDateWhen { false }` is [not best practice, according to Gradle](https://blog.gradle.org/stop-rerunning-tests). If the tests are dependent on a variable, then [register as a task input](https://docs.gradle.org/7.6/userguide/more_about_tasks.html#sec:task_inputs_outputs). If you want to re-run the tests, you can add [`--rerun` in Gradle 7.6](https://docs.gradle.org/7.6/userguide/command_line_interface.html#sec:builtin_task_options) – aSemy Jan 05 '23 at 11:19
31

For those using Kotlin/Kotlin DSL for Gradle, you need to put the following in your build.gradle.kts file:

tasks.withType<Test> {
    this.testLogging {
        this.showStandardStreams = true
    }
}

Also as mentioned in another answer, you will need to run gradle clean test for the output to print every time.

breandan
  • 1,965
  • 26
  • 45
Talha Malik
  • 311
  • 3
  • 2
  • 1
    Thanks man. It's so hard to find the Kotlin DSL equivalents of Gradle code. – shinvu Apr 26 '20 at 09:53
  • 2
    `outputs.upToDateWhen {false}` can be added to the above response to avoid running `gradle clean test`. Just `gradle test` should work – SayeedHussain Jul 12 '21 at 14:16
1

The testLogging answer is correct. For me, since I already had a tasks.test section, I figured it'd be easier to put it there instead.

tasks.test {
    useJUnitPlatform()
    this.testLogging {
        this.showStandardStreams = true
    }
}
Max Coplan
  • 1,111
  • 13
  • 27
0

Extending on @Talha Malik solution's above (and the ones in this other post), when dealing with a multi-module android app the following can be used (root build.gradle)

// Call from root build.gradle
setupTestLogging()

fun Project.setupTestLogging() {
    for (sub in subprojects) {
        sub.tasks.withType<Test> {
            testLogging {
                exceptionFormat = TestExceptionFormat.FULL
            }
        }
    }
}

(note that while exceptionFormat alone should be enough to get the wanted outcome, the events("standardOut" ...) mentioned above can be specified in the same way)

Marino
  • 800
  • 1
  • 12
  • 26
0

A bit late here, but wanted to add an alternative for anyone else that comes looking:

  test {
        onOutput { descriptor, event ->
                 logger.lifecycle("Test: " + descriptor + " produced standard out/err: " + event.message )
        }
  }

This helps distinguish output between tests when you are running multiple tests

Got this from gradle's official documentation: https://docs.gradle.org/current/dsl/org.gradle.api.tasks.testing.Test.html