前言
声明变量和引用是经常用到的,引用变量方式有两种:$var 或 ${var}
通过关键字variables 定义变量主要在config和step中,这两者的区别如下:
1.在 config 中定义的变量为全局变量的,范围比较大,也就是整个测试用例(testcase)的所有地方都可以引用;
2.在 step 中定义的变量为局部变量,范围比较小,局限于当前测试步骤(teststep)
全局变量说明
在config下声明全局变量,作用域在整个.yml文件
config: name: 登录case variables: username: admin password: 123456
通过$var 或 ${var}实现变量引用
config: name: 登录测试平台imos-Case variables: username: admin
password: 123456 teststeps: - name: post request: method: POST url: http://120.25.121.168:19001/api/login json: username: ${username} password: ${password}
validate: - eq: [status_code, 200]
局部变量说明
下面有2个step,第二个step设置了局部变量, 它的作用域只有第二个step中。
config: name: teststep变量引用案例 teststeps: - name: get request: method: GET url: http://120.25.121.168:19001/api/projects?search= validate: - eq: [status_code, 200] - name: post variables: username:admin
password:quxue2023
request:
method:
POST
url:
http://120.25.121.168:19001/api/login
json:
username: ${username}
password: ${password}
validate:
- eq: [status_code, 200]
变量优先级
config 全局变量和 step局部变量名最好不要相同,当出现相同时,step变量优先级大于config变量
config: name: 变量优先级案例 variables: username: admin teststeps: - name: get request: method: GET url: http://120.25.121.168:19001/api/projects?search= validate: - eq: [status_code, 200] - name: post variables: username: root request: method: POST url: http://120.25.121.168:19001/api/login json: username: ${username} password: quxue2023 validate: - eq: [status_code, 200]
结果:${username}
引用的结果是局部变量中的值 root
标签:username,03,name,step,httprunner4,password,config,变量 From: https://www.cnblogs.com/quxue/p/18133165