Monday, January 2, 2012

How To Unit Test Your Loadrunner Java Scripts with the Junit Test Framework

This post provides a method for testing LoadRunner java protocol scripts with junit java testing framework.  Junit tests are typically  integrated into your continuous integration environment so that your LoadRunner scripts are tested on every check-in.  This ensures that code check-ins do not break your scripts.  Your scripts are guaranteed to be  functional at any time for load testing.

Suppose that the following is your target LoadRunner java protocol script:


import lrapi.lr;


public class Actions
{
    public int init() throws Throwable 
    {
        return 0;
    }//end of init




    public int action() throws Throwable 
    {
try
        {
            lr.start_transaction("test");
   lr.log_message("This is the test transaction");
   lr.end_transaction("test", lr.PASS);
        }
        catch (Exception e)
        {
            lr.end_transaction("test", lr.FAIL);
            lr.error_message("ERROR: Received exception "+e.toString());
            
        }
return 0;
    }//end of action




    public int end() throws Throwable 
    {
return 0;
    }//end of end
}

This java loadrunner scripts runs with the following output:


Running Vuser...
Starting iteration 1.
Starting action Actions.
Notify: Transaction "test" started.
This is the test transaction
Notify: Transaction "test" ended with "Pass" status (Duration: 0.0073).
Ending action Actions.
Ending iteration 1.
Ending Vuser...


To test your script with junit, two main issues problems have to be solved:

  • Api calls into lrapi.lr have to be stubbed out
  • A junit test case has to be defined calling the loadrunner script methods

These issues can be solved as follows:
  • Create a class LoadRunnerJunitTests
  • Create a junit test method that calls into the loadrunner classes.
  • Remove the lrapi.lr import
  • Create a stub class "lr" implementing the lrapi.lr api methods and member variables used
  • Remove the public visibility of the Actions class
  • Add unit test failure calls in places where the script fails

The resulting class tests your script using the junit framework as follows:

package test;

import org.junit.Assert;
import org.junit.Test;

/*
 *  Example of how to test a LoadRunner Java protocol script using junit
 */
//TESTABILTY CHANGE: add the following junit test class to run the script methods
public class LoadRunnerJunitTests
{
@Test
public void testLoadrunner() throws Throwable
{
    Actions a = new Actions();
a.init();
a.action();
a.end();
    }
}

// Target LoadRunner script to be tested.
//TESTABILTY CHANGE: removed public visibilty of Actions class
class Actions
{
 public int init() throws Throwable 
 {
     return 0;
 }//end of init


 public int action() throws Throwable 
 {
     try
     {
         lr.start_transaction("test");
         lr.log_message("This is the test transaction");
         lr.end_transaction("test", lr.PASS);
     }
     catch (Exception e)
     {
         lr.end_transaction("test", lr.FAIL);
         lr.error_message("ERROR: Received exception : "+e.toString());

         //TESTABILITY CHANGE: fail the unit test if the script fails
         Assert.fail("Loadrunner script failure");
         
     }
return 0;
 }//end of action


 public int end() throws Throwable 
 {
return 0;
 }//end of end
}

//TESTABILTY CHANGE: add the following lr class implementing the needed loadrunner api methods and member variables
class lr
{
public static String PASS = "PASS";
public static String FAIL = "FAIL";
public static String AUTO = "AUTO";
public static void start_transaction(String str)
{
System.out.println("starting transaction " + str);
}
public static void end_transaction(String str, String status)
{
System.out.println("ending transaction " + str + " with status " + status);
}
public static void log_message(String str)
{
System.out.println("log message " + str);
}
public static void error_message(String str)
{
System.out.println("error message " + str);
}
public static void set_transaction_status(String str)
{
System.out.println("set_transaction_status " + str);
}
public static void abort()
{
System.out.println("aborting ");
System.exit(1);
}
}

The junit test of the loadrunner script runs with the following output:

starting transaction test
log message This is the test transaction
ending transaction test with status PASS



31 comments:

  1. According to Oracle, 3 billion devices run on Java. The TIOBE Programming Community Index is updated monthly and regularly lists Java as the most popular programming language. Why the constant popularity? Because it has many real-world applications. IEEE Spectrum ranked it as #2 among the 2016 top programming languages.
    Java training institute in chennai

    ReplyDelete
  2. Spring is one of the most popular enterprise application frameworks in the Java landscape today. It is so popular that it’s even considered the de-facto standard for building large scale applications in Java today.Spring is not just a framework. It’s also a complete ecosystem of supporting frameworks and tools that provide you everything you need to build complex applications. It provides support for various common things that most enterprise applications need, like connecting to the database, managing business services, security and so on.
    spring mvc validation example

    ReplyDelete
  3. I simply wanted to thank you so much again. I am not sure the things that I might have gone through without the type of hints revealed by you regarding that situation.Authorized Dot Net training in chennai
    Advance Digital Marketing Training in chennai– 100% Job Guarantee

    ReplyDelete
  4. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    Devops Training in pune

    Devops Training in Chennai

    Devops Training in Bangalore

    AWS Training in chennai

    AWS Training in bangalore

    ReplyDelete
  5. I am sure this post has helped me save many hours of browsing other related posts just to find what I was looking for. Many thanks!
    python training in tambaram
    python training in annanagar
    python training in OMR

    ReplyDelete
  6. Thanks for your informative article, Your post helped me to understand the future and career prospects & Keep on updating your blog with such awesome article.
    Blue Prism Training in Pune

    Blueprism training in tambaram

    Blueprism training in annanagar

    ReplyDelete
  7. We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!
    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  8. feel free to shoot me an e-mail. I look forward to hearing from you! Great blog by the way!
    nebosh course in chennai

    ReplyDelete
  9. Its a good post and keep posting good article.its very interesting to read.
    MSBI Training Institute in Chennai

    ReplyDelete
  10. This is really too useful and have more ideas and keep sharing many techniques. Eagerly waiting for your new blog keep doing more.
    Devops Training in Chennai | Devops Training Institute in Chennai

    ReplyDelete
  11. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    Microsoft Azure online training
    Selenium online training
    Java online training
    Java Script online training
    Share Point online training


    ReplyDelete
  12. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.

    Best PHP Training Institute in Chennai|PHP Course in chennai
    Best .Net Training Institute in Chennai
    Dotnet Training in Chennai
    Dotnet Training in Chennai

    ReplyDelete
  13. good job done by you by posting this one

    BEST ANGULAR JS TRAINING IN CHENNAI WITH PLACEMENT

    https://www.acte.in/angular-js-training-in-chennai
    https://www.acte.in/angular-js-training-in-annanagar
    https://www.acte.in/angular-js-training-in-omr
    https://www.acte.in/angular-js-training-in-porur
    https://www.acte.in/angular-js-training-in-tambaram
    https://www.acte.in/angular-js-training-in-velachery

    ReplyDelete
  14. Amazing Article ! I have bookmarked this article page as i received good information from this. All the best for the upcoming articles. I will be waiting for your new articles. Thank You !
    IELTS Coaching in chennai

    German Classes in Chennai

    GRE Coaching Classes in Chennai

    TOEFL Coaching in Chennai

    Spoken english classes in chennai | Communication training

    ReplyDelete
  15. "I am looking for and I love to post a comment thatExcelR Online data science training
    ""The content of your post is awesome"" Great work!"

    ReplyDelete