Loading...

October 15, 2014

Gradle Goodness: Show Standard Out or Error Output from Tests

We use the Test task in Gradle to run tests. If we use the System.out.println or System.err.println methods in our test we don't see the output when we execute the tests. We can customize the test task to show any output send to standard out or error in the Gradle output.

First we show our test class written with Spock, but it could also be a JUnit or TestNG test:

// File: src/test/groovy/com/mrhaki/gradle/SampleSpec.groovy
package com.mrhaki.gradle

import spock.lang.*

class SampleSpec extends Specification {

    def "check that Gradle is Gr8"() {
        when:
        def value = 'Gradle is great!'

        then:
        // Include a println statement, so
        // we have output to show.
        println "Value = [$value]"
        value == 'Gradle is great!'
    }

}

Now we write a simple Gradle build file which can execute our test:

// File: build.gradle
apply plugin: 'groovy' // Adds test task

repositories.jcenter()

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.7'
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}

Let's run the test task from the command line and look at the output:

$ gradle test
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test

BUILD SUCCESSFUL

Total time: 7.022 secs
$

Well at least our test is successful, but we don't see the output of our println method invocation in the test. We customize the test task and add the testLogging method with a configuration closure. In the closure we set the property showStandardStreams to the value true. Alternatively we can set the events property or use the events method with the values standard_out and standard_err to achieve the same result. In the next build file we use the showStandardStreams property:

// File: build.gradle
apply plugin: 'groovy' // Adds test task

repositories.jcenter()

dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.3.7'
    testCompile 'org.spockframework:spock-core:0.7-groovy-2.0'
}

test {
    testLogging {
        // Make sure output from 
        // standard out or error is shown
        // in Gradle output.
        showStandardStreams = true

        // Or we use events method:
        // events 'standard_out', 'standard_error'

        // Or set property events:
        // events = ['standard_out', 'standard_error']

        // Instead of string values we can
        // use enum values:
        // events org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_OUT,
        //        org.gradle.api.tasks.testing.logging.TestLogEvent.STANDARD_ERROR,
    }
}

We re-run the test task from the command line and look at the output to see the result from the println method:

$ gradle test
:compileJava UP-TO-DATE
:compileGroovy UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:compileTestJava
:compileTestGroovy
:processTestResources UP-TO-DATE
:testClasses
:test

com.mrhaki.gradle.SampleSpec > check that Gradle is Gr8 STANDARD_OUT
    Value = [Gradle is great!]

BUILD SUCCESSFUL

Total time: 8.716 secs
$

Written with Gradle 2.1.