Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure JUnit Ant task to only produce output on failures?

Tags:

java

junit

ant

I am configuring JUnit in Ant so that unit tests will be run on each build. I would like the output of failing tests to be printed in the Ant console output whenever they are run. I don't need to see any output from succeeding tests.

Here is the relevant bit of my build.xml file:

<junit>
    <classpath>
        <pathelement path="${build}"/>
    </classpath>
    <formatter type="brief" usefile="false"/>
    <batchtest>
        <fileset dir="${src}" includes="my/tree/junit/"/>
    </batchtest>
</junit>

This produces almost what I want, failing tests are detailed in the Ant output, except that succeeding tests also write the following output:

    [junit] Testsuite: my.tree.junit.ExampleTest
    [junit] Tests run: 7, Failures: 0, Errors: 0, Time elapsed: 0.002 sec

I believe I have tried all the combinations listed in the JUnit task documentation, including:

  • printsummary attribute
  • showoutput attribute
  • formatter element with each kind of type

My use case is running ant from the command line. As I write more tests, I don't want the output from succeeding tests to be so large that output from failing tests scrolls off the screen. I just want ant to be quiet unless there's a failing test that needs my attention. How can I configure Ant/JUnit to do this?

I am using Ant version 1.6.4 and JUnit 4.6.

like image 760
Greg Hewgill Avatar asked Jun 08 '09 05:06

Greg Hewgill


People also ask

How do you skip test cases in ant build?

If your project's build file is constructed like that, then it's a matter of running ANT with the compile target and that will skip the tests. If it isn't built like that then there is no other way than to change the build file and separate the unit tests into it's own target that is not the default.

What is necessary to place in classpath during the execution of JUnit test cases from ant?

Do not put either in ANT_HOME/lib , and instead include their locations in your CLASSPATH environment variable. Add both JARs to your classpath using -lib . Specify the locations of both JARs using a <classpath> element in a <taskdef> in the build file. Leave ant-junit.


1 Answers

One possibility would be to define your own xml formatter with the 'classname' attribute (and extending org.apache.tools.ant.taskdefs.optional.junit.XMLJUnitResultFormatter, potentially doing nothing on endTest() or endTestsuite() methods).
That formatter would ignore info message and only display failure messages.


Note: this settings mention the possibility of only displaying failed tests:

<junit showoutput="true"
       fork="true"
       failureproperty="tests.failed"
       errorproperty="tests.failed">
    <batchtest todir="${test.results.dir}">
        <fileset dir="test">
            <include name="**/*Test.java"/>
        </fileset>
    </batchtest>
    <classpath path="${classes.dir}:${junit.jar}:${test.classes.dir}"/>
    <formatter usefile="false" type="brief"/>
    <!-- <formatter type="xml"/> If missing, only displays failed tests -->
</junit>

Did you test that ?

Note: the "showoutput="true" and <formatter type="brief" usefile="false"/>" can be a bit problematic, as illustrated by this recent (February 2012) ticket)


Yet another approch would be to define your ant Juint Test runner, supporting ant JUnitResultFormatter and displaying only stderr messages.

The EclipseTestRunner from eclipse is a good example.

like image 151
VonC Avatar answered Oct 19 '22 10:10

VonC