httprunner 2.x学习12-在testcase中传递变量(output)
httprunner 2.x学习12-在testcase中传递变量(output)
前言
extract is used to replace output when passing former teststep's (as a testcase) export value to next teststep
export is used to replace output in testcase config
testcase 导入 api
测试用例(testcase) 是测试步骤的按顺序组织的,测试步骤也就是对应 api 里面的每个接口
场景案例:
A接口登录接口文档基本信息
访问地址:http://127.0.0.1:8000/api/v1/login/请求类型:POST 请求头部:application/json 请求参数:{"username":"test", "password":"123456"}
B接口查询商品信息
访问地址:http://127.0.0.1:8000/api/v2/goods?page=1&size=2 请求类型:GET 请求头部token参数: Authorization: Token xxxxx login token xxxxx
于是在 api 层写 2 个 api
login.yml
name: 用户账户登录base_url: http://127.0.0.1:8000variables: user: test1 psw: "123456"request: url: /api/v1/login method: POST json: username: $user password: $pswvalidate: - eq: [status_code, 200] - eq: [headers.Content-Type, application/json]
get_goods.yml
name: 查询全部商品base_url: http://127.0.0.1:8000variables: page: 1 size: 5request: url: /api/v2/goods method: GET headers: Authorization: Token $token params: page: $page size: $sizevalidate: - eq: [status_code, 200]
在 testcases 层引用 api ,步骤1 用extract 提取token变量,在步骤2里面,api里面虽然没定义token,但是可以引用到步骤1 提取的token。
config: name: 登陆后,查询商品详情teststeps:- name: 测试步骤:login api: api/login.yml extract: token: content.token- name: 测试步骤:查询商品详情 variables: page: 1 size: 2 api: api/get_goods.yml validate: - eq: [content.code, 0] - eq: [content.msg, success!]
output 全局变量
如果对上面的参数传递不太理解,可以在config 加上 output 关键字,把步骤 1 提取的token变量设置为全局变量,使它在整个yaml文件的步骤中都会生效
config: name: 登陆后,查询商品详情 output: - tokenteststeps:- name: 测试步骤:login api: api/login.yml extract: token: content.token- name: 测试步骤:查询商品详情 variables: page: 1 size: 2 api: api/get_goods.yml validate: - eq: [content.code, 0] - eq: [content.msg, success!]
运行结果会看到 Output 把变量输出到控制台,这样有个好处,可以方便查看变量的值是否正确,方便调试
INFO ==================== Output ==================== Variable : Value---------------- : -----------------------------token : 85da1cb0448b62d451bb5db1cd214cccd1bc139a------------------------------------------------INFO Start to render Html report ... INFO Generated Html report: D:\hrun2\reports\20210415T041914.869893.html Sentry is attempting to send 0 pending error messages Waiting up to 2 secondsPress Ctrl-Break to quit
output 可以在 testcase 间传递变量值
httprunner 2.x 的版本,testcase 可以引用 api, 同样的,testcase 也可以引用另外一个 testcase 文件,如何2个 testcase 之间需要传递变量,这个时候可以用output把变量暴露出来,给下个步骤引用。
先看一个登陆成功的 testcase, 引用前面的登陆api
config: name: 登陆接口-输入正确账号,正确密码,登陆成功teststeps:- name: 测试步骤:login api: api/login.yml variables: user: test1 psw: "123456" validate: - eq: [content.code, 0] - eq: [content.msg, login success!] - len_eq: [content.token, 40]
有些同学喜欢偷懒,觉得前面的用例是一大串的步骤,下个用例的步骤接着前面的用例,于是可以导入前面的用例。
当然httprunner是支持这样做的,其它的框架并不支持,我个人也不建议这样做,测试用例容易混乱,测试用例设计原则:我们要保证每个用例的独立性,不要来其它用例的结果。
先在 testcases/test_login_success.yml 中提取 token,接着在 config 里 output 变量 token
config: name: 登陆接口-输入正确账号,正确密码,登陆成功 output: - tokenteststeps:- name: 测试步骤:login api: api/login_base_url.yml variables: user: test1 psw: "123456" extract: token: content.token validate: - eq: [content.code, 0] - eq: [content.msg, login success!] - len_eq: [content.token, 40]
于是下个testcase/test_goods_all.yml 可以引用testcase
config: name: 登陆后,查询商品详情teststeps:- name: 测试步骤:引用testcases test_login_success.yml testcase: testcases/test_login_success.yml- name: 测试步骤:查询商品详情 variables: page: 1 size: 2 token: $token api: api/get_goods.yml validate: - eq: [content.code, 0] - eq: [content.msg, success!]
下个 api 步骤需重新引用下 $token 就可以找到了。