httprunner 跳过用例、录制生成用例、用例分层机制
跳过用例
使用场景:
1.依赖测试,如微信公众号平台中接口,需要token值才能执行,当未获取到时就不执行;
2.开发在修改接口问题时,即可跳过该用例
3.未完成的测试用例编写,需要执行时,也可使用;
相应方法:
skip:无条件跳过用例
skipIf:条件为真时跳过测试用例,如true,0,非空
skipUnless:条件为假时跳过测试用例,如false,0,空
实践:
- config: name: "获取token -- 用户管理~查看粉丝基本信息操作" base_url: "https://api.weixin.qq.com" - test: name: "获取token_value" request: url: "/cgi-bin/token" method: GET params: grant_type: "client_credential" appid: "wxec83eaada223a9c8" secret: "1867d7f1cabb3bafae0b7304e8251a09" extract: - tokenid: content.access_token validate: - eq: ["status_code",200] - eq: [content.expires_in, 7200] - test: name: "查看粉丝基本信息" skipUnless: $tokenid #上面的tokenid截取到了,这里就会执行,未截取到将不执行 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]
录制生成用例
操作步骤:
1、使用fiddler抓包,然后Flie->Export Sessions->Selected session(选择HTTP Archive v1.1类型)
2、在pycharm终端命令,cd到所在目录
3、执行命令:har2case xx.har xx.yml
用例分层机制
1.api(底层API编写)
name: "get access token" base_url: ${ENV(URL)} request: url: "/cgi-bin/token" method: "GET" params: grant_type: "client_credential" appid: "wxec83eaada223a9c8" secret: "1867d7f1cabb3bafae0b7304e8251a09" validate: - eq: ["status_code",200]
2.testcases(中间用例层)
- config: name: "验证能否正确获取token" - test: name: "step01: 执行get_access_token接口" api: case_hierarchy\api\get_access_token.yml validate: - eq: [content.expires_in,7200]
3.testsuites(测试套件层)
config: name: "测试套件" testcases: - name: "hrun testcase_get_access_token" testcase: case_hierarchy\testcases\testcase_get_access_token.yml
备注:第二层测试用例层除了可以调用接口层的api层接口定义yml文件外,还可以调用其它测试用例执行
简单示例:
-- 删除标签接口 name: "delete tag api" base_url: ${ENV(URL)} request: url: "/cgi-bin/tags/delete" method: "POST" headers: Content_Type: "application/json" params: access_token: $tokenid json: { "tag":{ "id" : $tagid } } validate: - eq: ["status_code",200] -- 获取token接口 name: "get access token" base_url: ${ENV(URL)} request: url: "/cgi-bin/token" method: "GET" params: grant_type: "client_credential" appid: "wxec83eaada223a9c8" secret: "1867d7f1cabb3bafae0b7304e8251a09" validate: - eq: ["status_code",200] -- 执行获取token接口 - config: name: "验证能否正确获取token" export: - tokenid - test: name: "step01: 执行get_access_token接口" api: "case_hierarchy\\api\\get_access_token.yml" extract: - tokenid: content.access_token validate: - eq: [content.expires_in,7200] -- 执行删除标签接口 - config: name: "删除标签" base_url: ${ENV(URL)} export: - tokenid - test: name: "step01: 执行get_access_token接口" testcase: "case_hierarchy\\testcases\\testcase_get_access_token.yml" - test: name: "step02: 删除标签" api: "case_hierarchy\\api\\delete_tag.yml" variables: tokenid: $tokenid tagid: 120 validate: - eq: [content.errmsg, 'ok'] - eq: [content.errcode,0]