httprunner踩坑
1、get接口参数使用params传入,例如:
name: get_stroy_content
base_url: http://xxx.xxx.xx.xx:xxxx
variables:
expected_status_code: 200
expected_msg: "request successfully"
request:
url: /favorite/getStoryContent
method: GET
params:
id: 15634
startDate: "2018-01-01"
endDate: "2019-09-01"
keyword: ""
start: 1
step: 10
headers:
Content-Type: "application/json"
validate:
- eq: ["status_code", $expected_status_code]
- eq: ["content.msg",$expected_msg]
2、post参数使用json传入,例如:
name: update_favorite
base_url: http://xxx.xxx.xx.xx:xxxx
variables:
expected_status_code: 200
expected_msg: "request successfully"
request:
url: /favorite/updateFavorite
method: POST
json:
id: 15633
name: "看见"
headers:
Content-Type: "application/json"
validate:
- eq: ["status_code", $expected_status_code]
- eq: ["content.msg",$expected_msg]
3、运行api文件下所有的接口描述
- 运行单个yml文件 hrun + test1.yml
- 运行是所有yml文件 hrun + api(yml文件的上层目录,这是相对路径)
- 运行testcases下单个yml文件 hrun + testcases\testcase2.yml
- 运行testcases下所有的yml文件 hrun + testcases (yml文件的上层目录,这是相对路径)
4、testcase中前后关联的接口用例,参数的调用(后一个接口需要前一个接口的返回值作为参数)
- 实例testcase_save_favorite.yml:
config:
name: "save/update favorite testcase"
variables:
expected_stauts_code: 200
base_url: http://xxx.xxx.xx.xx:xxxx
verify: False
output:
- favoriteId
teststeps:
-
name: check add favorite(*保存收藏夹)
api: api/add_save_favorite.yml
extract:
- favoriteId: content.data.favoriteId
-
name: remove favorite(清除保存的收藏夹)
api: api/remove_favorite.yml
variables:
id: ${switch_int($favoriteId)}
- api–> remove_favorite.yml
name: remove_favorite
base_url: http://xxx.xxx.xx.xx:xxxx
variables:
expected_status_code: 200
expected_msg: "request successfully.删除成功"
id: 15798
request:
url: /favorite/removeFavorite
method: POST
json:
id: $id
headers:
Content-Type: "application/json"
validate:
- eq: ["status_code", $expected_status_code]
- eq: ["content.msg",$expected_msg]
注意:extract用于提取接口返回值中的参数,variables用于替换api中的参数(其中api中的json或params的参数需要用variables声明在config中才能被替换),testcase_save_favorite.yml文件variables的id(key)与remove_favorite.yml文件variables的id(key)需要一致。
5、对于api(接口定义)、testcase、testsuites的理解
- api(接口定义)单个用例httprunner的最下单元,可以包含一个接口的url、params、method等,可以单独运行
- testcase 多个用例组合、复杂场景、需要steup_hooks or tesrdown_hooks的用例,可以引用api进行拼装组合
- testsuites 用例集合概念(方便大量用例执行),可以引用testcase,参数化概念(2.0后仅能通过testsuites进行参数化)
- 注意:teststep仅是testcase中的步骤而已@TOC
另外httprunner的csv方式参数化貌似有问题,找不到参数 。