我想通过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完成。这些是否容易实现?
答案 0 :(得分:0)
设置JUnit时,请将haltonerror
和haltonfailure
都设置为false
。这样,JUnit将继续处理。然后,还可以设置errorproperty
和failureproperty
参数。像往常一样运行JUnit。如果要生成报告,请生成报告。如果error属性或failure属性报告错误,则使用<fail/>
任务使构建失败:
<target name = "test"
depends="compiletest"
description="run unit tests">
<junit haltonfailure="false"
haltonerror="false"
failureproperty="failure.in.testing"
errorproperty="error.in.testing">
<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>
<!-- Now that everything has run, we'll check the error.in.testing -->
<!-- and the failure.in.testing properties to see if there were errors -->
<!-- of failed tests. -->
<fail message="JUnit had at least one error in a test${line.break}${error.in.testing}">
<condition>
<length string="${error.in.testing}"
when="greater" length="0"/>
</condition>
</fail>
<fail message="JUnit had at least one failed test${line.break}${failure.in.testing}">
<condition>
<length string="${failure.in.testing}"
when="greater" length="0"/>
</condition>
</fail>
</target>
我可以使用isset
条件:
<fail message="JUnit had at least one error in a test${line.break}${error.in.testing}">
<condition>
<isset property"error.in.testing"/>
</condition>
</fail>
<fail message="JUnit had at least one failed test${line.break}${failure.in.testing}">
<condition>
<isset property"failure.in.testing"/>
</condition>
</fail>
但是,我不确定这些属性是否未设置或仅等于空字符串。