测试时的测井

在测试时,Gradle 似乎将 stdout/stderr 重定向到 project_dir/build/reports/tests/index.html。有没有一种方法可以避免这种重定向,而是将内容打印到控制台?

附加信息:

  • 这是一个 Scala 2.9.1项目。
  • 我使用 SLF4进行日志记录。
76137 次浏览
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.

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

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

Which is good to see more from stacktrace

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.

test {
testLogging.showStandardStreams = true
}

and

test {
testLogging {
showStandardStreams = true
}
}

also works.

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"
}
}

For Regular Gradle Files

Paste this into build.gradle

// Test Logging
test {
testLogging {
showStandardStreams = true
}
}

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.

In my case I was working with Java and Spring-boot-starter-test.

I had the same issue and the problem was that I did not have any test engine.

So I add one to the dependencies of the build.gradle and it has work.

testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: dependencyVersion.junit5 testCompile group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: dependencyVersion.junit5

./gradlew --info clean build test

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

If you are using Kotlin DSL with build.gradle.kts the syntax is a bit different.

Make sure you have the junit in your dependencies:

dependencies {
testImplementation("org.junit.jupiter:junit-jupiter:5.4.2")
testImplementation("org.junit.jupiter:junit-jupiter-api")
testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine")
}

Then you need to add to your test task:

import org.gradle.api.tasks.testing.logging.TestExceptionFormat


tasks.test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
exceptionFormat = TestExceptionFormat.FULL
events("skipped", "failed")
}
}

Then you can adjust the settings based on your need.

For Android Gradle: https://stackoverflow.com/a/42425815/413127

For Android Gradle KTS (Kotlin):

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

Extending on @joshuakcockrell and @Blundell solutions (here and here), when dealing with a multi-module android app the following can be used (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)