Variable usage scenarios
Postman main variables and parameters for association
- Application 1 : Common variable set or may be provided by use of global variables
- Application 2 : When a set of interfaces to be tested in different environments, can create two environments, such as
test
the environment andstage
the environment, both environments addbase_url
variables and set different values, url request interface is used{{base_url}}
, so that only the Postman switching environment different environments can be tested
- Application 3 : Interface dependence, because the request Postman is read-only, we can not modify the request to set a good Pre-request Script script, then I generally required by the script variable to a collection / global / environmental variables, is then used in the request
{{变量名}}
to read variables to
Variable setting
Postman There are three variables:
Collection variables (the collection and subfolders all requests entered into force) can be added in New / Edit Collection-> Variables in
Global variables (effective for all requests), environmental management - Add> Globals in
Environment variables (to take effect when selecting the environment), click Add to add Environmental Management page or click on the name of an existing environment variable settings into the environment
- VARIAVLE: variable name
- INITIAL VALUE: The initial share value, the default value for the team when others shared use
- CURRENT VALUE: current value, variable value of their current use (generally we can only set this value)
- Perisit All: Keep all, the current value (CURRENT VALUE) for their own use to replace all of the initial value
- Reset All: Reset all, all of the current CURRENT VALUE reset to the initial value as the current
Use variables
- Request url, params body parameters or table or JSON / XML text by
{{变量名}}
using
- Use in Pre-request Script and Tests script
in the script required by using the following statement
var v_a = pm.variables.get("a"); // 获取项目变量
var g_a = pm.globals.get("a"); // 获取全局变量
var e_a = pm.environment.get("a"); // 获取环境变量
pm.variables.set("a", "c3"); //设置集合变量
pm.globals.set("a", "g3"); // 设置全局变量
pm.environment.set("a", "e3"); //设置环境变量
Note To select the corresponding environment when using environment variables
Variable priority
when variable the same name, priority:
the environment variable> Collection variable> global variable (Globals)
For more information, please add add learning of micro letter: lockingfree get