Postman - configure environment variables

PostMan is a relatively convenient interface testing tool, but during use, API requests may be created, but the API URL will change as the server IP address changes.

In this case, it would be very troublesome to re-modify the URL for every API, so PostMan also provides environment variable settings.

We can set the current environment Environment in the upper right corner of PostMan.

Tip: In actual daily work, we generally create a Collections folder for a module, a business process or a project, and put all the interfaces to be tested in this range into this Collections folder. At the same time, we will also add Folder, configure different environment variables for different environments, such as: local environment, test environment, production environment, etc.

1. Operating environment variable area in PostMan

In the upper right corner of PostMan, as shown below:

(1) The first one is: select the environment variables you defined before.

When we newly registered and started using PostMan, we did not define any environment variables, so nothing was displayed when we clicked the drop-down button.

(2) The second one is: quickly view the environment variables and global variables currently in effect in PostMan.

(3) The third one is: setting environment variables and global variables.

The previous version was an icon of a small gear.

The meaning of the above picture in Chinese and English is:

  • Environment variables are a set of variables that allow you to switch the context of a request (that is, switching different requests can use the configuration in the environment variable), and you can also share the contents of the environment variable between multiple workspaces.
  • You can declare a variable in the environment and give it an initial value, then { { }}use it by putting the variable name in the inner request.
现在我也找了很多测试的朋友,做了一个分享技术的交流群,共享了很多我们收集的技术文档和视频教程。
如果你不想再体验自学时找不到资源,没人解答问题,坚持几天便放弃的感受
可以加入我们一起交流。而且还有很多在自动化,性能,安全,测试开发等等方面有一定建树的技术大牛
分享他们的经验,还会分享很多直播讲座和技术沙龙
可以免费学习!划重点!开源的!!!
qq群号:110685036

2. Set environment variables

Take our previous example of "Query 02 College Information" as an example, as shown below:

(1) Configuration of environment variables

Click the Set Environment Variables button and click on the pop-up page Add.

Configure environment variables as shown below:

illustrate:

  • Environment Name: Just give the environment variable a name, generally corresponding to Collections.
  • VARIABLE:variable name.
  • INITIAL VALUE: The initial value of the variable.
  • CURRENT VALUE: The current value of the variable.
    The current value of the variable is this value used when sending the request.
    The current value is never synced to Postman's server. If no changes are made, the current value will automatically adopt the initial value.
    It is also explained in the prompt at the bottom of the interface above that if you need a variable to use reused values ​​in different locations, you can set the current value of the variable to prevent sharing sensitive values ​​with the team.
  • Persist All: Replace the previous initial values ​​with the current values ​​of all variables.
  • Reset All: Replace the initial values ​​of all variables with the subsequent current values.
  • ...: Three dots are to check the display columns of the current page. The default is fine.

Finally, click if you want to add a new environment variable Add, or click if you want to re-edit the environment variable Update. Afterwards, you will see the environment variables you just set in the environment variable setting page, as shown below:

illustrate:

  • Click the environment variable name to edit the content of the environment variable.
  • You can also perform the following operations on environment variables:
    • Share: Share current environment variables.
    • Duplicate Environment: Copy the current environment variables.
    • Download Environment: Download current environment variables.
    • Manage Roles: Management role. You need to log in to the team to perform this action.
    • Remove from workspace: Delete the current environment variables from the current workspace.
      If there is only one copy of the current environment variable, it only exists in this workspace and cannot be deleted. You can delete it permanently (using Delete) or share it to another workspace and delete it later.
    • Delete: Delete the current environment variables.

Summary: We can define the public variables or variables with special needs in all interfaces currently tested in environment variables.

hint:

Postman is very friendly to cooperative development. In Postman, you can invite friends to join the team to collaborate on testing and writing.
Click Invite to invite members to join the team. You can also create your own independent Workspace to isolate different projects. Workspace can be found in the top toolbar of Postman. Click the drop-down box to see the workspace you are in. Personal is your own workspace, and Team is the team you created. Or the workspace of a team you’re a part of.

As shown below:

(2) Use of environment variables

{ {变量名}}The use of environment variables is also very convenient. You can use it to call environment variables in the interface information or parameters .

For example, the same exercise as above.

Select the "College System Test" environment variable that you just configured. (When there are multiple sets of environment variables, you can choose to switch here)

Then use the content in the environment variable, as shown below:

Summary: In the future, we can define different environment variables for different test processes or different test environments.
  • Convenient modification of public variables.
  • Convenient to switch between different environments.

The above is the entire process of using environment variables in PostMan.

3. Set global variables

Global variables can be used for all requests in Postman without switching. The configuration and use of global variables are basically the same as the configuration and use of environment variables.

(1) Configuration of global variables

Click the Set Environment Variables button and click on the pop-up page Globals.

Configure global variables and click SaveSave.

After saving, nothing will be displayed on the environment variables page, as shown below.

We can view global variables in Quick View.

(2) Use of global variables

The same as the use of environment variables, use { {变量名}}to call global variables.

illustrate:

There are some default global variables in Postman, which { { will be displayed when we enter the variable value in the input box, as shown below:

There are random ids, timestamps, random numbers, etc., which we can choose to use at our own discretion.

Special note:
When a global variable and an environment variable have the same name, the environment variable has a higher priority than the global variable.

4. Quick view button

We can click the Quick View button to view global variables and currently active environment variables.

At the same time, you can directly modify the current values ​​of environment variables and global variables in the quick view interface (the initial value cannot be modified, as mentioned before about the difference between the initial value and the current value), which is also a very practical function.

You can also click Editto directly re-edit environment variables or global variables.

5. Import environment variables

Click the Set Environment Variables button and click on the pop-up page Import.

We can import the Postman environment variables exported by others, and the file is a .jsonfile.

It's very simple, so I won't demonstrate it here.

6. Supplement:

When we call environment variables, we can see Ethat environment variables Grepresent global variables.

Also, when the environment variable and the global variable have the same name, the environment variable will overwrite the global variable, as shown below:

In the display description of the global variable name, a horizontal line appears on the content, indicating that it will be overwritten by the name variable in the environment variable.

Finally, I would like to thank everyone who read my article carefully. Looking at the increase in fans and attention, there is always some courtesy. Although it is not a very valuable thing, if you can use it, you can take it directly!

Software Testing Interview Document

We must study to find a high-paying job. The following interview questions are from the latest interview materials from first-tier Internet companies such as Alibaba, Tencent, Byte, etc., and some Byte bosses have given authoritative answers. After finishing this set I believe everyone can find a satisfactory job based on the interview information.
 

Insert image description here

Guess you like

Origin blog.csdn.net/IT_LanTian/article/details/132812217