【问题标题】:How to capture stdout/stderr in junit 5 gradle test report?如何在 junit 5 gradle 测试报告中捕获 stdout/stderr?
【发布时间】:2017-08-26 08:39:53
【问题描述】:

我的 gradle 项目使用 junit 5,我正在尝试让测试报告显示在我的构建服务器上。 XML 报告基本上看起来不错——它包含所有测试类和方法,但缺少测试方法中打印的 stdout/stderr。只有一些 CDATA 包含测试元数据。

@Test
void testToString() {
    System.out.println("Hello world");
    ...
}

XML 报告:

<testcase name="testToString()" classname="com.my.company.PairsTest" time="0.008">
<system-out><![CDATA[
unique-id: [engine:junit-jupiter]/[class:com.my.company.PairsTest]/[method:testToString()]
display-name: testToString()
]]></system-out>
</testcase>

是否有设置告诉 gradle 插件捕获 stdout/stderr?我环顾了http://junit.org/junit5/docs/current/user-guide/#running-tests-build,但没有找到。

我正在使用org.junit.platform:junit-platform-gradle-plugin:1.0.0-M3org.junit.jupiter:junit-jupiter-{api,engine}:5.0.0-M3

【问题讨论】:

  • 当前未捕获到 stdout/stderr 的输出。您可以改用 TestReporter 吗? github.com/junit-team/junit5/blob/master/documentation/src/test/…
  • 大部分 stdout/stderr 是由正在测试的代码生成的,因此它们将无法使用 TestReporter。
  • (返回太快)@MarcPhilipp 是否有计划支持在 junit 中捕获 stdout/stderr,或者我应该在某处提交功能建议或 PR? IMO 的输出对于理解 CI 服务器上的测试失败非常有用,尤其是。如果故障在本地无法重现。

标签: gradle junit junit5


【解决方案1】:

正如 Marc Philipp 所提到的,目前在 JUnit 5 中不支持将输出捕获到 STDERR 或 STDOUT,无论是在平台中还是在 Jupiter 中。

如果您想要这样的功能,请在此处提出问题:https://github.com/junit-team/junit5/issues

更新(2017.11.01)

仅供参考:作为概念证明,我将基于 JUnit 4 的 OutputCapture 规则从 Spring Boot 移植到 JUnit Jupiter:https://github.com/sbrannen/junit5-demo/blob/master/src/test/java/extensions/CaptureSystemOutput.java

【讨论】:

  • 感谢您提出问题。
  • 仅供参考:我更新了答案以指向我的 Spring Boot OutputCapture 规则端口。
  • CaptureSystemOutput.java 正在工作
猜你喜欢
  • 2013-01-12
  • 1970-01-01
  • 2017-01-19
  • 2017-04-18
  • 2018-09-30
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多