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>

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.

Friday, March 16, 2012

Ant task for compiling classes

The following build.xml file contains 3 targets for deleting unnecessary directories, creating required directories and compiling classes in a specified directory.

//build.xml
  1. <project name="SampleBuildFile" default="compile-classes" basedir=".">
  2.     <property name="src.dir" value="src" />
  3.     <property name="lib.dir" value="lib" />
  4.     <property name="build.loc" value="E:/temp/build" />
  5.     <property name="classes.dir" value="${build.loc}/classes" />
  6.     <!-- Setting ClassPath -->
  7.     <path id="classpath">
  8.         <pathelement location="${classes.dir}"/>
  9.         <fileset dir="${lib.dir}" includes="*.jar"/>
  10.     </path>
  11.     <!-- Delete directories that are not needed -->
  12.     <target name="delete-dir" >
  13.             <delete dir="${build.loc}"/>
  14.             <echo> /* Deleted existing Directories */ </echo>
  15.     </target>
  16.     <!-- Create Directories -->
  17.     <target name="create-dir" depends="delete-dir">
  18.         <mkdir dir="${classes.dir}"/>
  19.         <echo> /* Created Directories */ </echo>
  20.     </target>
  21.     <target name="compile-classes" depends="create-dir">
  22.         <javac destdir="${classes.dir}" includeantruntime="false" debug="true" srcdir="${src.dir}">
  23.             <classpath refid="classpath"/>
  24.         </javac>
  25.         <echo> /* Compiled Classes */ </echo>
  26.     </target>
  27. </project>
In the above build file,

              Property task is used to set a property by name and value which are case sensitive.The attribute 'name' represents name of property and 'value' represents value to the property. By using this we can have an advantage when we want any modification further on property values because value represented by property name is used through out the build file so no need to change the property value for all.

Path task is used to building class path required to execute different ant targets in the build file.

             Here by default the 'compile-classes' target 'll call first when the build file 'll run, but this target depends on 'create-dir' target so it 'll call 'create-dir' target before it run.Again the 'create-dir' depends upon 'delete-dir' so it 'll call the 'delete-dir' target before 'create-dir' run.Clearly, the order of execution 'll be 
                       delete-dir   =>  create-dir   => compile-classes

             In the 3rd target the 'javac' task 'll compile all the java classes that are in the directory specified by 'srcdir' attribute i.e 'src' directory and all the compiles classes 'll be stored in the directory specified by 'destdir' attribute i.e 'E:/temp/build/classes'

The <classpath> is used to specify the PATH references using path tasks 'id' attribute value.

Ant script for create or delete directory

Ant Task for Deleting directories:
Below is an ant task that deletes the directory specified to 'dir' attribute of delete tag.
  1. <target name="delete-dir" >
  2.     <delete dir="build}"/>
  3.     <echo> /* Deleted existing directory */ </echo>
  4. </target>

The delete task deletes 'build' directory including all the files and subdirectories of 'build'.
The echo task just prints the message in console.

  1. <target name="delete-dir" >
  2.     <delete>
  3.         <fileset dir="." includes="**/*.java"/>
  4.     </delete>
  5. </target>

Here the delete task deletes all files with the extension '.java' from the current directory(represented by '.') and any subdirectories of it.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build"/>
  4.     </delete>
  5. </target>
Here the deletetask deletes all files and subdirectories of 'build', including build itself.

  1. <target name="delete-dir" >
  2.     <delete includeEmptyDirs="true">
  3.         <fileset dir="build" includes="**/*"/>
  4.     </delete>
  5. </target>
Here the delete task deletes all files and subdirectories of build, without build itself.

Ant Task for Making Directory:
Below is an ant task for making required directories specified to 'dir' attribute of 'mkdir' task.
  1. <target name="create-dir" >
  2.     <mkdir dir="build"/>
  3.     <echo> /* Created Directory */ </echo>
  4. </target>

The mkdir task creates a directory 'build' in to the root directory.
Note: mkdir creates the directory if not exists only, if exists it does nothing.