简体   繁体   English

Gradle:如何将 output 从测试 STDERR/STDOUT 获取到控制台?

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

(Gradle 3.2.1) I run some java tests, which logs output in Stderr/Stdout. (Gradle 3.2.1) 我运行了一些 java 测试,在 Stderr/Stdout 中记录了 output。 I can see that output, if I start如果我开始,我可以看到 output

gradle test --info

but in that case, much of unwanted output from 3-rd party libraries is there too.但在那种情况下,来自 3-rd 方库的许多不需要的 output 也在那里。

Documentation suggests using logging.caputureStandardError / logging.caputureStandardError (loglevel) , but it doesn't seem to have any effect. 文档建议使用logging.caputureStandardError / logging.caputureStandardError (loglevel) ,但它似乎没有任何效果。

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

Then if running gradle test , not STDERR/STDOUT is output in console.然后,如果运行gradle test ,控制台中的 STDERR/STDOUT 不是 output。

How can I get just the output from the tests classes in console?如何从控制台的测试类中获取 output?

Add these lines to build.gradle : 将这些行添加到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. 注意: dependsOn cleanTest不是必需的,但如果不使用 ,则需要在test任务之前运行cleanTestclean任务。


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. 注意: outputs.upToDateWhen {false}不是必需的,但如果没有使用 ,则需要在test任务之前运行cleanTestclean task。

For more info and options see the documentation . 有关更多信息和选项,请参阅文档

For those using kotlin/kotlin dsl for gradle, you need to put the following in your build.grade.kts file: 对于那些使用kotlin / kotlin dsl进行gradle的用户,需要在build.grade.kts文件中添加以下内容:

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. 另外,如另一个答案所述,您需要运行“gradle clean test”才能输出每次打印输出。

The testLogging answer is correct. testLogging答案是正确的。 For me, since I already had a tasks.test section, I figured it'd be easier to put it there instead.对我来说,因为我已经有一个tasks.test部分,所以我认为将它放在那里会更容易。

tasks.test {
    useJUnitPlatform()
    this.testLogging {
        this.showStandardStreams = true
    }
}

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 )扩展上述@Talha Malik 解决方案(以及其他帖子中的解决方案),在处理多模块 android 应用程序时,可以使用以下内容(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) (请注意,虽然仅exceptionFormat就足以获得想要的结果,但上面提到的events("standardOut"...)可以以相同的方式指定)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在单元测试中测试函数的输出 (stdout/stderr) - How to test a function's output (stdout/stderr) in unit tests 有哪些方法可以运行 Rust cargo test 并让测试运行器将任何日志 output 显示到控制台 stdout 或 stderr? - What are ways to run Rust cargo test and have the test runner display any log output to the console stdout or stderr? 如何将stdout和stderr输出从Perl脚本重定向到Windows上的文件? - How do I redirect stdout and stderr output from a Perl script to a file on Windows? 如何从Test :: Script运行获取标准输出? - How to get the stdout from a Test::Script run? 捕获stdout和stderr以测试Node.js CLI - Capture stdout and stderr to test Node.js CLI 将所有“ yesod测试”的输出发送到控制台 - Sending all output from “yesod test” to the console Gradle 测试 - 为失败的测试打印标准输出/标准错误 - Gradle test - print stdout/stderror for failed tests 如何使用androidStudio和gradle在控制台中显示测试信息? - How to show test info in console using androidStudio and gradle? Gradle:如何在控制台中实时显示测试结果? - Gradle: How to Display Test Results in the Console in Real Time? 有没有一种方法可以在Grails Spock测试中输出到STDOUT? - Is there a method to output to STDOUT in a Grails Spock test?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM