Gradle:如何在控制台上实时显示testing结果?

我希望看到testing结果(system.out / err,来自正在testing的组件的日志消息), 因为它们在我运行的同一个控制台中运行:

gradle test 

而不要等到testing完成才能看到testing报告(这些testing报告只在testing完成时才会生成,所以在testing运行时我不能“尾巴”任何东西)

您可以在命令行上使用INFO日志logging级别运行Gradle。 它会显示每个testing运行的结果。 不利的一点是,你也将得到更多的输出其他任务。

 gradle test -i 

你可以在你的build.gradle文件中添加一个Groovy闭包,它为你做日志logging:

 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支持更多的选项来loggingtesting输出 。 有了这些选项,您可以通过以下configuration实现类似的输出:

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

随着stearyllase回答:

将下面的代码添加到您的build.gradle (自1.1版以来)工作正常输出通过跳过失败的testing。

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

另外我想说的是(我发现这对于初学者来说是一个问题)是, gradle test命令每次更改只执行一次testing。

所以如果你第二次运行testing结果将不会有输出 。 你也可以在building输出中看到这个:gradle然后在testing上说UP-TO-DATE 。 所以它没有执行第n次。

智能gradle!

如果你想强制testing用例运行,使用gradle cleanTest test

这是有点偏离主题,但我希望它会帮助一些新手。

编辑

正如sparc_spread在评论中所述:

如果你想迫使gradle 总是运行新的testing (这可能不是一个好主意),你可以添加testLogging { [...] } outputs.upToDateWhen {false}testLogging { [...] } 。 继续阅读。

和平。

这是我的花式版本:

看中测试结果

 import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent tasks.withType(Test) { testLogging { // set options for log level LIFECYCLE events TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_OUT exceptionFormat TestExceptionFormat.FULL showExceptions true showCauses true showStackTraces true // set options for log level DEBUG and INFO debug { events TestLogEvent.STARTED, TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_ERROR, TestLogEvent.STANDARD_OUT 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)) } } } } 

把这个添加到build.gradle来停止吞咽stdout和stderr的gradle。

 test { testLogging.showStandardStreams = true } 

这是logging在这里 。

“testing”任务不适用于Android插件,Android插件使用以下内容:

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

请参阅以下内容: https : //stackoverflow.com/a/31665341/3521637

作为Shubham伟大的答案后续,我喜欢build议使用枚举值而不是string 。 请看看TestLogging类的文档 。

 import org.gradle.api.tasks.testing.logging.TestExceptionFormat import org.gradle.api.tasks.testing.logging.TestLogEvent tasks.withType(Test) { testLogging { events TestLogEvent.FAILED, TestLogEvent.PASSED, TestLogEvent.SKIPPED, TestLogEvent.STANDARD_ERROR, TestLogEvent.STANDARD_OUT exceptionFormat TestExceptionFormat.FULL showCauses true showExceptions true showStackTraces true } } 

在使用Android插件的Gradle中:

 gradle.projectsEvaluated { tasks.withType(Test) { task -> task.afterTest { desc, result -> println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}" } } } 

那么输出将是:

执行testingtestConversionMinutes [org.example.app.test.DurationTest],结果为:SUCCESS

免责声明:我是Gradletestinglogging器插件的开发者。

您可以使用Gradletestinglogging器插件在控制台上打印精美的日志。

Gradle测试记录器动画

用法

 plugins { id 'com.adarshr.test-logger' version '<version>' } 

确保你总是从Gradle Central获得最新版本 。

组态

你根本不需要任何configuration。 但是,插件提供了几个选项。 这可以按照以下步骤完成(默认值显示):

 testlogger { theme 'standard' // pick a theme - mocha, standard or plain showExceptions true // set to false to disable detailed failure logs slowThreshold 1000 // set threshold in milliseconds to highlight slow tests showSummary true // displays a breakdown of passes, failures and skips along with total duration } 

我希望你会喜欢使用它。

Shubham伟大的答案和JJD合并使用枚举而不是string

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

我最喜欢的基于Shubham Chaudhary的简约版本的答案。 在这里输入图像说明

把这个在build.gradle文件中:

 test { afterSuite { desc, result -> if (!desc.parent) println("${result.resultType} " + "(${result.testCount} tests, " + "${result.successfulTestCount} successes, " + "${result.failedTestCount} failures, " + "${result.skippedTestCount} skipped)") } }