Foreword
In HttpRunner, if you declare a variable, you can key variables to complete, to refer to variable declarations, it is through $ + variable names to achieve (such as $ token). variables can config and test use.
testing scenarios
We will continue to learn before the login interface, before the use case is this:
- config:
name: extract test
request:
base_url: http://api.nnzhp.cn
- test:
name: login case
request:
url: /api/user/login
method: POST
headers:
Content-Type: application/x-www-form-urlencoded
User-Agent: Fiddler
data:
username: test1010
passwd: aA123456
validate:
- eq: [status_code, 200]
- eq: [content.error_code, 0]
In the above use case, our username and passwd is in data write dead, but in the actual test, our data may change frequently, so we'd better put a variable passed to the username and passwd , so Example of use may facilitate maintenance.
config used variables
Variables declared in config, corresponding to a global variable defined, the scope for the entire use case, may be used in the respective test.
- config:
name: variables test
request:
base_url: http://api.nnzhp.cn
variables:
username: test1010
passwd: aA123456
We declare variables in the config username and passwd , variable names can be freely defined and referenced in the test.
Note: When referenced, $ after the variable name, variable name must be consistent with the declaration.
data:
username: $username
passwd: $passwd
Variables defined in the config, you can apply to the current entire YAML script, for example, can be used in multiple test:
- test:
省略部分内容
data:
username: $username
passwd: $passwd
- test:
省略部分内容
data:
username: $username
passwd: $passwd
test used variables
Statement in test variables, local variables corresponding to defined the scope of the current test step, i.e. act only on a test. test methods and declared reference variable, and the config is as follows:
- test:
name: login case
variables:
username: test1010
passwd: aA123456
request:
省略部分内容
data:
username: $username
passwd: $passwd
Obviously, the use of variables in config and test is different, if only in a defined variable test, so there are multiple test, we need to write:
- test:
name: login case
variables:
username: test1010
passwd: aA123456
request:
省略部分内容
data:
username: $username
passwd: $passwd
- test:
name: login case
variables:
username: test1010
passwd: aA123456
request:
省略部分内容
data:
username: $username
passwd: $passwd
config and test use both variables
Sometimes, we may want to define both global and local variables, you can use variables in the config and test. If the config and test defined in the same variable name, it will give priority to use local variables test test procedures defined in the current test execution.
Complete with YAML format is as follows:
- config:
name: variables test
request:
base_url: http://api.nnzhp.cn
variables:
username: test101011111
passwd: aA123456
- test:
name: login case
variables:
username: test1010
passwd: aA123456
request:
url: /api/user/login
method: POST
headers:
Content-Type: application/x-www-form-urlencoded
User-Agent: Fiddler
data:
username: $username
passwd: $passwd
validate:
- eq: [status_code, 200]
- eq: [content.error_code, 0]
Above this use case, the user name and password in config is wrong, the user name and password test is correct. The local variables used when performing Example, when used in the definition of the test, so it is able to log successful.