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