前言
- 本篇来学习下Httprunner中断言(validate)的使用
校验相等
validate: - eq: ["status_code", 200]
校验包含
validate: - contains: [body.msg,"success!"]
校验开头或结尾
- startswith 字符串以expect_value开头
- endswith 字符串以expect_value结尾
yaml
validate: - eq: [status_code, 200] - startswith: [body.msg, "login"] - endswith: [body.msg, "success!"]
pytest
.validate() .assert_equal("status_code", 200) .assert_startswith("body.msg", "login") .assert_endswith("body.msg", "success!")
使用正则匹配
- assert_regex_match 方法第一个参数是jmes_path,第二个参数是正则表达式
返回值
{ "code":0, "msg":"login success!", "username":"test1", "token":"1234567890" }
yaml
validate: - assert_regex_match: [body.token, "\d+"]
注意
- 需注意的是正则匹配只能匹配字符串类型,不是字符串类型的可以用 jmespath 函数to_string()转字符串
.validate() .assert_regex_match("to_string(body.code)", "0")
断言总结
- validate 可以支持的校验方式
项目 | 缩写 | 功能 |
电脑 | $1600 | jdjd |
equal | “eq”, “equals”, “equal” | 相等 |
less_than | “lt”, “less_than” | 小于 |
less_or_equals | “le”, “less_or_equals” | `2112小于或等于 |
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结尾 |