Practical tips: How to implement parameter auto-increment in Postman Runner

What is Postman Runner?

Postman Runner is a powerful function in the Postman tool, which can be used to run API test scripts in batches. By setting different parameter combinations, we can easily test various situations of the API, and generate test reports and test results.

How does Postman Runner implement parameter auto-increment?

Step 1: Set global parameters

When using Postman Runner to run test scripts, we need to set a global parameter counter. Next, we will automatically increment this parameter through the script.

1. Click the eye icon in the upper right corner, find the Edit button in the Globals column and click it.

2. In the pop-up window, set the parameters. Fill in the parameter name counter in VARIABLE, and fill in the parameter value 1 in CURRENT VALUE. Finally, click the SAVE button to save the settings.

Step 2: Bring global parameters into request parameters

Now, we need to pass the global parameter counter into the request parameters. In this way, we can auto-increment the global parameters when running the API test script.

Add parameter counter at Params and set value as global parameter { {counter}}. Note that global parameters are surrounded by double curly braces { { }}. Now, we can click on the Send button to send the request. At this point, we can see that the value of the global variable counter is 1 in the result.

Step 3: Implement parameter auto-increment

Ok, now we have set the global parameter and brought it into the request parameter. Next, we need to use scripts to automatically implement the auto-increment operation of parameters.

1. Write the following script at Pre-request Script:

//postman.getGlobalVariable获取定义的全局变量
var temp = parseInt(postman.getGlobalVariable("counter"));
temp += 1;
//postman.setGlobalVariable设置定义的全局变量
postman.setGlobalVariable("counter", temp);

2. After the script setting is completed, click the Send button to run. At this point, we can see that the value of the global variable counter is automatically increased by 1 during the process of sending the request. Moreover, every time you click the Send button, Postman Runner will automatically increase the parameters according to the set rules.

Now, we have successfully implemented the parameter auto-increment function in Postman Runner. By using this function, we can easily test various situations of the API, thereby improving our development efficiency and testing quality.

Knowledge expansion:

Learn more about Postman related usage skills:

Guess you like

Origin blog.csdn.net/LiamHong_/article/details/131172793