前言
httprunner 3.x使用 validate 方法断言
comparator 校验方式
validate 可以支持的校验方式
comparator | 缩写 | 功能 |
---|---|---|
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结尾 |
yaml 中可以写2种格式校验
httprunner 校验返回结果有2种格式
- {"comparator_name": [check_value, expect_value]}
- {"check": check_value, "comparator": comparator_name, "expect": expect_value}
我们平常用的比较多是是上面这种,直接拿到返回结果,校验与期望结果是否一致
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
validate:
- eq: [status_code, 200]
- less_than: [status_code, 400]
- equals: [body.code, 0]
- equals: [body.msg, success!]
- length_equals: [body.datas, 2]
- greater_than_or_equals: [body.datas.0.age, 18]
pytest脚本中的断言
先调用.validate()后,可以看到全部assert方法,在StepRequestValidation 类中定义
方法 | 对应校验方法 | 功能 |
---|---|---|
assert_equal | equal | 相等 |
assert_not_equal | not_equal | 不等于 |
assert_less_than | less_than | 小于 |
assert_less_or_equals | less_or_equals | 小于或等于 |
assert_greater_than | greater_than | 大于 |
assert_greater_or_equals | greater_or_equals | 大于或等于 |
assert_assert_string_equals | string_equals | 转字符串相等 |
assert_length_equal | length_equal | 长度相等 |
assert_length_greater_than | length_greater_than | 长度大于 |
assert_length_greater_or_equals | length_greater_or_equals | 长度大于或等于 |
assert_length_less_than | length_less_than | 长度小于 |
assert_length_less_or_equals | length_less_or_equals | 长度小于或等于 |
assert_contains | contains | check_value 包含 expect_value |
assert_contained_by | contained_by | expect_value 包含check_value |
assert_type_match | type_match | type类型匹配 |
assert_regex_match | regex_match | 正则匹配re.match(expect_value, check_value) |
assert_startswith | startswith | 字符串以xx开头 |
assert_endswith | endswith | 字符串以xx结尾 |
使用方式先调用 .validate()
方法,才能调用 assert 断言方法