1.参数化
自动化测试中,肯定涉及到用例测试步骤都基本一致,只是输入参数不一样的用例。这种情况就可以使用参数化方法来解决,避免重复编码。
HttpRunner中参数化使用的是parameters,要区分它跟variables的区别,variables是一个变量,为了不重复定义,可定义一次,下边使用$变量名 的方式使用。parameters是用例所需的输入参数,可以分为多组不同数据。
参数为username,uid,password,下边是两组参数。
parameters:
- username-uid-password: #两组参数
- [test1,1,123]
- [test2,2,125]
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000
parameters:
- username-uid-password: #两组参数
- [test1,1,123]
- [test2,2,125]
# variables:
# username: user1
# password: 123456
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
app_version: 2.8.6
device_sn: FwgRiO7CNA50DSU
os_platform: ios
json:
sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: /api/get-token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
# - eq: [content.token, baNLX1zhFYP11Seb]
extract:
- token: content.token
- test:
name: /api/users/$uid
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: $username
password: $password
method: POST
url: /api/users/$uid
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
如下形式的参数共有4*4=16种组合,所以会进行用例会执行16次
parameters:
- user: ["test1", "test2", "test3", "test4"]
- psw: ["123456", "123456", "123456", "123456"]
如果参数比较多,还可以使用csv格式进行保存,然后在yml或json文件中引用csv文件进行运行,通过内置的 parameterize(可简写为 P)函数引用 CSV 文件
uid.csv
uid
1001
1002
1999
2000
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000
parameters:
- uid: ${parameterize(uid.csv)}
variables:
username: user1
password: 123456
- test:
name: /api/get-token
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
app_version: 2.8.6
device_sn: FwgRiO7CNA50DSU
os_platform: ios
json:
sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
method: POST
url: /api/get-token
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
# - eq: [content.token, baNLX1zhFYP11Seb]
extract:
- token: content.token
- test:
name: /api/users/$uid
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: $token
json:
name: $username
password: $password
method: POST
url: /api/users/$uid
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
2.结果验证
结果验证相当于其他框架断言,httprunner使用validate关键字
validate
支持两种格式:
{"comparator_name": [check_item, expect_value]}比较符号:检测项,期望值
{"check": check_item, "comparator": comparator_name, "expect": expect_value}
- eq equals,判断实际结果和期望结果是否相等,可以用"eq", "equals", "==", "is"
- lt less_than, 判断实际结果小于期望结果 ,可以用 "lt", "less_than"
- le less_than_or_equals,判断实际结果小于等于期望结果 ,可以用 "le", "less_than_or_equals"
- gt greater_than,判断实际结果大于期望结果,可以用"gt", "greater_than"
- ge greater_than_or_equals,判断实际结果大于等于期望结果,可以用"ge", "greater_than_or_equals"
- ne not_equals, 判断实际结果和期望结果不相等,可以用"ne", "not_equals"
- str_eq string_equals 判断转字符串后对比 实际结果和期望结果是否相等,可以用"str_eq", "string_equals"
- len_eq length_equals 判断字符串或list长度,可以用"len_eq", "length_equals", "count_eq"
- len_gt length_greater_than 判断实际结果的长度大于和期望结果,可以用"len_gt", "count_gt", "length_greater_than", "count_greater_than"
- len_ge length_greater_than_or_equals 实际结果的长度大于等于期望结果,可以用"len_ge", "count_ge", "length_greater_than_or_equals", "count_greater_than_or_equals"
- len_lt length_less_than 实际结果的长度小于期望结果,可以用"len_lt", "count_lt", "length_less_than", "count_less_than"
- len_le length_less_than_or_equals 实际结果的长度小于等于期望结果,可以用"len_le", "count_le", "length_less_than_or_equals", "count_less_than_or_equals