Interview Questions

How do I parameterize my JMeter test cases?

Apache JMeter FAQ

(Continued from previous question...)

How do I parameterize my JMeter test cases?

Parameters can be set at both the Test Plan and Thread Group levels.

At the Test Plan level, parameters can be used as constants to minimize changes throughout the test plan when a server or port changes, for example. [N.B. functions are not currently supported at Test Plan level.]

Within Thread Groups, the User Parameters Pre-Processor can be used to set different parameters for each simulated user.

Note: if only one user (i.e. User_1) is defined, the value will be used for all threads. Functions ARE supported here. See later FAQs for more information.

The following example parameterizes the pet category from the Java Pet Store.
1. Install Petstore application on a local Tomcat server.
2. Start JMeter
3. Click the Add button to add the following user defined variables to the Test Plan node:
1. server: localhost
2. port: 8080
3. protocol: http

4. Add a Thread Group to the Test Plan setting:
1. Number of Threads: 2
2. Clear the forever checkbox
3. Loop Count: 1
Add Config Element > HTTP Cookie Manager to Thread Group
Add Pre Processors > User Parameters to Thread Group

7. Click Add User to add an additional user to the User Parameters
8. Click Add Variable and set name to category, User_1 to DOGS and User_2 to FISH

9. Add Sampler > HTTP Request to Thread Group with the following settings:
1. Server Name or IP: ${server}
2. Port Number: ${port}
3. Protocol: ${protocol}
4. Path: /estore/control/category
10. Click the Add button to add a parameter to the request:
1. Name: category_id
2. Value: ${category}
Add Listener > View Results Tree to Thread Group
12. Save
Run > Start

When you view the results in the View Results Tree, you can see the ${category} value was replaced with the value from the User Parameters. Each thread will use the category from the User Parameter setting automatically. If there are more than two threads, they will reuse the values in these settings, so the third thread would use DOGS.

(Continued on next question...)

Other Interview Questions