Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run all unit tests with Ant builder

I have a directory with a bunch of JUnit tests in my project. So far I have used separate target for each unit test. For example:

   <target name="MyTest">
        <mkdir dir="${junit.output.dir}"/>
        <junit fork="yes" printsummary="withOutAndErr">
            <formatter type="xml"/>
            <test name="tests.MyTest" todir="${junit.output.dir}"/>
            <classpath refid="MyProject.classpath"/>
        </junit>
    </target>

This method requires me to change build file every time I add a Unit test.
I want to able able to to run all unit tests in the project with a single Ant builder target.
Is it possible to do?

like image 250
Artium Avatar asked Jan 21 '11 15:01

Artium


1 Answers

Yep it is, you need to look at the fileset tag, e.g:

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

  <formatter type="xml"/>

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

The important part is the use of fileset and a glob/wildcard pattern to match the names of the tests. Full docs on the junit task with examples here:

http://ant.apache.org/manual/Tasks/junit.html

like image 111
Jon Avatar answered Sep 27 '22 18:09

Jon