Tech and Media Labs
This site uses cookies to improve the user experience.




Running Unit Tests With Ant

Jakob Jenkov
Last update: 2014-05-24

You can run your JUnit unit tests with Apache Ant - an open source build tool. In this text I will briefly show you how. I will not explain Ant in complete detail. I expect that you already know Ant. If not, go the the Ant website (see link above), and study it.

Ant has a built-in task called "junit" which can run your unit tests. Here is a simple example:

<target name="unit-test-1" >

  <junit printsummary="yes" haltonfailure="yes">

    <classpath>
      <pathelement location="${project.class.path}"/>
      <pathelement location="${build.tests}"/>
      <pathelement path="${java.class.path}"/>
    </classpath>

    <test name="com.jenkov.test.MyUnitTest"
             haltonfailure="no" outfile="result">

      <formatter type="plain"/>
      <formatter type="xml"/>

    </test>

  </junit>
</target>

In the classpath element I add the output locations of my project code, and test code. In other words, the directories where my compiled code + test code is located.

In the test element I declare which unit test class to execute. I also specify the name of the output report in the outfile attribute ("result").

Nested inside the test element I�'ve put two formatter elements. These formatters define the output format of the output report specified in the test element. Since I have added two formatters, two output reports will be generated. One in "plain" text, and one in "xml".

Batch Test Runs

In the previous example only a single test was executed. It is also possible to execute a batch of tests, rather than having to specify every single unit test explicitly, in the Ant script.

Here is a batch unit test example:

<target name="unit-test-1" >

    <mkdir dir="${reports.tests}"/>

    <junit printsummary="yes" haltonfailure="yes">
      <classpath>
        <pathelement location="${project.class.path}"/>
        <pathelement location="${build.tests}"/>
        <pathelement path="${java.class.path}"/>
      </classpath>

      <formatter type="plain"/>
      <formatter type="xml"/>

      <batchtest fork="yes" todir="${reports.tests}">
        <fileset dir="${src.test}">
          <include name="**/*Test*.java"/>
        </fileset>
      </batchtest>

    </junit>

</target>

The classpath stuff is the same as in the first example.

Notice how the two formatter elements have now been moved outside the batchtest element. These formatters are now used for all test elements specified inside the junit element.

The batchtest task is what carries out the work. Here, the interesting attribute to notice is the todir attribute. This attribute specifies the directory to output the test report files to.

Inside the batchtest element is nested a standard fileset element, which tells what Java-files to include (meaning what tests to execute). In this example I execute all files that has the text "Test" and ".java" in their file name, regardless of what directory they are located in, recursively.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC