tol*_*ius 209 testing console gradle
我希望看到测试结果(system.out/err,来自正在测试的组件的日志消息),因为它们在我运行的同一个控制台中运行:
gradle test
Run Code Online (Sandbox Code Playgroud)
而不是等到测试完成后才能查看测试报告(仅在测试完成时生成,所以在测试运行时我不能"尾随-f")
Ben*_*hko 152
您可以在命令行上使用INFO日志记录级别运行Gradle.它会在运行时向您显示每个测试的结果.缺点是你将获得更多其他任务的输出.
gradle test -i
Run Code Online (Sandbox Code Playgroud)
Shu*_*ary 151
这是我喜欢的版本:
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} passed, ${result.failedTestCount} failed, ${result.skippedTestCount} skipped)"
def startItem = '| ', endItem = ' |'
def repeatLength = startItem.length() + output.length() + endItem.length()
println('\n' + ('-' * repeatLength) + '\n' + startItem + output + endItem + '\n' + ('-' * repeatLength))
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
ste*_*ase 144
您可以在build.gradle文件中添加一个Groovy闭包,为您执行日志记录:
test {
afterTest { desc, result ->
logger.quiet "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
Run Code Online (Sandbox Code Playgroud)
在你的控制台上它然后如下所示:
: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
Run Code Online (Sandbox Code Playgroud)
Since version 1.1 Gradle supports much more options to log test output. With those options at hand you can achieve a similar output with the following configuration:
test {
testLogging {
events "passed", "skipped", "failed"
}
}
Run Code Online (Sandbox Code Playgroud)
Lan*_*tel 104
由于stefanglase回答:
将以下代码添加到您的build.gradle(自1.1版本)以来,对于传递,跳过和失败测试的输出都可以正常工作.
test {
testLogging {
events "passed", "skipped", "failed", "standardOut", "standardError"
}
}
Run Code Online (Sandbox Code Playgroud)
另外我要说的是(我发现这对初学者来说是个问题)是gradle test命令每次更改只执行一次测试.
因此,如果您第二次运行它,测试结果将没有输出.您还可以在建筑物输出中看到这一点:gradle然后在测试中说UP-TO-DATE.所以它没有执行第n次.
智能gradle!
如果要强制运行测试用例,请使用gradle cleanTest test.
这略微偏离主题,但我希望它能帮助一些新手.
编辑
正如sparc_spread在评论中所说:
如果要强制gradle这个来始终运行新的测试(这可能并不总是一个好主意),你可以添加outputs.upToDateWhen {false}到testLogging { [...] }.继续阅读这里.
和平.
ada*_*shr 84
免责声明:我是Gradle Test Logger插件的开发人员.
您可以使用Gradle Test Logger插件在控制台上打印漂亮的日志.该插件提供了许多主题和配置选项,以满足大量用户.
注意:Gradle Test Logger插件v1.4 +现在也支持并行测试执行.只需使用合适的主题.
plugins {
id 'com.adarshr.test-logger' version '<version>'
}
Run Code Online (Sandbox Code Playgroud)
确保始终从Gradle Central获取最新版本.
您根本不需要任何配置.但是,该插件提供了一些选项.这可以按如下方式完成(显示默认值):
testlogger {
// pick a theme - mocha, standard, plain, mocha-parallel, standard-parallel or plain-parallel
theme 'standard'
// set to false to disable detailed failure logs
showExceptions true
// set to false to hide stack traces
showStackTraces true
// set to true to remove any filtering applied to stack traces
showFullStackTraces false
// set to false to hide exception causes
showCauses true
// set threshold in milliseconds to highlight slow tests
slowThreshold 2000
// displays a breakdown of passes, failures and skips along with total duration
showSummary true
// set to true to see simple class names
showSimpleNames false
// set to false to hide passed tests
showPassed true
// set to false to hide skipped tests
showSkipped true
// set to false to hide failed tests
showFailed true
// enable to see standard out and error streams inline with the test results
showStandardStreams false
// set to false to hide passed standard out and error streams
showPassedStandardStreams true
// set to false to hide skipped standard out and error streams
showSkippedStandardStreams true
// set to false to hide failed standard out and error streams
showFailedStandardStreams true
}
Run Code Online (Sandbox Code Playgroud)
我希望你会喜欢使用它.
Dar*_*win 46
添加此项以build.gradle阻止gradle吞咽stdout和stderr.
test {
testLogging.showStandardStreams = true
}
Run Code Online (Sandbox Code Playgroud)
它在这里记录.
use*_*637 35
'test'任务不适用于Android插件,对于Android插件使用以下内容:
// Test Logging
tasks.withType(Test) {
testLogging {
events "started", "passed", "skipped", "failed"
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅以下内容:https://stackoverflow.com/a/31665341/3521637
JJD*_*JJD 17
作为Shubham的最佳答案的后续,我建议使用枚举值而不是字符串.请查看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
}
}
Run Code Online (Sandbox Code Playgroud)
And*_*ann 11
我最喜欢的简约版基于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)")
}
}
Run Code Online (Sandbox Code Playgroud)
在使用Android插件的Gradle中:
gradle.projectsEvaluated {
tasks.withType(Test) { task ->
task.afterTest { desc, result ->
println "Executing test ${desc.name} [${desc.className}] with result: ${result.resultType}"
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后输出将是:
使用结果执行测试testConversionMinutes [org.example.app.test.DurationTest]:SUCCESS
小智 7
如果你有一个用Kotlin DSLbuild.gradle.kts编写的你可以打印测试结果(我正在开发一个 kotlin 多平台项目,没有应用“java”插件):
tasks.withType<AbstractTestTask> {
afterSuite(KotlinClosure2({ desc: TestDescriptor, result: TestResult ->
if (desc.parent == null) { // will match the outermost suite
println("Results: ${result.resultType} (${result.testCount} tests, ${result.successfulTestCount} successes, ${result.failedTestCount} failures, ${result.skippedTestCount} skipped)")
}
}))
}
Run Code Online (Sandbox Code Playgroud)
对于 Android,这很有效:
android {
...
testOptions {
unitTests.all {
testLogging {
outputs.upToDateWhen { false }
events "passed", "failed", "skipped", "standardError"
showCauses true
showExceptions true
}
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
102434 次 |
| 最近记录: |