1 Parametric Interpretation
What is parameterization? Literally, it is to prepare the data in advance (in a broad sense, it can be a specific data value or a data generation rule), instead of writing it hard in the script, and the value is taken from the prepared data when the script is executed. .
Parameterization: It is a common technique for automated test scripts. Some inputs in the script can be replaced by parameters. For example, in the scenario where parameters are passed by GET/POST request during login, the value range of the parameters can be specified when the script is running. and rules. When the script is running, different parameter values are selected as input according to the needs. This method is called Data Driven Test (Data Driven Test), and the value range of the parameters is called Data Pool (Data Pool).
JMeter provides a variety of parameterization methods, and the following four are commonly used.
Way |
Applicable scene |
---|---|
CSV Data Set Config |
We usually refer to parameterization. Data is stored in files, with a wide range of parameterized values and strong flexibility |
User Parameter |
Applicable when the parameter value range is small |
function helper |
_Random and other functions, generate random numbers and random strings to achieve parameterization |
User Defined Variables |
User-defined variables, more for setting global variables |
2 Parametric implementation
2.1 CSV Data Set Config
When we mention parameterization in JMeter, we think of CSV Data Set Config (hereinafter referred to as CSV) by default. CSV can read the data in the file and generate variables, which can be referenced by JMeter scripts to achieve parameterization. Let's explore it in detail.
Introduction to CSV
Right-click the thread group –> Add –> Configuration Components –> CSV Data Set Config to create a CSV, the interface looks like this:
The parameters are explained in detail as follows:
parameter |
describe |
must |
---|---|---|
Name |
A descriptive name for this component as it appears in the script |
yes |
Filename |
file name. The name of the file to read. You can write an absolute path or a relative path (relative to the bin directory). If you write the file name directly, the file should be placed in the bin directory. For distributed testing, there should be the same CSV file in the corresponding directory in the host and remote machines |
yes |
File Encoding |
file encoding. The encoding format when reading the file, if not filled, the encoding format of the operating system will be used |
no |
Variable Names |
variable name. Multiple variable names must be separated by delimiters. If this item is empty, the first line of the file will be read and parsed as a list of column names |
no |
Ignore first line |
Is the first line ignored? Select false if there is no header in the csv file |
yes |
Delimiter |
delimiter. Separate a row of data into multiple variables, the default is a comma, and "\t" can also be used. If a line of data is delimited with fewer values than the variables defined in Vairable Names, these variables will retain their previous values (if any) |
yes |
Allow quoted data? |
Are double quotes allowed for variables? Variables can be enclosed in double quotes if allowed, and these variable names can contain delimiters |
no |
Recycle on EOF? |
Does it loop again when an end-of-file character is encountered? Default is true |
yes |
Stop thread on EOF? |
Does the thread stop when an end-of-file character is encountered? Default is true |
yes |
Recycle on EOF? |
When Recycle on EOF is False, stop the thread. When Recycle on EOF is True, this item is meaningless, and the default is false |
yes |
Sharing mode |
Thread sharing mode. 1. All threads (default): In a thread group, each thread (user) takes values in a unique sequence; 2. current thread: In a thread group, each thread (user) takes values in sequence; 3. Thread groups are independent, But each thread (user) in each thread group takes values in a unique order; |
yes |
What needs to be emphasized is that Sharing mode
it is the thread sharing mode . The thread sharing mode refers to the order mode in which multiple threads take values of file data. JMeter provides 3 modes:
All threads
: All threads. If the script has multiple thread groups, in this mode, all threads in each thread group must also take values in a unique order. For example, the script has 2 thread groups, each with 2 threads, and there are 5 lines of data in the file. When the script is running, it will take values repeatedly as shown in the figure below:
Current thread group
: The current thread group. Each thread group is isolated, and the thread order in the thread group has a unique value.
- Current thread: the current thread. In this mode, each thread is independent and sequentially unique values.
CSV example
Let's look at an example. First, there is a userInfo.txt file, placed in the bin directory , the content is as follows:
- File name: the file is in the bin directory, using a relative directory
- Variable name: The two columns of data belong to the two variables of mobile and password respectively
- Delimiter: separated by comma
The way to refer to CSV-generated variables in an HTTP request is ${变量名}
the way:
Run the script to see the resulting tree:
As you can see, the data in the file is successfully referenced by the script. JMeter's parameterization using CSV is as simple as that.
Precautions
One of the most common problems with CSV usage is incorrect file paths. When encountering this kind of problem, because there is no obvious prompt when running the script, many people will feel very confused after encountering it, and do not know where the problem is. In fact, if you observe carefully, you will find that the number in the yellow triangle in the upper right corner is increasing. Click this area to open the log, and the corresponding error is recorded in the log: , which means that the parameterization File userInfo2.txt must exist and be readable
file does not exist or the path is unreachable.
2.2 User Parameters
User Parameters, that is, user parameters, can also be parameterized.
Creation method: Right click on the HTTP request –> Add –> Pre processors (Pre processors) –> User parameters.
Add two variables of mobile and password through [Add Variable], and add 3 sets of data through [Add User]:
Reference parameterized data in an HTTP request:
Run the script, and the set data is successfully referenced.
This method is relatively simple, the data range is limited, and there are few applicable scenarios. Moreover, each thread will always use a set of data. For example, if 4 threads are set concurrently, then thread 1 uses the data of user_1, thread 2 uses the data of user_2, thread 3 uses the data of user_3, and thread 4 uses the data of user_1, no matter how many threads cycle Second-rate.
2.3 User-defined variables
User-defined variables can also realize parameterization of request parameters.
Creation method: right click on the thread group –> configure element (config element) –> user-defined variable.
As shown above, it is also very simple to use, just add variable names and corresponding values. The reference variable is used in the same way as before ${mobile}
.
[User-defined variables] are generally not used to parameterize HTTP requests, but to define global variables, such as parameterized file paths, hosts, urls, etc.
[User-defined variable] is created on [Thread Group], it will take effect in the thread group, if it is created on [Test Plan], it will take effect on all thread groups.
2.4 Random
The Random function in the function helper,
Creation method: Tools–>Function Assistant dialog–>Select a function–>_Random:
In the figure above, an expression is generated: ${__Random(8000,9000,)}
, we use this expression to replace the variable value we want to parameterize, such as the price variable in the figure below:
Run the script, look at the result tree, and you can see the effect:
This method is suitable for the parameterization of variables whose values are irregular and random in a certain interval, such as price, quantity, etc., but not suitable for parameterization of variables with strong rules, such as mobile phone numbers.
Friends who are doing the test can come in and communicate. The group has compiled a lot of learning materials and interview questions, project resumes, etc....