前言
HttpRunner4.x 支持两种(jmespath 和 正则)提取返回结果的方式,并且内置了丰富的校验结果的方式
assert校验方式
validate 可以支持的校验方式
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结尾 |
yaml 中可以写2种格式校验
httprunner 校验返回结果有2种格式
{"assert_name": [check_value, expect_value]}
{"check": check_value, "assert": assert_name, "expect": expect_value, "message": check status code}
第一种校验方式,也就是前面常用到的
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 # 可选项,当校验失败时的提示语
(备注:第二种校验方式与httprunner3.x版本相比做了一些修改,以前的校验格式是{"check": check_value, "comparator": comparator_name, "expect": expect_value}
现在改成了{"check": check_value, "assert": assert_name, "expect": expect_value, "message": check status code}
总体来说"comparator" 关键字换成了 "assert", "message"关键字无关紧要,没多大作用
)
使用示例
config:
name: 校验结果
teststeps:
-
name: post
variables:
username: test456
request:
method: POST
url: http://httpbin.org/post
json:
username: ${username}
password: "123456"
extract:
url: body.url
origin: body.origin
type: headers."Content-Type"
validate:
- check: status_code
assert: eq
expect: 200
- check: body.url
assert: eq
expect: "http://httpbin.org/postx"
message: 校验body的url值
contains 校验包含
关于 contains 和 contained_by 函数定义
- check_value 是需要校验的返回结果
- expect_value 是期望结果,可以是这几种类型:list, tuple, dict, basestring
使用示例
config:
name: 校验结果
teststeps:
-
name: post
variables:
username: test456
request:
method: POST
url: http://httpbin.org/post
json:
username: ${username}
password: "123456"
extract:
url: body.url
validate:
- eq: [status_code, 200]
- contains: ["${url}", "httpbin.org/post"]
标签:httprunner,断言,校验,value,equals,expect,validate,eq,check
From: https://www.cnblogs.com/yoyoketang/p/17375624.html