Monday, March 19, 2012

Ant task/script for generating JUnit report

Here is an example explaining an ant task for running compiled classes and generating JUnit report after execution of classes.
  1. <target name="run-classes" depends="compile-classes">
  2.     <!-- Define the Ant tasks for running classes -->
  3.     <junit fork="yes" printsummary="no" haltonfailure="no">    
  4.         <batchtest fork="yes" todir="${build.loc}/reports" >
  5.           <fileset dir="${classes.dir}">
  6.             <include name="**/.class" />
  7.           </fileset>
  8.         </batchtest>
  9.         <formatter type="xml" />
  10.         <classpath refid="classpath" />
  11.     </junit>
  12.    
  13.     <!-- Ant task for generating reports -->
  14.     <junitreport todir="${build.loc}/reports">
  15.         <fileset dir="${build.loc}/reports">
  16.             <include name="TEST-*.xml"/>
  17.         </fileset>
  18.         <report todir="${build.loc}/reports"/>
  19.     </junitreport>     
  20.     <echo> Genereated JUnit Reports </echo>
  21. </target>

In the above ant target there are 2 sub-tasks i.e junit and junitreport.The junit task is for running test classes and junitreport is for generating JUnit report.

Explanation:
fork
     if true, it 'll run the tests in separate virtual machine.
printsummary:
     if yes, at the end of the test, a one-line summary will be printed.
haltonfailure:
     if yes, the build process will be stopped if the test fails.
The batchtest defines a number of tests based on pattern matching.
In this case formatter type is xml so an XML result will be output to result xml file

        The batchtest task of junit task 'll run all the tests classes that are in ${classes.dir} directory and produce xml files in the ${build.loc}/reports directory then the junitreport task take all the xml files that are starting with 'TEST-' and 'll generate report in the same directory.

No comments:

Post a Comment