Sunday, March 25, 2012

Ant task/script for generating TestNg report

Here is an example explaining an ant task for generating TestNG report after execution of classes defined in TestNG xml file.
  1. <property name="src.dir" value="src" />
  2. <property name="lib.dir" value="lib" />
  3. <property name="log.dir" value="logs" />
  4. <property name="build.loc" value="build" />
  5. <property name="classes.dir" value="${build.loc}/classes" />
  6. <property name="reports.dir" value="${build.loc}/reports" />
  7. <property name="testNG.report" value="${reports.dir}/TestNG" />
  8. <property name="suite.dir" value="suite" />
  9. <!-- Class-Path -->
  10. <path id="classpath">
  11.     <pathelement location="${classes.dir}"/>
  12.     <fileset dir="${lib.dir}" includes="*.jar"/>
  13. </path>
  14. <!-- Delete directories that are not needed -->
  15. <target name="delete-dir" >
  16.         <delete dir="${build.loc}"/>
  17.         <echo> /* Deleted existing Compiled Directory Classes */ </echo>
  18. </target>
  19. <!-- Create Directories -->
  20. <target name="create-source-dir" depends="delete-dir">
  21.     <mkdir dir="${classes.dir}"/>
  22.     <mkdir dir="${testNG.report}"/>
  23.     <echo> /* Created Directories */ </echo>
  24. </target>
  25. <!-- Compiling Tests -->
  26. <target name="compile-classes" depends="create-source-dir">
  27.     <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  28.         <classpath refid="classpath"/>
  29.     </javac>
  30.     <echo> /* Compiled Directory Classes */ </echo>
  31. </target>
  32. <!-- Running Tests and TestNG report generation -->
  33. <target name="testNGreport" depends="compile-classes">
  34.     <taskdef resource="testngtasks" classpathref="classpath"/>
  35.     <testng classpathref="classpath" outputDir="${testNG.report}" haltOnfailure="true">
  36.           <xmlfileset dir="." includes="${suite.dir}/testng.xml" />
  37.     </testng>
  38.     <echo> /* Run Directory Classes */ </echo>
  39. </target>
By the above target 'testNGreport' you can load TestNG task in ant and execute you selenium tests. It is just that ant would access your testng.xml file to execute tests. This is same as how you be executing tests from with eclipse using TestNG - Eclipse plugin. 
Below is the testNG file that is used in the above build file.
//TestNG.xml
  1. <suite name="Suite1" verbose="1" >
  2.   <test name="Regression1">
  3.     <classes>
  4.       <class name="test.sample.ExampleTest1"/>
  5.       <class name="test.sample.ExampleTest2"/>
  6.     </classes>
  7.   </test>
  8. </suite>

No comments:

Post a Comment