httprunner 环境变量Debugtalk辅助函数set/tear down_hooks使用参数化
Posted clownalin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httprunner 环境变量Debugtalk辅助函数set/tear down_hooks使用参数化相关的知识,希望对你有一定的参考价值。
环境变量设置
1.项目中添加.env的文件
2.在项目中使用${.env(变量名)}进行使用
- config: name: "phpwind论坛的首页模拟" base_url: ${ENV(URL)} requests: headers: "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" "Accept-Encoding": "gzip, deflate" "Accept-Language": "zh-CN,zh;q=0.9" "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36" export: - code - info - contenttype - content - test: name: "phpwind论坛的首页模拟" request: url: "/phpwind/" method: GET extract: - code: status_code - info: reason - contenttype: headers.Content-Type - content: ‘title="(.+?)">biaoti826</a>‘ validate: - eq: ["status_code",200] - str_eq: [$content,‘biaoti826‘]
Debugtalk辅助函数
1.在debugtalk.py中编写的方法可以在httprunner中的yml文件中使用(${方法名})
import requests def get_token_id(): get_param_data ={‘grant_type‘: ‘client_credential‘, ‘appid‘: ‘wxec83eaada223a9c8‘, ‘secret‘: ‘1867d7f1cabb3bafae0b7304e8251a09‘} response = requests.get(url=‘https://api.weixin.qq.com/cgi-bin/token‘, params=get_param_data) return response.json()[‘access_token‘] if __name__ == ‘__main__‘: print(get_token_id())
# debugtalk使用 - config: name: "获取token -- 用户管理~查看粉丝基本信息操作" base_url: "https://api.weixin.qq.com" variables: - tokenid: ${get_token_id()} - test: name: "查看粉丝基本信息" request: url: "/cgi-bin/user/info" method: GET params: access_token: $tokenid openid: "od-53v0GMqGTEiPY-QC549RTXkCk" lang: zh_CN validate: - eq: [content.language,zh_CN]
set/tear down_hooks使用
1.既可以写在config中,也可以写在test中
- config: name: "获取token -- 用户管理~查看粉丝基本信息操作" base_url: "https://api.weixin.qq.com" variables: - tokenid: ${get_token_id()} setup_hooks: - ${setup_case()} teardown_hooks: - ${teardown_case()} - test: name: "查看粉丝基本信息" request: url: "/cgi-bin/user/info" method: GET params: access_token: $tokenid openid: "od-53v0GMqGTEiPY-QC549RTXkCk" lang: zh_CN setup_hooks: - ${setup_step()} teardown_hooks: - ${teardown_step()} validate: - eq: [content.language,zh_CN]
参数化使用
方法一:
1.新建test_suites文件夹
2.新建yaml文件如下
config: name: "test suite" testcases: - name: "Create tags" testcase: testcaseshttprunnerhomework02create_tag.yml parameters: tag_name: [‘test11‘,‘test12‘,‘test13‘,‘test14‘,‘test15‘,‘test16‘,‘test17‘,‘test18‘,‘test19‘,‘test20‘]
3.使用 hrun test_suitesyml文件执行
以上是关于httprunner 环境变量Debugtalk辅助函数set/tear down_hooks使用参数化的主要内容,如果未能解决你的问题,请参考以下文章
httprunner使用02参数化.env环境变量调用debugtalk.py文件的函数关联validate 断言
httprunner 环境变量Debugtalk辅助函数set/tear down_hooks使用参数化
httprunner学习11-辅助函数debugtalk.py
httprunner 2.x学习20 - HttpRunner().run()连续执行2个不同项目,只会解析到第一个项目的debugtalk.py