[Software Testing] Use of variables in Postman

The variable types that can be set in Postman include global variables, environment variables, collection variables, data variables and local variables. The difference is that each variable has a different scope. Global variables apply to all collections, environment variables apply to the currently selected environment (different environment variables can be used in all collections), collection variables only apply to the current collection, and data variables are used for batch operations. Acts on the selected use case, and the settings in the local variable script only apply to the current script.

1. How to set and use global variables

1. Global variable setting method

1) Set in environment management and take effect immediately after saving

Insert image description here
Insert image description here

Insert image description here

2) Set global variables in the script, and the effective time is related to the execution of the use case

picture

2. Get the value of global variables

1) Use { {global variable name}} in the request parameters or body to get the variable value

Image] [image
Insert image description here

2) Use pm.globals.get("global variable name") in the script to obtain

picture

2. How to set environment variables and how to use them

1. Environment variable settings

1) Set in environment management

pictures pictures pictures

Insert image description here
Insert image description here

2) Set environment variables in the script . The effective time is related to the execution of the use case. The environment variables set in the script will be added to the corresponding environment after the use case is executed. The added environment is related to the environment selected when executing the use case.

Image] [image
Insert image description here

2. Get the value of the environment variable

1) Use { {environment variable name}} in the URL or request parameters or body to obtain the variable value (the variable value is related to the selected environment)

picture

2) Use the pm.environment.get("variable_key") method in the script to obtain

picture

3. Set method and use of set variables

1. Set variables in the edit collection

picture

2. Use { {collection variable name}} to read in the URL or request parameters or request body.

picture

4. Use of data variables

1. Most of them are used when running test cases in batches, and write the required test data in txt or csv or json documents.

pictures pictures pictures
Insert image description here
Insert image description here

2. Read the variable value in the file

2.1 If the data is edited with txt or csv text, you need to use data. variable name or data["variable name"] in the Pre-reques Script of the use case to obtain the value of the corresponding variable in the file and assign it to a global variable or environment variable. Then use the corresponding method to obtain the value of global variables or environment variables to read the value.

Image] [image

Insert image description here

2.2 If it is json text, you can directly use { {variable name in text}} to read the variable value in the text

picture

3. Click Runner in the interface or select Run in the collection to open the batch execution interface.

Image] [image

Insert image description here

4. Select file

4.1 When selecting a csv or json file for execution, the tool can automatically identify the file type and number of iterations

Image] [image

Insert image description here

4.2 The tool for selecting txt file execution cannot automatically identify the file type and number of iterations and needs to be modified by yourself.

picture

5. After selecting the file, check the use case that needs to be executed and click Run test to execute. The corresponding execution results can be viewed in the interface after execution. The file parameter values ​​can be viewed in the request URL or request body.

Image] [image

Insert image description here

5. Local variables

Local variable settings can only be set in scripts and can only take effect in the current script. Use var variable name = value to set local variables. Use the variable name directly in the script to get the corresponding value.

picture

Finally, I would like to thank everyone who reads my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, if you can use it, you can take it directly:

Insert image description here

This information should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey. I hope it can also help you!

Guess you like

Origin blog.csdn.net/NHB456789/article/details/132806999