Friday, March 8, 2013

How to Run Jmeter test script(.jmx) using WSO2 Test Automation Framework

Most of time Jmeter tests are executed from Jmeter tool GUI. Once you want to execute a test script without Jmeter GUI, There is a option called Jmeter command-line arguments or you can find some maven plugging to do it.

 Test Automation Framework also provide the ability to execute the Jmeter test without GUI. This post help you to run Jmeter script programatically. Test Automation framework look after underline Jmeter test execution. It use the Jmeter command-line options to execute the test and initialize a jmeter in the target directory. No Jmeter installation is required to execute the test.

I have a jmeter script which tests the service list page of the WSO2 ESB. Script contains following requests

1) Go to login page
2) Authenticate using username and password
3) Go to List Services page
4) SingOut
 
Create a maven project with bellow dependency
<dependencies>  
     <dependency>  
       <groupId>org.testng</groupId>  
       <artifactId>testng</artifactId>  
       <version>6.1.1</version>  
     </dependency>  
     <dependency>  
       <groupId>org.wso2.carbon.automation</groupId>  
       <artifactId>org.wso2.carbon.automation.tools.jmeter</artifactId>  
       <version>4.0.8</version>  
     </dependency>  
   </dependencies>  

Create a testng test class to execute the test. You need to point out your .jmx file path
package com.test;  
 import org.testng.annotations.Test;  
 import org.wso2.automation.tools.jmeter.JMeterTest;  
 import org.wso2.automation.tools.jmeter.JMeterTestManager;  
 import java.io.File;  
 public class JMeterTestToListServicesTestCase {  
   @Test()  
   public void listServices() throws Exception {  
     JMeterTest script = new JMeterTest(  
         new File("/home/nuwanw/projects/JmeterIntegrationWithFramework" +  
              "/src/test/resource/jmeter/ESB-list-service.jmx"));  
     JMeterTestManager manager = new JMeterTestManager();  
     manager.runTest(script);  
   }  
 }  

Then you have added a test to execute the jmeter test.
Add following surefire configuration in the pom file to run test while building the project

<build>  
     <plugins>  
       <plugin>  
         <artifactId>maven-surefire-plugin</artifactId>  
         <version>2.12.3</version>  
         <inherited>false</inherited>  
         <configuration>  
           <argLine>-Xms512m -Xmx1024m -XX:MaxPermSize=128m</argLine>  
           <testFailureIgnore>true</testFailureIgnore>  
           <disableXmlReport>false</disableXmlReport>  
           <parallel>false</parallel>  
           <includes>  
             <include>**/*TestCase.java</include>  
           </includes>  
         </configuration>  
       </plugin>  
     </plugins>  
   </build>  

You can see the test result after building the project if thee is no failures
Results :  
 Tests run: 1, Failures: 0, Errors: 0, Skipped: 0  

Jmeter test executor generate a separate log file and report(.jtl) file for each and every test script executed. If you need any Further Result, go to target directory. Then there is a directory called jmeter. under jmeter directory you can find logs and reports directories. inside those directories you can see a .log files and .jtl files. log and report files are generated from you test script name + timeStamp 

Now you can execute the Jmeter test which ensure a functionality of a product with proper assertions with the product integration test while building the product.



6 comments:

  1. Hi,

    I am trying to create a project for the above written code but i am not able to get maven dependency.
    I am getting the following error.

    Dependency '''org.wso2.carbon.automation:org.wso2.carbon.automation.tools.jmeter:4.0.8''' not found>

    Do we need to install JMETER before the project ?
    Please help me to create the project?

    ReplyDelete
    Replies
    1. Hi
      Can you please try the version 4.2.0 and check you maven online repositories urls.

      Thanks

      Delete
  2. Hi,
    In which path should be the testng test class placed?
    Can you please elaborate on the paths for each of the above.
    Thanks in advance

    ReplyDelete
  3. Thanks for great guide, Nuwan, much appreciated. There is another one which highlights more options on how to kick off a JMeter test without GUI, see 5 Ways To Launch a JMeter Test without Using the JMeter GUI for details.

    ReplyDelete
  4. Why do we need TestNG? Is JUnit not sufficient? I tried with JUnit and it run well. The only problem with WSO2 library is, it doesn't respect reuse connection parameter defined in JMX files.

    ReplyDelete
    Replies
    1. Yes. You can use any unit test framework. May be the JMeter version used is not support the reuse connection parameter

      Delete