【发布时间】:2013-12-26 23:36:30
【问题描述】:
我想通过 Ant 进行设置,以运行我的 JUnit 测试并报告摘要级别和单个类级别的内容。我想让所有的测试都运行,即使有一些错误,但如果有任何错误,整个构建都会失败。
这是我目前在 build.xml 文件中的内容:
<target name = "test" depends="compiletest" description="run unit tests">
<junit haltonfailure="yes">
<classpath path="${buildtest}">
<path refid="test.classpath"/>
</classpath>
<batchtest fork="yes">
<formatter type="brief" usefile="false"/>
<fileset dir="${test}">
<include name="**/*Test.java"/>
<include name="**/Test*.java"/>
</fileset>
</batchtest>
</junit>
</target>
假设我有两个测试类,每个测试类都有 1 个错误,它会产生以下输出:
Buildfile: /opt/project/build/build.xml
init:
compile:
compiletest:
test:
[junit] Testsuite: com.company.foo.TestQuoteParser
[junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.013 sec
[junit]
[junit] Testcase: thisShouldThrowException(com.company.foo.TestQuoteParser): FAILED
[junit] Expected exception: java.lang.RuntimeException
[junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
[junit]
[junit]
BUILD FAILED
/opt/project/build/build.xml:88: Test com.company.foo.TestQuoteParser failed
Total time: 1 second
我希望执行每个测试类,无论之前的测试类是通过还是失败,但总体构建仍然失败。似乎我可以在第一次失败/错误时使构建失败,或者让所有测试运行但构建成功。我还想在最后打印一份总体摘要。像这样的:
Buildfile: /opt/project/build/build.xml
init:
compile:
compiletest:
test:
[junit] Testsuite: com.company.foo.TestQuoteParser
[junit] Tests run: 3, Failures: 1, Errors: 0, Time elapsed: 0.013 sec
[junit]
[junit] Testcase: thisShouldThrowException(com.company.foo.TestQuoteParser): FAILED
[junit] Expected exception: java.lang.RuntimeException
[junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
[junit]
[junit]
[junit] Testsuite: com.company.foo.TestEntityParser
[junit] Tests run: 6, Failures: 1, Errors: 0, Time elapsed: 0.023 sec
[junit]
[junit] Testcase: thisShouldThrowException(com.company.foo.TestEntityParser): FAILED
[junit] Expected exception: java.lang.RuntimeException
[junit] junit.framework.AssertionFailedError: Expected exception: java.lang.RuntimeException
[junit]
[junit]
[junit] Summary: com.company
[junit] Tests run: 9, Failures: 2, Errors: 0, Time elapsed: 0.038 sec
BUILD FAILED
/opt/project/build/build.xml:88: 2 failures
Total time: 1 second
我宁愿将所有这些都显示在屏幕上,而不必查看文件(甚至是漂亮的 HTML 报告文件),因为工作将通过 SSH 完成。有没有那么容易做到的?
【问题讨论】:
-
您是否考虑过使用 Jenkins 来构建您的项目。我知道 Jenkins 会运行你所有的测试用例,即使其中一些容易出错
-
Here is (link) 如何继续执行测试但最终构建失败。
标签: java unit-testing ant junit