Jmeter (34) - From entry to advanced level - Parameterization (detailed tutorial)

1 Introduction

  The previous thirty or so articles mainly introduced some operations and basic knowledge of Jmeter, which can be regarded as some entry-level knowledge points. Starting from this article, we will learn the more advanced operations and in-depth knowledge points of Jmeter. Today's article is mainly about parameterization. In fact, similar knowledge points have been introduced more or less before. There is no systematic explanation of the knowledge. Since this is used more and often in actual work, Hong brother will introduce it today. It will be systematically explained as a separate topic.

Especially when using the jmeter tool, whether doing interface testing or performance testing , parameterization is a knowledge point that must be mastered and is very useful. Parameterized usage scenarios, such as:

  1) Multiple requests have the same IP address. If the server address is changed, the script needs to change the IP of each request.

  2) Register an account and do not allow duplicate accounts; when you want to register users in batches

  3) Simulate multiple user logins and need to use different user information to log in

  4) The output result of the previous request is used to pass in the parameters of the next request, such as the token information obtained by logging in, and the parameter call used to submit the bill request.

2. What is parameterization?

  Concept: The process of dynamically obtaining data and assigning values ​​according to requirements.

  Parameterization: In layman's terms, test data is an important preparation when testing. When the data for each iteration is different, parameterization needs to be performed and the test data is read from the parameterized file. . For example: a test user needs to enter a username and password when logging in. If the system does not allow the same username and password to log in at the same time, or if you want to better simulate multiple users to log in to the system. At this time, the user name and password need to be parameterized so that each virtual user uses a different user name and password for access.

If you want to learn automated testing, I recommend a set of videos to you. This video can be said to be the number one automated testing tutorial on the entire network played by Bilibili. The number of people online at the same time has reached 1,000, and there are also notes that can be collected and communicated with various channels. Master technical communication: 798478386   

[Updated] A complete collection of the most detailed practical tutorials on Python interface automation testing taught by Bilibili (the latest practical version)_bilibili_bilibili [Updated] A complete collection of the most detailed practical tutorials on Python interface automated testing taught by Bilibili (practical version) The latest version) has a total of 200 videos, including: 1. Why interface automation should be done for interface automation, 2. Overall view of request for interface automation, 3. Interface practice for interface automation, etc. For more exciting videos from UP master, please follow the UP account . icon-default.png?t=N7T8https://www.bilibili.com/video/BV17p4y1B77x/?spm_id_from=333.337.search-card.all.click

3. Several parameterization methods of jmeter

Here is the following registration interface as an example: Interface address: https://api.apiopen.top/api.html

Developer registration

Request method: POST

Request address: https://api.apiopen.top/developerRegister

Query parameter name type required describe Exampleeg
name string yes username peakchao
passwd string yes password 123456
email string yes Email, user feedback will be notified via email. [email protected]

Return example:

{
    "code": 200,
    "message": "成功!",
    "result": {
        "apikey": "b9b3a96f7554e3bead2eccf16506c13e"
    }
}

 

3.1 In jmeter, get the parameter value through ${variable name}.

1. User-defined variables

1) Right-click the thread group to add-->Configuration components-->User-defined variables, and the following setting page appears.

  Enter the name and value; note: the name can be customized, as shown in the figure below:

2) Then parameterize the set variable name to ${ip}, ${email} in the registration and login requests.

  Note: The referenced parameter name must be consistent with the name of the user-defined variable setting. Brother Hong here gives an example of registration, as shown in the figure below:

 3) Click Run to view the result tree, which shows success, as shown in the figure below:

2. Function assistant obtains parameter value

1) Select the menu bar option-->Function Assistant dialog box, and the function assistant box will pop up. There are multiple functions to choose from under the function. We will mainly look at __RandomString. You can learn about the others by yourself.

2) To register multiple accounts (for example, 10 users), the registration information requires that the email address and user name  cannot be repeated; so you can think of it this way, the @qq.com segment behind the QQ mailbox is fixed, then the segment in front of the QQ mailbox Several people are randomly selected, and the page settings are as shown in the figure below:

 3) Copy the generated function string and paste it for parameter reference, as shown in the figure below:

4) Set the number of threads in the thread group to 10 and simulate the registration of 10 virtual users, as shown in the following figure:

 5) Click Run to view the result tree, which shows success. (Brother Hong here only gives an example of the email address. You can imitate the user name after setting it. "Brother Hong here added a random function random after "Brother Beijing Hong", so Brother Hong Beijing Only if there is a number at the end so that it will not be repeated" can the registration be successful, otherwise an error of duplicate user name will be reported) as shown in the figure below:

3. CSV Data Set Config obtains parameter values

1) If the QQ email that needs to be registered cannot be random and must be the QQ email truly provided by the user for registration, use the following method to collect the user’s real QQ number and nickname in advance and store it in a local txt or CSV file (CSV file defaults separated by commas), as shown below:

2) Right-click the thread group and add-->Configuration Component-->CSV Data Set Config, as shown in the figure below:

 

Key parameter description:

  Filename: The full path of the file needs to be passed in. My file is located in the C drive directory and is named users.txt.

  File encoding: The encoding format of the parameter file. You don’t need to fill it in.

  Variable Names: The variable names corresponding to each column in the parameter file, which are also the parameter variable names you want to reference in the request. The first column here is the username, the second column is the password, and the third column is the email address. Variable names can be customized.

  igonre first line (only used if Variable Names is not empty): When the variable name is set in the first line of the CSV file, this item is set to true. At this time, each time a request is made to read the file, the first line will be automatically ignored and read directly. The second row of data. If the first line is not set, select False

  Delimiter: The delimiter in the file, the default is English comma separation. So please note that multiple parameters in each line of the txt document are separated by commas.

  Recycle on EOF: When set to True, the value is allowed to be recycled from the beginning; to False, when the file is read to the end, the file is stopped reading.

Stop Thread EOF: When Recycle on EOF is false and Stop Thread EOF is true, after reading the records  in the csv file , it stops running, and the number of threads and execution times are invalid.

  Sharing Mode: Sharing mode. All threads: all threads, all threads loop to get values, thread 1 gets the first line, thread 2 gets the next line; Current thread group: current thread group, each thread group loops to get values ​​respectively; Current thread: current thread, this test plan All threads inside take the first row.

Remarks: Here I will use popular language to briefly talk about the relationship between Recycle on EOF and Stop thread on EOF results.

Recycle on EOF: When reaching the end of the file, whether to read parameters in a loop, options: true and false

Stop thread on EOF: Whether to stop the thread when it reaches the end of the file, options: true and false

When Recycle on EOF selects true, it makes no sense to select true or false for Stop thread on EOF. In layman's terms, the continuous loop reading is controlled in the front, and it makes no sense to stop or run later.

When Recycle on EOF selects false, Stop thread on EOF selects true, there are 4 threads and 3 parameters, then only 3 requests will be made.

When Recycle on EOF selects flase, Stop thread on EOF selects flase, there are 4 threads and 3 parameters, then it will be requested 4 times, but there are no parameters available for the 4th time, and the loop is not allowed, so the 4th request is wrong.

3) Use CSV Data Set Config to define

a. Direct parameterized reference, as shown in the figure below:

b. Make parameterized references through function assistants, as shown in the figure below:

 

4) Set the number of threads in the thread group to 3. After execution, the following result will be output: Success, as shown in the figure below:

 

Guess you like

Origin blog.csdn.net/Faith_Lzt/article/details/132691889