Postman Series (8) - Global Variables (Global), Environment Variables (Environment), Collection Variables (Collection)

Table of contents

1. Variable scope

2. Global variables (Global), environment variables (Environment)

a. Example: Use variables to configure request header parameters

b. Example: Configure domain name using environment variables

3. Collection variables

Postman supports defining variables and accessing them within the scope allowed by the variables. This is especially useful when using the same value in multiple places.

1. Variable scope

Postman supports the following variable scopes: Global, Collection, Environment, Data, Local.

If a variable with the same name is declared in two different scopes, the value stored in the variable with the narrowest scope will be used. For example, if there is a Global variable and a Local variable both named username, the value stored in the variable with the narrowest scope will be used. Variables run under Local.

Variable scope is suitable for different tasks in Postman:

Environment variables ( Environment ): Allow customization for different environments, such as local development and testing or production. But only one environment can be active at a time.

Global variables ( Global ): Global variables can be used anywhere.

Collection variables ( Collection ): are available throughout the request in the collection and are environment-independent, so do not change based on the selected environment.

Local variable ( Local ): This variable is temporary and can only be accessed within the request script. Local variable values ​​are limited to a single request or collection run and are no longer available after the run completes.

Data variables ( Data ): CSV and JSON files from external sources that define datasets that can be used when running a collection through Newman or Collection Runner.

2. Global variables (Global), environment variables (Environment)

You can choose to create and edit environments and global variables through the Environment tab on the left side of postman or by using the environment quick browse button in the upper right corner.

Environment tab on the left side of postman

Use the Environment Quick Browse button in the upper right corner

When setting a variable, the variable name must be filled in , and the name is used to reference the variable in other locations.

The value of a variable is divided into two parts, "INTIAL VALUE (initial value)" and CURRENT VALUE (current value) .

The initial value can be shared with other accounts in the same team, and will also be synchronized to the account's cloud data. The current value can only be used locally and is not synchronized to cloud data.

When only the initial value or current value is filled in, the filled in value is used directly. If both the current value and the initial value exist, the current value is used.

The two buttons in the upper left corner of the page are used to synchronize the initial value and current value data. Persist All is used to synchronize the current value data to the initial value, and Rseset All is used to synchronize the initial value data to the current value.

After the variable definition is completed, the variable can be accessed. The way to apply variables is to use the variable key plus double curly brackets. For example: { {username}}

a. Example: Use variables to configure request header parameters

At work, we often encounter multiple interfaces using the same request header parameters. If each request header is manually configured, it is cumbersome and time-consuming. Request header parameters can be referenced in the form of global variables to simplify the configuration process.

Variables can be configured directly or generated through existing parameters.

To generate a global variable, you can select the value you want to use, select all values, and Postman will automatically pop up the set as variable button. After clicking it, fill in the value of the value and the variable type it belongs to, and you can directly generate the variable.

Click the "Set as variable" button to pop up the set variable pop-up box. You can select "Set as a new variable" to create a new variable, or you can set the value to the current value of the created variable.

(1) Select to set value as the current value of the created variable. The position of the original value automatically refers to the selected variable, and the current value (CURRENT VALUE) of the variable also becomes the value just set.

(2) Click "Set as a new variable" to pop up the new variable pop-up box. Fill in the variable key, select the variable scope (Scope) and click Set. It will be automatically referenced after the setting is completed.

b. Example: Configure domain name using environment variables

When testing interfaces, it is often necessary to use different domain names for testing, such as the domain name of the test line and the domain name of the official line. Using environment variables to switch domain names is a suitable method.

Set the domain name as an environment variable. It is recommended to use the same parameter name for the test line and the official line under the same domain name to facilitate switching environments.

Replace the domain name with a variable in the interface:

When you need to switch between the test line and the official line, click the environment drop-down box in the upper right corner of postman decryption to select the environment.

3. Collection variables

Collection variables can only be used in the current collection and are not controlled by the environment. Collection variables refer to variables in the same way as global variables and environment variables. Use the variable key plus double curly brackets, for example: { { username}}.

Collection variables can be added when the collection is created or any time afterwards.

When creating a collection, define variables in Variables. The way to define variables is the same as global variables and environment variables.

If you want to create or edit variables for an existing collection, select the collection in the Collections sidebar on the left side of Postman, or click Edit Collection and select Variables.

Postman series article directory:

Zero-Based Entry Interface Function Testing Tutorial-Table of Contents_Bad Poet_Ww’s Blog-CSDN Blog


The sample files used in the postman series of articles have been uploaded to my official account [Essential Skills for Software Testing]

If necessary, you can click on the article to download the QR code and go to get it~

Guess you like

Origin blog.csdn.net/weixin_40883833/article/details/126493208