如何配置Maven将JUnit断言失败消息打印到控制台

15

我正在使用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.

浏览到上面的目录后,我找到了一个包含我的JUnit测试错误消息(包括堆栈跟踪)的.txt文件。 我想在构建过程中将相同的消息和堆栈跟踪打印到控制台上(如果它也被添加到报告文件中,那就太好了)。 有什么想法吗?

1个回答

16

有一个能够确保配置是否将测试报告写入文件或控制台的方法:useFile。默认为true

http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#useFile

在Maven中,您可以通过在pom.xml中包含以下部分来将其配置为false

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <useFile>false</useFile>
            </configuration>
        </plugin>
    </plugins>
</build>

这将生成类似以下的输出:
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.example.MyTest
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.055 sec <<< FA
ILURE!
foo(com.example.MyTest)  Time elapsed: 0.017 sec  <<< FAILURE!
java.lang.AssertionError: false is never true
        at org.junit.Assert.fail(Assert.java:91)
        at org.junit.Assert.assertTrue(Assert.java:43)
        at com.example.MyTest.foo(MyTest.java:10)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(Framework
Method.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCal
lable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMe
thod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMet
hod.java:20)
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4Cla
ssRunner.java:79)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRun
ner.java:71)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRun
ner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.
java:53)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4
Provider.java:123)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider
.java:104)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(
ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke
(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(Suref
ireStarter.java:172)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWh
enForked(SurefireStarter.java:104)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:
70)

Results :

Failed tests:   foo(com.example.MyTest): false is never true

Tests run: 1, Failures: 1, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.675s
[INFO] Finished at: Wed Aug 10 13:15:46 CST 2011
[INFO] Final Memory: 7M/153M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.
9:test (default-test) on project test-output: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Code\experiments\test-output\target\surefire-reports
for the individual test results.
[ERROR] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureExc
eption

注意:

使用此配置设置可能会破坏 CI 服务器的测试失败报告机制。像 TeamCity 和 Jenkins 这样的 CI 服务器使用 surefire-reports.txt 文件来显示详细的测试失败信息,似乎这个设置使这些报告无法生成。不幸的是,我找不到一种方法使报告同时在控制台上显示并被写入文件。


我仍然收到 .xml 报告,不知道 Jenkins 等是否可以使用它们。 - cyber-monk
.xml 文件可能是 CI 服务器首选的文件格式,你很可能是安全的。我实际上没有尝试创建使用此选项的 TeamCity 或 Jenkins 作业;我不确定这是否会有问题。 - Jesse Webb
1
解决这个问题的简单方法是为jenkins/teamCity使用一个特殊配置文件,将该值设置为true。您的默认/本地开发配置文件将其值设置为false! - Nils
6
作为附加信息,值得一提的是,命令行选项为-Dsurefire.useFile=false - ericbn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接