20

我正在使用类似这样的目标通过 Ant 运行 JUnit:

<target name="junit" depends="compile">
    <mkdir dir="${report.dir}"/>
    <junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
        <classpath>
            <path refid="classpath"/>
            <path location="${classes.dir}"/>
        </classpath>
        <formatter type="brief" usefile="false"/>
        <formatter type="xml"/>

        <batchtest fork="yes" todir="${report.dir}">
            <fileset dir="${src.dir}" includes="**/*Tests.java" />
        </batchtest>
    </junit>
</target>

我有这样的课:

public class UserTests extends TestCase {

    public void testWillAlwaysFail() {
        fail("An error message");
    }
    public void testWillAlwaysFail2() {
        fail("An error message2");
    }
}

haltonfailure="yes"一旦任何单个测试失败,似乎会导致构建停止,只记录第一个失败的测试。将其设置为“关闭”会导致整个构建成功(即使测试失败消息被写入输出)。

我希望它运行所有测试(即使一个失败),然后如果任何测试失败则停止构建。

是否有可能做到这一点?

4

1 回答 1

43

您可以设置任务的failureproperty属性junit,然后使用任务测试属性fail

<junit haltonfailure="no" failureproperty="test.failed" ... >
   ...
</junit>
<fail message="Test failure detected, check test results." if="test.failed" />
于 2010-10-27T23:43:29.603 回答