How to Initialize Database Variables and Assign Them Values - For Multiple Thread Groups
Creating a variable and assigning a value to it, is important for writing test cases that use database queries. In our last blog post “How to Initialize Database Variables and Assign Them Values”, we learned to initialize variables and assign values to them, in a database, for one Thread Group. In this blog post we will do it for multiple thread groups.
Let’s get started.
We will use the same API example we used in the previous blog post. This API uses input parameters (for example, the "address" parameter). It then takes the unique value (the client's address) and returns data (about the payments made by it). The API takes the information from the database (the Payment Table).
- The verification of the existence of a unique client address value is checked in the Customer Table, where address = address_id
- The unique value of the customer address, in the Customer Table, defines a unique customer number (customer_id)
- The value of the customer_id in the Payment Table shows the payments made by the customer
- If the payment exists, the API displays all the data from the Payment Table
- If there are no payments, the API returns an empty response
We will perform test cases for two clients (instead of one like last time). We will receive payment data for each customer in a separate Thread Group. Thus, each test case is a separate Thread Group.
Before launching each test case, incoming data should be checked to ensure that it corresponds to the purposes of the test case. To verify the incoming data in each Thread Group (we will assume that each Thread Group is a separate test case), you need to perform a client search to determine the availability of the payment data, and only then perform a request to the API.
In other words, each Thread Group will contain repeated actions to search for incoming data to perform a test case (in our example, repeated actions are a client search). To avoid these repetitive actions, you can search for clients in a separate Thread Group. This will reduce the time for the execution of the test case and avoid duplication of requests to the database, which can be done once.
To perform the test cases, you need to do the following:
1. Add two Thread Groups, to your script.
2. Configure the connection to the database as indicated in the previous article "MySQL Database and JMeter - How to Test Your Connection".
3. In each Thread Group element, add one JDBC Request element:
- Thread Group # 1. JDBC Request # 1 will perform a client search in the "Customer" table
- Thread Group # 2. JDBC Request # 2 and JDBC Request # 3 will search for payments in the "Payment" table for each customer
4. In the Test Plan element, add the View Results Tree element, which allows you to display the responses received from the database, as shown below.
5. For JDBC Request # 1 from Thread Group # 1, add the BeanShell Assertion element.
6. Search for 2 customers in the Customer table through an SQL Query.
7. Run the tests and view the results for Thread Group #1 - we can see the customers in the Customer table.
8. Assign customer_id values to variables and assign address_id values to variables in the BeanShell Assertion. The purpose is to get incoming data for several test cases (thread groups) but in one thread group, and then use them in several thread groups.
Note: The props.put method from the JMeterVariables class is used to create a variable available in any JMeter element in any Thread Group.
9. Get IDClient_1 and IDClient_2 in Thread Group # 2.
Since the variables IDClient_1 and IDClient_2 were not created in the Thread Group in which they will be used, it is necessary to get these variables before getting to the Thread Group in which they will be used.
To do this, in Thread Group # 2, add the BeanShell PreProcessor element.
10. Get the IDClient payment information from the Payment Table by passing the IDClient_1 and IDClient_2 variables through the SQL query.
The IDClient_1 variable is converted to a value at the time the request is submitted to the Payment Table. A similar conversion is performed for the variable IDClient_2.
The results of receiving all payments by IDClient_1 = customer_id from the payment table:
The results of receiving all payments by IDClient_2 = customer_id from the payment table:
That’s it! You now know how to initialize database variables and assign them values, when working with multiple thread groups.
You can learn more JMeter for free through our JMeter academy.