When is parameterization used?
For example: a module needs to be tested with multiple sets of different data == to verify the correctness of the business
Login module: Correct username, password === Success; Wrong username, correct password === Failure
Postman implements parameterization
In actual interface testing, some parameters must be unique each time a request is sent (such as registration). In this case, postman can be used to parameterize the test data.
There are three main ways for postman to set parameters:
- Built-in variable implementation
- Use code in the Pre-request Script tab (recommended)
- Implemented in the form of external files; such as csv files/json format files
1. Implementation of built-in variables
Postman has the following three built-in variables, suitable for one-time use; Disadvantage: Internal key variables cannot be compared accurately
{ {$guid}}: Generation GUID; Follow the instructions:
{ {$timestamp}}: current timestamp; as shown below
You can check the time through the online timestamp conversion tool: https://tool.lu/timestamp/
{ {$randomInt}}: Random integer from 0-1000
2. Use code in the Pre-request Script tab to implement it (recommended)
Pre-request Script is the operation to be done before executing the interface request, and tests is the operation to be done after executing the request. Built-in variables are generally placed in Request. We can also use code to implement them in Pre-request Script. The advantage of using code is that they can be reused.
Pre-request Script is the same as test and supports javascript syntax
Several parameterizations implemented inPre-request Script; as shown below
//Get the current timestamp in milliseconds var now_time = Date.now() pm.globals.set("now_time",now_time) //guid implementation const guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' .replace(/x/g, () => (Math.floor(Math.random() * 16)).toString(16)) .replace(/y/g, () => (Math.floor(Math.random() * 4 + 8)).toString(16)); pm.globals.set("guid_value",guid) //Random integer implementation const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min pm.globals.set("randomInt_num",randomInt(8,15)) //Select implementation from multiple options const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min const getRandomValue = list => list[randomInt(0, list.length - 1)]; const charsInName = ['Wang','Li','Zhang'] pm.globals.set("people_name",getRandomValue(charsInName)) //Realize random mobile phone number const randomInt = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min var mobile_num = `18${randomInt(100000000, 999999999)}`; pm.globals.set("mobile_num",mobile_num) //Synchronization waiting implementation: wait for 5 seconds before starting execution const sleep = (milliseconds) => { const start = Date.now(); while (Date.now() <= start + milliseconds) {} } sleep(5000)
Practical example: writing code throughPre-request Script can accurately compare assertions in random parameters
1. Write code in Pre-request Script
2. Reference variables in the request; { {Variable name}}
3. Make assertions in tests to verify whether the actual results are consistent with the expected results.
4. View execution results
3. Parameterization through external files
In postman, in addition to the above two methods to achieve parameterization, you can also use external data files (supporting csv files and data containing json format text). External data files can currently be imported through the Runner tab.
csv file format example:
For csv files to work in Collection Runner, the first line must be the variable name to be used in the request. Each line is a use case and represents an iteration.
1. First prepare the csv file, the encoding is UTF-8; as shown above
2. Quote the variable name in postman. In the following figure, the wd parameter in Baidu search introduces { {search_word}}, the assertion in the tests tab refers to data.expected_result
3. Click the Runner button, check the request to be executed, set the number of cycles, import the csv file, and click run to execute; as shown below
4. View execution results
json file you need to make sure your file has an array of key/value pairs. Each element in the array is a key-value pair object, representing an iteration. key is used as the variable name to be used in the request, and value is used as the value of the key.
json file parameterization steps:
1. Prepare the json format test data file to be used;
2. In the postman script, use { in HTTP requests { key }} to quote, use data. key or data["key"] in script to quote;
3. Open the Runner window, click the Select File button next to Data, and import the jsondata parameterized file; click the Data File Type to select application/json, and click the Preview button to check that the data is correct.
4. View execution results
Recommended tutorials related to automated testing:
The latest automated testing self-study tutorial in 2023 is the most detailed tutorial for newbies to get started in 26 days. Currently, more than 300 people have joined major companies by studying this tutorial! ! _bilibili_bilibili
2023 latest collection of Python automated test development framework [full stack/practical/tutorial] collection essence, annual salary after learning 40W+_bilibili_bilibili
Recommended tutorials related to test development
The best in the entire network in 2023, the Byte test and development boss will give you on-site teaching and teach you to become a test and development engineer with an annual salary of one million from scratch_bilibili_bilibili
postman/jmeter/fiddler test tool tutorial recommendation
The most detailed collection of practical tutorials on JMeter interface testing/interface automated testing projects. A set of tutorials for learning jmeter interface testing is enough! ! _bilibili_bilibili
To teach yourself how to capture packets with fiddler in 2023, please be sure to watch the most detailed video tutorial on the Internet [How to Learn to Capture Packets with Fiddler in 1 Day]! ! _bilibili_bilibili
In 2023, the whole network will be honored. The most detailed practical teaching of Postman interface testing at Station B can be learned by novices_bilibili_bilibili
Summarize:
Optical theory is useless. You must learn to follow along and practice it in order to apply what you have learned to practice. At this time, you can learn from some practical cases.
If it is helpful to you, please like and save it to give the author an encouragement. It also makes it easier for you to search quickly next time.
If you don’t understand, please consult the small card below. The blogger also hopes to learn and improve with like-minded testers.
At the appropriate age, choose the appropriate position and try to give full play to your own advantages.
My path to automated test development is inseparable from plans at each stage, because I like planning and summarizing.
Test development video tutorials and study notes collection portal! !