如何配置 Maven 将 JUnit 断言失败消息打印到控制台
我正在使用 Surefire Maven 插件来运行单元测试。我的测试类如下所示:
public class Test1 {
@org.junit.Test
public void testThatFails(){
Assert.assertTrue("false is never true", false);
}
}
当测试失败时,我希望看到与 AssertionError 关联的消息“false is never true”,但它不会打印到控制台。相反,我被指示在 Surefire 报告目录中进行挖掘并找到它(这很糟糕)。
Running com.example.Test1
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 5.799 sec <<< FAILURE!
Running com.example.Test2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Running com.example.Test3
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.122 sec
Results :
Failed tests:
testThatFails(com.example.Test1)
Tests run: 12, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
Please refer to /<path-to-pom.xml>/target/surefire-reports for the individual test results.
浏览到上述目录后,我找到了 .txt 文件,其中包含 JUnit 测试的错误消息,包括堆栈跟踪。我希望在构建过程中看到相同的消息和堆栈跟踪打印到控制台(如果它也被添加到报告文件中,那就太好了)。有什么想法吗?
I'm using the surefire maven plugin to run unit tests. My test class looks like this:
public class Test1 {
@org.junit.Test
public void testThatFails(){
Assert.assertTrue("false is never true", false);
}
}
When a test fails I expect to see the message "false is never true" that was associated with the AssertionError, but it's not printed to the console. Instead I'm instructed to dig around in the surefire report directory and find it (which sucks).
Running com.example.Test1
Tests run: 4, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 5.799 sec <<< FAILURE!
Running com.example.Test2
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.016 sec
Running com.example.Test3
Tests run: 7, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.122 sec
Results :
Failed tests:
testThatFails(com.example.Test1)
Tests run: 12, Failures: 1, Errors: 0, Skipped: 0
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] There are test failures.
Please refer to /<path-to-pom.xml>/target/surefire-reports for the individual test results.
After browsing to above directory I find the .txt file which contains the error message from my JUnit test, including the stack trace. I would like to see that same message and stack trace printed to the console during build (if it also gets added to a report file that would be great). Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有一个万无一失的配置,它将指定是将测试报告写入文件还是控制台:
useFile
。默认为true
。http://maven.apache.org/plugins/maven -surefire-plugin/test-mojo.html#useFile
使用 Maven,您可以通过在
pom.xml
中包含以下部分将其配置为false
:这将生成如下输出:
注意:
使用此配置设置可能会破坏显示测试失败的 CI 服务器的报告机制。 TeamCity 和 Jenkins 等 CI 服务器使用
surefire-reports.txt
文件来显示详细的测试失败信息,看起来此设置不会生成这些报告。不幸的是,我找不到一种方法可以使报告在两个控制台中显示并写入文件。There is a surefire configuration which will specify whether to write the test reports to a file or to the console:
useFile
. This defaults totrue
.http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#useFile
With Maven, you can configure it to be
false
by including the following section in yourpom.xml
:This will generate output like:
NOTE:
Using this configuration setting may break the reporting mechanisms of CI servers which show test failures. CI servers like TeamCity and Jenkins use the
surefire-reports.txt
files to show detailed test failure information and it looks like this setting makes these reports not get generated. Unfortunately, I couldn't find a way to make the report show in both console and get written to a file.