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.

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

2 comments:

  1. Hi karthic,I have successfully configured Jenkins with Maven. My code is just launching Selenium driver..the code is mentioned below but the issue is the "execution completed" text is getting printed on jenkins console but the driver is not getting launched.

    package com.test;

    import org.junit.Test;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;


    public class AppTest
    {

    WebDriver driver;
    @Test
    public void open()
    {


    driver=new FirefoxDriver();
    driver.get("https://www.facebook.com/");
    System.out.println("execution completed");
    }
    }

    Please let me know if you have any idea

    ReplyDelete
  2. test it without Jenkins integration, check if that works first. But how r u triggering the test from Jenkins?, use the same command to run it first without jenkins.

    ReplyDelete