Logging while testing through Gradle

2019-01-22 05:55发布

问题:

While testing, Gradle appears to redirect stdout/stderr to project_dir/build/reports/tests/index.html. Is there a way to avoid this redirection, and get things printed to the console instead?

Additional information:

  • It's a Scala 2.9.1 project.
  • I am using slf4s for logging.

回答1:

apply plugin : 'java'

test {
    testLogging.showStandardStreams = true
}

http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html

This requires a current gradle version. I am assuming that the Scala tests are run under the Java test task.



回答2:

As @roby answered:

adding the following code to your build.gradle

apply plugin : 'java'

test {
    testLogging.showStandardStreams = true
}

Important!

You need to run gradle test or build with added clean command.

./gradlew clean test

or

./gradlew clean build

Hope that works.



回答3:

I am using also (testLogging.exceptionFormat = 'full'):

test {
    testLogging.showStandardStreams = true
    testLogging.exceptionFormat = 'full'
}

Which is good to see more from stacktrace



回答4:

For Android Gradle Files

If you are inside an android gradle file (if apply plugin: 'com.android.application' is at the top of your build.gradle file)

Then paste this into build.gradle

// Test Logging
tasks.withType(Test) {
    testLogging {
        events "standardOut", "started", "passed", "skipped", "failed"
    }
}


回答5:

This works me:

test {
    testLogging {
        showStandardStreams = true
    }
}


回答6:

Just to add, the:

showStandardStreams = true

is a shorthand for:

events = ["standard_out", "standard_error"]

It is important to keep this in mind when mixing both entries as the following:

test {
    testLogging {
        showStandardStreams = true
        events = ["passed", "failed", "skipped"]
    }
}

will result in no stdout whereas the reverse order:

test {
    testLogging {
        events = ["passed", "failed", "skipped"]
        showStandardStreams = true
    }
}

will add the stdout entries to the list, so stdout will work.

See the source for details.