我如何让Ant JUnit任务运行所有测试,然后在任何测试失败时停止构建的其余部分

时间:2010-10-27 23:29:47

标签: ant junit junit3

我正在使用类似这样的目标通过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"似乎会导致构建在任何单个测试失败后立即停止,仅记录第一个失败的测试。将其设置为“off”会导致整个构建成功(即使测试失败消息写入输出)。

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

是否可以这样做?

1 个答案:

答案 0 :(得分:41)

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

<junit haltonfailure="no" failureproperty="test.failed" ... >
   ...
</junit>
<fail message="Test failure detected, check test results." if="test.failed" />
相关问题