通过 Gradle 自定义 Junit5 测试输出
Customising Junit5 test output via Gradle
我正在尝试从我的 junit 测试中输出 BDD,如下所示:-
Feature: Adv Name Search
Scenario: Search by name v1
Given I am at the homepage
When I search for name Brad Pitt
And I click the search button2
Then I expect to see results with name 'Brad Pitt'
当 运行 在 IntelliJ IDE 中时,这显示得很好,但是当 运行 在 Gradle 中时,什么都不显示。我做了一些研究并启用了测试 showStandardStreams
布尔值,即
在我的 build.gradle
文件中,我添加了...
test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
}
}
这会产生...
> Task :test
Adv Name Search STANDARD_OUT
Feature: Adv Name Search
Tests the advanced name search feature in IMDB
Adv Name Search > Search by name v1 STANDARD_OUT
Scenario: Search by name v1
Given I am at the homepage
When I search for name Brad Pitt
And I click the search button2
Then I expect to see results with name 'Brad Pitt'
... 这非常接近,但我真的不想看到 gradle 的输出(带有 STANDARD_OUT
的行 + 额外的空行)。
Adv Name Search STANDARD_OUT
有没有办法不在 test
部分显示额外的 Gradle 日志记录?
或者我的测试根本不应该使用 System.out.println
,而是使用适当的日志记录(例如 log4j)+ gradle 配置来显示这些?
感谢任何帮助/建议。
更新 (1)
如果有人想快速克隆和 ./gradlew clean test
,我在 https://github.com/bobmarks/Whosebug-junit5-gradle 创建了一个最小可重现示例。
可以做的一件事是在 testLogging Options
中设置显示粒度 属性
来自文档
"The display granularity of the events to be logged. For example, if set to 0, a method-level event will be displayed as "Test Run > Test Worker x > org.SomeClass > org.someMethod". If set to 2, the same event will be displayed as "org.someClass > org.someMethod".
您可以将 test { … }
配置替换为以下内容以获得您需要的配置:
test {
useJUnitPlatform()
systemProperty "file.encoding", "utf-8"
test {
onOutput { descriptor, event ->
if (event.destination == TestOutputEvent.Destination.StdOut) {
logger.lifecycle(event.message.replaceFirst(/\s+$/, ''))
}
}
}
}
FWIW,我最初发布了以下(不完整的)答案,结果是关注配置测试日志记录的错误方法:
I hardly believe that this is possible. Let me try to explain why.
Looking at the code which produces the lines that you don’t want to see, it doesn’t seem possible to simply configure this differently:
- Here’s the code that runs when something is printed to standard out in a test.
- The method it calls next unconditionally adds the test descriptor and event name (→
STANDARD_OUT
) which you don’t want to see. There’s no way to switch this off.
So changing how standard output is logged can probably not be changed.
What about using a proper logger in the tests, though? I doubt that this will work either:
- Running tests basically means running some testing tool – JUnit 5 in your case – in a separate process.
- This tool doesn’t know anything/much about who runs it; and it probably shouldn’t care either. Even if the tool should provide a logger or if you create your own logger and run it as part of the tests, then the logger still has to print its log output somewhere.
- The most obvious “somewhere” for the testing tool process is standard out again, in which case we wouldn’t win anything.
- Even if there was some interprocess communication between Gradle and the testing tool for exchanging log messages, then you’d still have to find some configuration possibility on the Gradle side which configures how Gradle prints the received log messages to the console. I don’t think such configuration possibility (let alone the IPC for log messages) exists.
我正在尝试从我的 junit 测试中输出 BDD,如下所示:-
Feature: Adv Name Search
Scenario: Search by name v1
Given I am at the homepage
When I search for name Brad Pitt
And I click the search button2
Then I expect to see results with name 'Brad Pitt'
当 运行 在 IntelliJ IDE 中时,这显示得很好,但是当 运行 在 Gradle 中时,什么都不显示。我做了一些研究并启用了测试 showStandardStreams
布尔值,即
在我的 build.gradle
文件中,我添加了...
test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
}
}
这会产生...
> Task :test
Adv Name Search STANDARD_OUT
Feature: Adv Name Search
Tests the advanced name search feature in IMDB
Adv Name Search > Search by name v1 STANDARD_OUT
Scenario: Search by name v1
Given I am at the homepage
When I search for name Brad Pitt
And I click the search button2
Then I expect to see results with name 'Brad Pitt'
... 这非常接近,但我真的不想看到 gradle 的输出(带有 STANDARD_OUT
的行 + 额外的空行)。
Adv Name Search STANDARD_OUT
有没有办法不在 test
部分显示额外的 Gradle 日志记录?
或者我的测试根本不应该使用 System.out.println
,而是使用适当的日志记录(例如 log4j)+ gradle 配置来显示这些?
感谢任何帮助/建议。
更新 (1)
如果有人想快速克隆和 ./gradlew clean test
,我在 https://github.com/bobmarks/Whosebug-junit5-gradle 创建了一个最小可重现示例。
可以做的一件事是在 testLogging Options
中设置显示粒度 属性来自文档
"The display granularity of the events to be logged. For example, if set to 0, a method-level event will be displayed as "Test Run > Test Worker x > org.SomeClass > org.someMethod". If set to 2, the same event will be displayed as "org.someClass > org.someMethod".
您可以将 test { … }
配置替换为以下内容以获得您需要的配置:
test {
useJUnitPlatform()
systemProperty "file.encoding", "utf-8"
test {
onOutput { descriptor, event ->
if (event.destination == TestOutputEvent.Destination.StdOut) {
logger.lifecycle(event.message.replaceFirst(/\s+$/, ''))
}
}
}
}
FWIW,我最初发布了以下(不完整的)答案,结果是关注配置测试日志记录的错误方法:
I hardly believe that this is possible. Let me try to explain why.
Looking at the code which produces the lines that you don’t want to see, it doesn’t seem possible to simply configure this differently:
- Here’s the code that runs when something is printed to standard out in a test.
- The method it calls next unconditionally adds the test descriptor and event name (→
STANDARD_OUT
) which you don’t want to see. There’s no way to switch this off.So changing how standard output is logged can probably not be changed.
What about using a proper logger in the tests, though? I doubt that this will work either:
- Running tests basically means running some testing tool – JUnit 5 in your case – in a separate process.
- This tool doesn’t know anything/much about who runs it; and it probably shouldn’t care either. Even if the tool should provide a logger or if you create your own logger and run it as part of the tests, then the logger still has to print its log output somewhere.
- The most obvious “somewhere” for the testing tool process is standard out again, in which case we wouldn’t win anything.
- Even if there was some interprocess communication between Gradle and the testing tool for exchanging log messages, then you’d still have to find some configuration possibility on the Gradle side which configures how Gradle prints the received log messages to the console. I don’t think such configuration possibility (let alone the IPC for log messages) exists.