Gradle:如何在控制台实时显示测试结果?
我希望看到测试结果(system.out/err,来自正在测试的组件的日志消息)当它们在我运行的同一个控制台中运行时:
gradle test
而不是等到测试完成才查看测试报告(仅在测试完成时生成,因此在测试运行时我无法“tail -f”任何内容)
I would like to see test results ( system.out/err, log messages from components being tested ) as they run in the same console I run:
gradle test
And not wait until tests are done to look at the test reports ( that are only generated when tests are completed, so I can't "tail -f" anything while tests are running )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
这是我喜欢的版本:
Here is my fancy version:
您可以在命令行上以 INFO 日志记录级别运行 Gradle。它将在运行时向您显示每个测试的结果。缺点是你也会为其他任务获得更多的输出。
You could run Gradle with INFO logging level on the command line. It'll show you the result of each test while they are running. Downside is that you will get far more output for other tasks also.
免责声明:我是 Gradle Test Logger 插件的开发者。
您只需使用 Gradle 测试记录器插件 在控制台上打印漂亮的日志。该插件使用合理的默认设置来满足大多数用户的需求,只需很少的配置或无需配置,但还提供了许多主题和配置选项来适合每个人。
示例
标准主题
Mocha 主题
用法
确保您始终从 Gradle Central 获取最新版本。
配置
您根本不需要任何配置。但是,该插件提供了一些选项。这可以按如下方式完成(显示默认值):
我希望您会喜欢使用它。
Disclaimer: I am the developer of the Gradle Test Logger Plugin.
You can simply use the Gradle Test Logger Plugin to print beautiful logs on the console. The plugin uses sensible defaults to satisfy most users with little or no configuration but also offers a number of themes and configuration options to suit everyone.
Examples
Standard theme
Mocha theme
Usage
Make sure you always get the latest version from Gradle Central.
Configuration
You don't need any configuration at all. However, the plugin offers a few options. This can be done as follows (default values shown):
I hope you will enjoy using it.
您可以在 build.gradle 文件中添加一个 Groovy 闭包来为您进行日志记录:
在您的控制台上,它的内容如下所示:
自版本 1.1 Gradle 支持很多 记录测试输出的更多选项。有了这些选项,您可以通过以下配置实现类似的输出:
You can add a Groovy closure inside your build.gradle file that does the logging for you:
On your console it then reads like this:
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:
正如 stefanglase 回答的那样:
将以下代码添加到您的
build.gradle
(自版本 1.1 起)对于通过、跳过的输出效果很好和失败测试。我想说的另外一点(我发现这对于初学者来说是一个问题)是
gradle test
命令每次更改仅执行一次测试。因此,如果您第二次运行它,则测试结果将不会有输出。您还可以在构建输出中看到这一点:gradle 然后在测试中显示 UP-TO-DATE。所以它没有执行第n次。
智能分级!
如果您想强制运行测试用例,请使用
gradle cleanTest test
。这有点偏离主题,但我希望它能对一些新手有所帮助。
编辑
正如sparc_spread在评论中所述:
如果你想强制gradle始终运行新的测试(这可能并不总是一个好主意)您可以将
outputs.upToDateWhen {false}
添加到testLogging { [...] }
。继续阅读此处。和平。
As stefanglase answered:
adding the following code to your
build.gradle
(since version 1.1) works fine for output on passed, skipped and failed tests.What I want to say additionally (I found out this is a problem for starters) is that the
gradle test
command executes the test only one time per change.So if you are running it the second time there will be no output on test results. You can also see this in the building output: gradle then says UP-TO-DATE on tests. So its not executed a n-th time.
Smart gradle!
If you want to force the test cases to run, use
gradle cleanTest test
.This is slightly off topic but I hope it will help some newbies.
edit
As sparc_spread stated in the comments:
If you want to force gradle to always run fresh tests (which might not always be a good idea) you can add
outputs.upToDateWhen {false}
totestLogging { [...] }
. Continue reading here.Peace.
将其添加到
build.gradle
以阻止 gradle 吞噬 stdout 和 stderr。它已记录此处。
Add this to
build.gradle
to stop gradle from swallowing stdout and stderr.It's documented here.
“测试”任务不适用于 Android 插件,对于 Android 插件,请使用以下内容:
请参阅以下内容:https://stackoverflow.com/ a/31665341/3521637
'test' task does not work for Android plugin, for Android plugin use the following:
See the following: https://stackoverflow.com/a/31665341/3521637
作为 Shubham 的精彩答案的后续,我建议使用 enum 值而不是 字符串。请查看文档TestLogging 类。
As a follow up to Shubham's great answer I like to suggest using enum values instead of strings. Please take a look at the documentation of the TestLogging class.
我最喜欢的简约版本基于 Shubham Chaudhary 的回答。
将其放入
build.gradle
文件中:My favourite minimalistic version based on Shubham Chaudhary answer.
Put this in
build.gradle
file:在 Gradle 中使用 Android 插件:
那么输出将是:
In Gradle using Android plugin:
Then the output will be:
如果您有一个用 Kotlin DSL 编写的
build.gradle.kts
,您可以使用打印测试结果(我正在开发一个 kotlin 多平台项目,没有“java”插件应用):If you have a
build.gradle.kts
written in Kotlin DSL you can print test results with (I was developing a kotlin multi-platform project, with no "java" plugin applied):对于 Android,这效果很好:
}
请参阅 运行 Android 单元/ 从控制台进行仪器测试
For Android, this works nicely:
}
See Running Android unit / instrumentation tests from the console
只需将以下闭包添加到您的 build.gradle 中即可。每次测试执行后都会打印输出。
Just add the following closure to your build.gradle. the output will be printed after the execution of every test.
合并 Shubham 的精彩答案 和 JJD使用枚举代替字符串
Merge of Shubham's great answer and JJD use enum instead of string
对于使用 Kotlin DSL 的用户,您可以执行以下操作:
For those using Kotlin DSL, you can do:
继 Benjamin Muschko 的回答(2011 年 3 月 19 日),您可以将
-i
标志与 grep,过滤掉数千个不需要的行。示例:强过滤器 - 仅显示每个单元测试名称和测试结果以及整体构建状态。不显示设置错误或异常。
软过滤器 - 显示每个单元测试名称和测试结果,以及设置错误/异常。但它也会包含一些不相关的信息:
软过滤器,替代语法:(搜索标记被分割成单独的字符串)
它如何工作的说明:
第一个命令是
./gradlew test -i
和"-i"
表示“Info/Verbose”模式,实时打印每次测试的结果,但也会显示大量不需要的内容调试线。因此,第一个命令
./gradlew test -i
的输出通过管道传输到第二个命令grep
,该命令将根据正则表达式过滤掉许多不需要的行。"-E"
启用单个字符串的正则表达式模式;"-e"
启用多个字符串的正则表达式;正则表达式字符串中的"|"
表示“或”。在强过滤器中,允许使用
" > "
显示单元测试名称和测试结果,并允许使用"BUILD"
显示整体状态。在软过滤器中,“-v”标志表示“不包含” " 和
"^"
表示“行的开头”。因此,它会删除所有以“Executing”或“Creating”开头的行,等等。Android 仪器单元测试示例,使用 gradle 5.1:
Jacoco 单元测试覆盖率示例,使用等级 4.10:
Following on from Benjamin Muschko's answer (19 March 2011), you can use the
-i
flag along with grep, to filter out 1000s of unwanted lines. Examples:Strong filter - Only display each unit test name and test result and the overall build status. Setup errors or exceptions are not displayed.
Soft filter - Display each unit test name and test result, as well as setup errors/exceptions. But it will also include some irrelevant info:
Soft filter, Alternative syntax: (search tokens are split into individual strings)
Explanation of how it works:
The first command is
./gradlew test -i
and"-i"
means "Info/Verbose" mode, which prints the result of each test in real-time, but also displays large amounts of unwanted debug lines.So the output of the first command,
./gradlew test -i
, is piped to a second commandgrep
, which will filter out many unwanted lines, based on a regular expression."-E"
enables the regular expression mode for a single string;"-e"
enables regular expressions for multiple strings; and"|"
in the regex string means "or".In the strong filter, a unit test name and test result is allowed to display using
" > "
, and the overall status is allowed with"BUILD"
.In the soft filter, the
"-v"
flag means "not containing" and"^"
means "start of line". So it strips out all lines that start with "Executing " or start with "Creating ", etc.Example for Android instrumentation unit tests, with gradle 5.1:
Example for Jacoco unit test coverage, with gradle 4.10:
如果您使用的是木星并且所有答案都不起作用,请考虑验证其设置是否正确:
然后尝试接受的答案
If you are using jupiter and none of the answers work, consider verifying it is setup correctly:
And then try the accepted answers
对于那些使用 Kotlin DSL 的人来说,更全面的回应:
这应该会产生接近 @odemolliens 答案的输出。
A more comprehensive response for those using th Kotlin DSL:
This should produce an output close to @odemolliens answers.
我为 Kotlin DSL 编写了一个测试记录器。您可以将此块放在项目范围
build.gradle.kts
文件中。I've written a test logger for Kotlin DSL. You can put this block on your project scope
build.gradle.kts
file.