在测试过程中,Gradle似乎会将stdout/stderr重定向到project_dir/build/reports/tests/index.html
。是否有方法避免此重定向,并将结果打印到控制台上?
附加信息:
- 这是一个Scala 2.9.1项目。
- 我正在使用slf4s进行日志记录。
apply plugin : 'java'
test {
testLogging.showStandardStreams = true
}
http://gradle.org/docs/current/dsl/org.gradle.api.tasks.testing.Test.html
这需要使用当前版本的Gradle。我假设Scala测试是在Java测试任务下运行的。
我也使用了 (testLogging.exceptionFormat = 'full'
):
test {
testLogging.showStandardStreams = true
testLogging.exceptionFormat = 'full'
}
从堆栈跟踪中看到更多信息是很好的
如果您目前在Android Gradle文件中(如果apply plugin: 'com.android.application'
出现在您的build.gradle文件顶部)
那么请将以下内容粘贴到build.gradle文件中:
// Test Logging
tasks.withType(Test) {
testLogging {
events "standardOut", "started", "passed", "skipped", "failed"
}
}
将以下内容粘贴到build.gradle文件中
// Test Logging
test {
testLogging {
showStandardStreams = true
}
}
正如@roby所回答的:
将以下代码添加到您的build.gradle
文件中
apply plugin : 'java'
test {
testLogging.showStandardStreams = true
}
重要!
您需要运行带有 clean
命令的 gradle test 或 build。
./gradlew clean test
or
./gradlew clean build
希望那能行。
只是补充一下,:
showStandardStreams = true
是以下缩写:showStandardStreams
events = ["standard_out", "standard_error"]
混合两个输入时要记住以下重要事项:
test {
testLogging {
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
将会导致没有标准输出(stdout),而反过来则不会。
test {
testLogging {
events = ["passed", "failed", "skipped"]
showStandardStreams = true
}
}
将标准输出的记录添加到列表中,因此标准输出将正常工作。
详见源代码。
showStandardStreams
在顺序上排在第二位时,只是为了双重检查,标准流是否被追加到事件列表中?看起来是这样的,但只是双重检查。 - George Pantazestest {
testLogging.showStandardStreams = true
}
和
test {
testLogging {
showStandardStreams = true
}
}
同样适用。
如果您正在使用 build.gradle.kts
的 Kotlin DSL,语法会有所不同。
请确保您的依赖项中有 JUnit:
dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}
那么您需要在测试任务中添加以下内容:
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
tasks.test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
exceptionFormat = TestExceptionFormat.FULL
events("skipped", "failed")
}
}
然后您可以根据自己的需要调整设置。
对于Android Gradle:
https://dev59.com/JGox5IYBdhLWcg3wNRtj#42425815对于Android Gradle KTS (Kotlin):
// Test Logging
tasks.withType<Test> {
testLogging {
events("standardOut", "started", "passed", "skipped", "failed")
}
}
./gradlew --info clean build test
使用--stacktrace
选项运行以获取堆栈跟踪。 使用--info
或--debug
选项运行以获取更多日志输出。 使用--scan
运行以获取完整的洞察信息。
build.gradle
)。// Call from root build.gradle
setupTestLogging()
fun Project.setupTestLogging() {
for (sub in subprojects) {
sub.tasks.withType<Test> {
testLogging {
exceptionFormat = TestExceptionFormat.FULL
}
}
}
}
exceptionFormat
就足以获得所需的结果,但上述提到的events("standardOut" ...)
也可以以同样的方式指定。
$ gradle clean test
命令或在testLogging
块内使用outputs.upToDateWhen {false}
。有关详细信息,请参阅文档。 - Johnny Baloney