Gradle: How to get output from test STDERR/STDOUT into console?

做~自己de王妃 提交于 2020-05-07 10:43:06

问题


(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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40954017/gradle-how-to-get-output-from-test-stderr-stdout-into-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!