Showing posts with label TestAutomation. Show all posts
Showing posts with label TestAutomation. Show all posts

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.

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

Tuesday, 31 March 2015

Continuous Integration with Jenkins for Test Automation.


Hello, in this blog I would like to share my knowledge and understanding on Continuous integration for test automation using Jenkins.

As a test automation engineer we need to understand about using the continuous integration methodology well, as firms are moving towards agile development the rate of code check-ins to the source code management are more.

With every code check-in, the probability of breaking the functionality of a system is considerable, this can be checked and prevented by using continuous  integration. If we test the system functionality using fully automated unit and integration tests soon after a code check-in happens, then the possibility of missing a code flaw into the system will be very low and if the code breaks during the build soon after the check-in the developer or the team can take a decision on the next steps, usually it will be the code roll-back.

In brief this is how the continuous integration works in real time.

  • A central repository for source code management (SVN, Git)
  • Test automation suite consisting of both integration tests and functional tests.
  • A build server on which the Jenkins, code builder(Ant/Maven) and the Test runner (Junit/TestNG/MTR).
  • Jenkins is configured to build the latest code from the repository and as a post build action run the automated tests.
  • So whenever a code check-in happens, Jenkins execute the above step, else Jenkins can also be configured to run on a daily basis (nightly build), where the build action happens only once in a day during night.
  • Jenkins sends an email notifications as well to the respective people after the test run is completed.


As automation testers we need to achieve the following to be part of the Continuous integration process,

  • Automate all our test cases that includes your Functional, Integration and UAT cases, use selenium/SoapUI/ or your customized automation tool.
  • Use respective test runners to trigger the test cases automatically, via command line, I use ANT to trigger my REST api test suite.
  • Identify a way to generate user friendly test reports, (most of the existing test framework has their inbuilt test report generator), I use TestNG which gives an Html report.
  • Collect all the dependencies like JAR files, DLLs etc.. and place them in one place for reference.
  • Design your test suite in such a way that you put all your automation code, reference libraries, test runner files etc.. into one directory, that can be moved to any system without difficulty and cab be run with no efforts of set-up.


Setup Jenkins server as below,

  • Install Jenkins as windows service or to run manually download 'Jenkins.war' from here.
  • To start Jenkins server, browse to the directory where you've downloaded 'Jenkins.war' via command line.
  • Now run java -jar jenkins.war --httpPort=8989 command, this will start Jenkins locally on port 8989.
  • Goto your favourite web-browser and explore to localhost:8989 in the URL tab, this will open the Jenkins dashboard.
  • Take your time to explore different options, links and other tabs on this dashboard.


Setup Jenkins to run automated Test run as below,

Create a new item(jenkins Job) from the home page of Jenkins dashboard.
Click on 'New item' and then give a project name(item name) and select 'Freestyle project', click 'OK'



  • Give a project description for this item, this will be helpful if future if you are handling with multiple projects at a time.
  • Select 'None' in the source code management option.

  • Do not select anything in 'Build Triggers'.
  • Select 'Invoke Ant' option in 'Build'.
  • Save these now.

 


  • After saving goto the home page of this dashboard again.
  • Click on 'Manage Jenkins' and select 'Configure system'
  • Set the environment path for the JAVA class and Ant.
  • Set the JDK path for JAVA_HOME, as shown below.


















  • Set the ANT bin path as shown below.



Now we are done with our Jenkins Set-up.


Set-up the automation code to be run after every build.

  • when you run the Jenkins server on your machine, it will create a local workspace for running the Jenkins jobs/projects.
  • Now explore that directory and see that there is a directory created for your newly created jenkins project the directory structure would be 'c:\program files\jenkins\jobs\your-project\workspace\'
  • You need to place your bundled automation project right in this path which I talked about in the very beginning.
  • In my case as seen above, I've used ANT to trigger my tests, Jenkins trigger ANT and ANT will use my 'build.xml' file available in the workspace where my entire project is bundled, to run the tests.


We are done with our Jenkins setup to run our automated tests.... CONGRATS !!!


Now Goto the main Dashboard page where you can see your project in the list of available items.


To run this item, click on the icon with green arrow seen at the end.
Now your test will run and the console output will be stored in Jenkins against the build number.



  • To explore more on customizing your test runs, email notifications see Jenkins website.
  • Usually, the test automation is run after the actual code build happens, so Jenkins allows you to trigger this item as a post-build action, so that every time there is a code change in the application and check-in happens, the code build happens (this is another separate build item) and as a post-build action our above set up item will be executed.

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