Welcome to this tutorial on setting up JMeter tests to run on Jenkins using Maven.
In this tutorial, we will cover the following topics.
- Creating A JMeter Test
- Setting Up A Maven Project
- Running JMeter From Command Line
- Running JMeter From Jenkins
So, you have created your first JMeter test and are now wondering how you can make it part of your CI pipeline. Well, you have come to the right place. In this tutorial you are going to learn how to run your JMeter tests on Jenkins using Maven. It is assumed that you already know how to use JMeter and can create a Maven project.
Creating A JMeter Test
Once you have a JMeter test, you may want to pass in parameters from the command line or from Jenkins at run time. Let’s assume that you want to specify the number of threads, ramp-up period and loop count. This is done by using a property function.
Click here to learn more about the __P property function.
As shown above, the three properties have been added, namely thread, rampUp and loopCount. The “1” after the property name indicates a default value. For instance, if no threads are specified when the test runs, the test will default to running with one thread.
Setting Up A Maven Project
Once your project is set up, you will need to edit its pom.xml file to tell Maven how to run the JMeter tests. You will add the JMeter Maven plugin, the name of the JMeter file (.jmx) and any user properties. When finished, the pom file should look something like this:
At the time of writing, the above shows the latest version for each plugin. In the future, you may need to update the versions accordingly.
The next thing you will need to do is to create a folder called jmeter inside src/test in the Maven project and place the .jmx file in that folder. Also, you need to verify that the pom.xml file has automatically been placed at the same hierarchy level as the src folder when the project was initially created. All this is depicted below.
Running JMeter From Command Line
Here is the command to run the test directly from the command line:
mvn clean -Dthreads=3 -DrampUP=3 -DloopCount=2 verify
As you can see, this command tells Maven to run the test with 3 threads, 3 seconds ramp up period and a loop count of 2. A successful build will look like this:
Running JMeter From Jenkins
Lastly, you will create a Jenkins project to run the JMeter test.
- In Jenkins, click New Item, give your project a name, select Freestyle project and click OK
- Click This project is parameterized in the project’s configuration screen
- Click Add Parameter drop down menu and select String Parameter
- Enter THREADS in the Name field (you may also enter a Default Value)
- Create two more parameters, RAMP_UP and LOOP_COUNT by following the same steps
- In the Build section, click the Add build step drop down and select Invoke top-level Maven targets
- Set the Goals field as follows
- Click Save
- Click Build with Parameters, your screen should look as follows
- Enter some values for each of the fields (or leave some of the fields blank)
- Click Build
The build should be successful, and any blank fields should be substituted by corresponding default values as specified in the JMeter file.
Congratulations, you can now run your JMeter tests on Jenkins and even take it a step further by adding reporting capabilities using the Jenkins JMeter plugin. Have fun!