Search code examples
antjunitjunit3

How can I have the Ant JUnit task run all tests and then stop the rest of the build if any test has failed


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>

I have a class like this:

public class UserTests extends TestCase {

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

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.

Is it possible to do this?


Solution

  • 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" />