HttpRunner 2.0 参数化数据驱动案例,废话不说,直接上干货。
1、测试用例目录结构
api:接口集
testcases:测试用例
testsuites:测试套件
data:测试数据
2、api 接口集
create1.yaml:
name: 创建外方 variables: - customerName: 姓名 - customerGender: male - customerBirth: 1998-09-09T00:00:00+0800 - resName: 姓名 request: method: POST url: $url/out_visits headers: Accept: application/json;charset=UTF-8 Content-Type: application/json json: customerName: $customerName customerGender: $customerGender customerBirth: $customerBirth resName: $resName
params: extract: - visitId: content.responseBody.id validate: - eq: - status_code - 200 - eq: - content.status - $status - eq: - content.responseBody.resName - $resName - eq: - content.responseBody.customerBirth - $customerBirth
为了演示执行2个接口,在创建一个create2.yaml文件,代码与create1.yaml内容一样
3、testcases 测试用例
testcases_1.yaml:
- config: name: "创建外方1" id: 创建外方1 - test: name: 创建外方1_$msg api: api/create1.yaml
testcases_2.yaml:
- config: name: "创建外方2" id: 创建外方2 - test: name: 创建外方2_$msg api: api/create2.yaml
4、testsuites 测试套件
testsuites_1.yaml:
config: name: "单接口测试testsuites" testcases: create1: testcase: testcases/testcases_1.yaml parameters: resName-status-customerBirth-msg: ${P(data/account1.csv)} create2: testcase: testcases/testcases_2.yaml parameters: resName-status-customerBirth-msg: ${P(data/account2.csv)}
5、data 测试数据
account1.csv:
resName,customerGender,status,customerBirth,msg xingming,male,SUCCESS,1998-09-09T00:00:00+0800,create1_resName为字母 姓名,male,SUCCESS,1998-09-09T00:00:00+0800,create1_resName为汉字 姓名3,male,ERROR,1998-09-08T00:00:200+0800,create1_customerBirth日期格式不正确失败
account2.csv:
resName,customerGender,status,customerBirth,msg xingming,male,SUCCESS,1998-09-09T00:00:00+0800,create2_resName为字母 姓名,male,SUCCESS,1998-09-09T00:00:00+0800,create2_resName为汉字 姓名,male,ERROR,1998-09-08T00:00:200+0800,create2_customerBirth日期格式不正确失败
6、运行
# 执行用例 httprunner --dot-env-path dev.env testsuites/testsuite_1.yaml
参考:https://cn.httprunner.org/advanced/parameters/