前言
HttpRunner4.x 内置了丰富的校验结果的方式
校验方式
assert | 缩写 | 说明 |
---|---|---|
equal | "eq", "equals", "equal" | 相等 |
less_than | "lt", "less_than" | 小于 |
less_or_equals | "le", "less_or_equals" | 小于或等于 |
greater_than | "gt", "greater_than" | 大于 |
greater_or_equals | "ge", "greater_or_equals" | 大于或等于 |
not_equal | "ne", "not_equal" | 不等于 |
string_equals | "str_eq", "string_equals" | 转字符串相等 |
length_equal | "len_eq", "length_equal" | 长度相等 |
length_greater_than | "len_gt","length_greater_than" | 长度大于 |
length_greater_or_equals | "len_ge","length_greater_or_equals" | 长度大于或等于 |
length_less_than | "len_lt", "length_less_than" | 长度小于 |
length_less_or_equals | ""len_le", "length_less_or_equals" | 长度小于或等于 |
contains | check_value 包含 expect_value | |
contained_by | expect_value 包含check_value | |
type_match | type类型匹配 | |
regex_match | 正则匹配re.match(expect_value, check_value) | |
startswith | 字符串以xx开头 | |
endswith | 字符串以xx结尾 |
2种校验方式:
validate: - eq: [status_code, 200] - eq: [body.url, "http://httpbin.org/post"] - eq: [body.origin, "183.193.27.78"] - eq: [headers."Content-Type", "application/json"]
validate: - check: status_code # 检查点,支持 jmespath 和正则提取 assert: eq # 断言方式 expect: 200 # 期望结果 message: check status code # 可选项,当校验失败时的提示语
案例:
config: name: 案例 teststeps: - name: post variables: username: admin request: method: POST url: http://120.25.121.168:19001/api/login json: username: ${username} password: "quxue2023" validate: - eq: [status_code, 200] - eq: [body.data.username, "admin"] - eq: [body.data.message, "成功"] - eq: [headers."Content-Type", "application/json"]
contains 校验包含
关于 contains 和 contained_by 函数定义
- check_value 是需要校验的返回结果
- expect_value 是期望结果,可以是这几种类型:list, tuple, dict, basestring
config: name: 案例 teststeps: - name: post variables: username: admin request: method: POST url: http://120.25.121.168:19001/api/login json: username: ${username} password: "quxue2023" validate: - eq: [status_code, 200] - eq: [body.data.username, "admin"] - contains: [body.data.username, "2024"] - eq: [body.data.message, "成功"] - eq: [headers."Content-Type", "application/json"]
标签:username,httprunner,05,less,equals,length,validate,eq,than From: https://www.cnblogs.com/quxue/p/18133243