我想看测试结果(系统)。out/err,来自正在测试的组件的日志消息),因为它们运行在我运行的同一控制台:

gradle test

不要等到测试完成才查看测试报告(测试报告仅在测试完成时生成,因此在运行测试时不能“尾部-f”任何内容)


当前回答

对于那些使用Kotlin DSL的用户,你可以这样做:

tasks {
  named<Test>("test") {
    testLogging.showStandardStreams = true
  }
}

其他回答

'test'任务不适用于Android插件,对于Android插件使用以下方法:

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

详见:https://stackoverflow.com/a/31665341/3521637

您可以在构建中添加Groovy闭包。Gradle文件,为您做日志记录:

test {
    afterTest { desc, result -> 
        logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
    }
}

在你的控制台上,它是这样的:

:compileJava UP-TO-DATE
:compileGroovy
:processResources
:classes
:jar
:assemble
:compileTestJava
:compileTestGroovy
:processTestResources
:testClasses
:test
Executing test maturesShouldBeCharged11DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test studentsShouldBeCharged8DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test seniorsShouldBeCharged6DollarsForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
Executing test childrenShouldBeCharged5DollarsAnd50CentForDefaultMovie [movietickets.MovieTicketsTests] with result: SUCCESS
:check
:build

从1.1版本开始,Gradle支持更多的选项来记录测试输出。有了这些选项,你可以通过以下配置实现类似的输出:

test {
    testLogging {
        events "passed", "skipped", "failed"
    }
}

你可以在命令行上使用INFO日志级别运行Gradle。它将在运行时显示每个测试的结果。缺点是其他任务的输出也会多得多。

gradle test -i

如果你正在使用jupiter,没有一个答案工作,请考虑验证它是否正确设置:

test {
    useJUnitPlatform()
    outputs.upToDateWhen { false }
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

然后试试那些公认的答案

合并Shubham的伟大答案和JJD使用enum而不是字符串

tasks.withType(Test) {
   testLogging {
       // set options for log level LIFECYCLE
       events TestLogEvent.PASSED,
            TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT
       showExceptions true
       exceptionFormat TestExceptionFormat.FULL
       showCauses true
       showStackTraces true

    // set options for log level DEBUG and INFO
       debug {
        events TestLogEvent.STARTED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.FAILED, TestLogEvent.STANDARD_OUT, TestLogEvent.STANDARD_ERROR
        exceptionFormat TestExceptionFormat.FULL
       }
       info.events = debug.events
       info.exceptionFormat = debug.exceptionFormat

       afterSuite { desc, result ->
           if (!desc.parent) { // will match the outermost suite
               def output = "Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)"
               def startItem = '|  ', endItem = '  |'
               def repeatLength = startItem.length() + output.length() + endItem.length()
               println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
           }
       }
   }
}