variables变量声明与引用
前言
在httprunner中,支持变量声明和引用。在config和test中均可通过variables关键字定义变量,然后在测试步骤中通过$变量名 的方式引用。
局部变量
在登录的案例中,账号和密码是写死的,一般写用例的时候,我们最好把这种可能会变的参数单独写个变量。做到测试数据和代码的分离,以便后续维护。
局部变量只在当前的test下生效
- test:
name: login case
variables:
user: test
psw: 123456
request:
url: http://127.0.0.1:9000/api/v1/login/
method: POST
headers:
Content-Type: application/json
json:
username: $user
password: $psw
extract:
- token: content.token # 提取 token
validate:
- eq: [status_code, 200]
- eq: [content.msg, login success!]
全局变量
设置一个全局变量,需把变量声明(variables)放到 config 下,这样就在整个.yml 文件生效了
- config:
name: test API
variables:
user: test
psw: 123456
- test:
name: login case
request:
url: http://127.0.0.1:9000/api/v1/login/
method: POST
headers:
Content-Type: application/json
json:
username: $user
password: $psw
extract:
- token: content.token # 提取 token
validate:
- eq: [status_code, 200]
- eq: [content.msg, login success!]
注意:如果test和config中variables声明的参数名相同,在运行测试用例的时候会取test里面的参数值