Dave's Brain

Browse - Programming Tips - How do I run junit from Ant?

Date: 2014oct28
Language: Java
Product: junit, ant

Q.  How do I run junit from Ant?

A.  The first thing to know is that it runs on compiled code.
So don't point it at your .java files.  It expects your compiled code
to be put into a .jar file.  Note that you can not use a .war file -- it must be a regular .jar file.

So for me the first thing was a target to make the .jar file:

<target name="jar" depends="compile"> <mkdir dir="${test}"/> <jar jarfile="${test}/myproject.jar" basedir="${build}"/> </target>
Next, I have classes called *Test.java that I want to run junit on. These are compiled into *Test.class in the build folder. We make this target depend the "jar" step.
<target name="test" depends="jar"> <junit printsummary="on" haltonfailure="yes" showoutput="yes" logfailedtests="yes"> <!-- Needed so errors are displayed --> <formatter type="plain" usefile="false"/> <-- If you have a master-classpath add it to the classpath --> <classpath refid="master-classpath"/> <-- Now, add the .jar we just built --> <classpath> <pathelement location="${test}/myproject.jar"/> </classpath> <batchtest> <fileset dir="${build}"> <include name="**/*Test.class"/> </fileset> </batchtest> </junit> </target>
Finally this can be run via:
ant test

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.