[英]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
任务之前运行cleanTest
或clean
任务。
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
任务之前运行cleanTest
或clean
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.