Monday 6 April 2015

ANT build for running TestNG test cases

Hello, in this blog I'll talk about running TestNG test cases using ANT.

If you have developed any test automation suite using Selenium/REST, then you need to manage running the suite for every code change in the application, this usually happens on a different server, ie, you develop the test suite using your favourite IDE(ex: Eclipse, Netbeans).. but when you want to execute your test suite on a server different from your local system, then you need to use a build tool, that extracts your source code, dependent libraries, files etc.. from your local project work space.

ANT is a very famous tool commonly used for building the source code and triggering other tasks post certain tasks, its a very powerful tool when it comes to developing test automation framework and tests.

Below is my scenario, and I've shared my ideas on how I've used ANT for running my test cases.

Scenario.
  • I have a 'src' directory where my java classes rests, this include all my different packages consisting of my tests classes, utility classes, framework classes etc.(in simple words it is same as the 'src' directory under the eclipse workspace.
  • I have an xml file 'testng.xml' for running the selected test classes available above.
  • We cannot run the java files as it is, we need to compile them to generate class files, this we don't care while using IDE, because compilation is done automatically by the IDE every time you run the program.
  • But here we are not going to use IDE for running our tests, instead use ANT to run them.
  • So we need to first compile all the java files from the 'src' directory and then store them in a different directory 'bin'.
  • JVM can run the class files available in the 'bin' directory.
  • Configure ANT to first compile the java files and then use TestNG to run the test cases from the 'bin' directory.

How to do it?

Follow these steps carefully while you set-up the build process.
  • Create a new directory(project directory) in your local system or in the build server from which you want to run the tests.
  • Create sub folders 'src', 'bin', 'lib'.
  • From your local IDE workspace, do the below,
          copy the java files from 'src' folder to your newly created 'src' folder.
          copy the all the JAR files referenced in the project build path to 'lib' folder.

Now set-up ANT.
  • Download the ANT, unzip it and store in on your local system/build server from which you want to run the tests.
  • Set the path for ANT in your environment variable, and test if ANT is detected by your OS.
  • Now create a build.xml file and add the below items and save this file in the above project folder which we created initially.

<?xml version="1.0" encoding="UTF-8"?>
<project name="automation" default="main" basedir=".">

    <!--Define Path-->
    <path id="project-classpath">
        <fileset dir="./lib" includes="*.jar"/>
        <pathelement location="bin"/>
    </path>

    <!-- Initialization -->
    <target name="init">
        <echo>starting Ant Initilization Taskk</echo>
        <delete dir="bin"/>
        <mkdir dir="bin"/>
    </target>

    <!--Compile-->
    <target name="compile">
        <javac srcdir="src" destdir="bin">
        <classpath>
            <fileset dir="./lib" includes="*.jar"/>
            <pathelement location="bin"/>
        </classpath>
        </javac>
    </target>
</project>

  • Now you have created the first two important steps of a build process,  compiling and saving them in a folder.
  • The above xml file instructs ANT to use the java files in 'src' directory and using java-compiler compile them into class files, and store in 'bin' directory.
  • You can also see how all the JAR files are made use while compiling.
This will end here, next we need to run the generated class files using TestNG class.


To setup TestNG in the build process follow the below steps,
  • Download TestNG on to your local/sever machine.
  • Set the environment variable to point to the Testng.jar.
  • Now add the below set of code in your existing 'build.xml'.

<taskdef name="testng" classname="org.testng.TestNGAntTask">
        <classpath>
            <pathelement location="./lib/testng.jar"/>
        </classpath>
    </taskdef>

    <!--Execute TestNG testsuite -->
    <target name ="run">
        <testng outputdir="./test-output" classpathref="project-classpath"  useDefaultListeners="true">
            <xmlfileset dir="." includes="testng.xml"/>
            <!-- generate report title -->
            <sysproperty key="org.uncommons.reportng.title" value="Test Automation"/>
            <sysproperty key="org.uncommons.reportng.escape-output" value="false"/>
        </testng>
    </target>

  • we are now done with set-up of the ant and 'build.xml file,
  • Now create an xml file named 'testng.xml' and within that specify the test classes which you need to run as below in my case,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="FullRegression" parallel="none">
  <test name="Test">
    <classes>
   <class name="regressionPackage.Cases01"/>
   <class name="regressionPackage.Cases02"/>
   <class name="regressionPackage.Cases03"/>
   <class name="regressionPackage.Cases04"/>
   <class name="regressionPackage.Cases08"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

where 'regresionPackage' is the package name under which the test classes are stored.
and Cases01 to Cases08 are the actual test classes containing the tests.
  • Save this xml again under the same project directory where the 'build.xml' resides.
  • To run this process, through command line navigate to the above project directory and type 'Ant'. this will start the execution and you'll see the TestNG report generated into 'test-output' directory.

Tips and Tricks,

There were few manual steps which we did for copying the java files from the 'src' folder of your workspace to the newly created directory, but even this can be automated using ANT copy task (I leave this to you)

All the best for your new automated build process.

******************************************************************************

No comments:

Post a Comment