Sunday, February 12, 2012

Unit Testing with ANT

Here I'm going to talk about how to write the XML code for Unit testing with ANT,

 <target name="compile" description="Compile Classes">
        <javac srcdir="${src}" destdir="${target}"/>
 </target>

In the above target, it compiles the source files from the ${src} and store them in the ${target}

<path id="test.classpath">
        <pathelement path="${test}"/>
        <pathelement path="${libs}/JUnit.jar"/>
        <pathelement path="${target}"/>
</path>
Here there are certain type of paths that you should introduce to the junit tests in ANT to run.
  1. ${test} : where your test classes are stored
  2. ${libs}/JUnit.jar : where your jUnit.jar is stored
  3. ${target} : where your compiled classes are stored
  <target name="compile-test" depends="compile" description="Compile Test Classes">
            <javac srcdir="${test}" verbose="true">
            <classpath refid="test.classpath"/>
        </javac>
 </target>
This particular target compiles the test classes to make them ready to run. The refid (means reference ID) here points to the paths mentioned above

    <target name="test-run" depends="compile-test" description="Run Test Cases">       
        <junit printsummary="yes" haltonfailure="yes" showoutput="yes" >
            <classpath refid="test.classpath"/>
            <batchtest fork="yes" todir="${test}">
                <formatter type="xml"/>
                <fileset dir="${test}">
                    <include name="**/*Test*.java"/>
                </fileset>
            </batchtest>
        </junit>
    </target>
 This is the test-run target, meaning this is where the test is run. All you need to understand is when selecting the fileset, I've selected all the test classes but you can limit them here.
If you've read through this simple tutorial, you should get a good understanding of how unit testing works. You can dig into more by following the link below.

If you need more details on ANT :

1 comment: