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

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

问题描述:

我正在使用如下目标通过 Ant 运行 JUnit:

I'm running JUnit via Ant using a target something like this:

<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" 似乎会导致构建在任何单个测试失败后立即停止,仅记录第一个失败的测试.将其设置为关闭"会导致整个构建成功(即使测试失败消息已写入输出).

haltonfailure="yes" seems to cause the build to halt as soon as any single test has failed, logging only the first failed test. Setting it to "off" causes the entire build to succeed (even though test failure messages are written to the output).

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

What I want it for all tests to be run (even if one has failed), and then the build to be stopped if any tests have failed.

可以这样做吗?

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

You can set the failureproperty attribute of the junit task, then test the property afterwards with the fail task:

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