responses-validator 专用于对 reqeuests 的响应对象进行断言,
同时为了适用于 yaml 的场景,支持了多种灵活、可扩展的写法
本文详细叙述了响应头和正文所支持的 4 种断言写法:
-
断言部分字段
-
断言包含匹配
-
断言相等匹配
-
断言正则匹配
-
断言函数匹配
详细示例见下文
1. 响应头断言的基本用法
首先,对响应头和响应正文的断言均是可选的。
其次,响应头中字段较多,可只对部分字段进行断言
假设某响应头原文如下
HTTP/1.1 200 OKCache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Encoding: gzipContent-Type: text/htmlDate: Mon, 12 Apr 2022 13:10:34 GMTLast-Modified: Mon, 23 Jan 2017 13:24:17 GMTPragma: no-cacheServer: bfe/1.0.8.18Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/Transfer-Encoding: chunked
在编写断言时,可仅对其中 2 个字段进行断言
Connection: keep-aliveContent-Type: text/html
具体示例:
# tests/test_headers_fields.yamltest_name: 断言响应头部分字段steps:- request:method: geturl: https://www.baidu.com- response: # 对部分稳定字段进行断言,测试通过status_code: 200headers:Connection: keep-aliveContent-Type: text/html- response: # 对全部字段进行断言,测试失败status_code: 200headers:Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transformConnection: keep-aliveContent-Encoding: gzipContent-Type: text/htmlDate: Mon, 12 Apr 2022 13:10:34 GMTLast-Modified: Mon, 23 Jan 2017 13:24:17 GMTPragma: no-cacheServer: bfe/1.0.8.18Set-Cookie: BDORZ=27315; max-age=86400; domain=.baidu.com; path=/Transfer-Encoding: chunked
执行结果如下:

图 1. 仅对部分字段进行断言
2. 断言包含匹配
有时,响应头中包含动态内容(例如日期)
在断言时,希望忽略部分内容,而对另一部分进行判断
在 responses-validator 可以使用【glob】模式,并利用通配符代指动态内容
| 符号 | 含义 |
|---|---|
| * | 任意数量的任意字符 |
| ? | 1 个任意字符串 |
| [xyz] | 1 个括号中的字符 |
| [!abc] | 1 个括号外的字符 |
具体示例如下:
# tests/test_headers_glob.yamltest_name: 断言响应头包含匹配steps:- request:method: geturl: https://www.baidu.com- response:status_code: 200headers:Connection: # 完整格式value: '*alive*' # 匹配表达式mode: 'glob' # 匹配模式msg: '没有包含字符串alive' # 断言描述,失败时显示,非必填- response:status_code: 200headers:Connection: '*alive*' # 默认使用glob模式,可在断言中加入*
执行结果如下:

图 2. 对字段进行包含匹配
3. 断言相等匹配
在【glob】模式(默认)下,*、? 等字符串有特殊含义,
如果我们要断言的内容恰好是 * 等特殊符号呢?
在 responses-validator 可以使用【same】模式,
此模式下将进行最朴素的【相等断言】,排除了符号、语法等干扰
具体示例:
# tests/test_headers_same.yamltest_name: 断言响应头相等匹配steps:- request:method: geturl: https://www.baidu.com- response:status_code: 200headers:Connection: # 断言成功value: 'keep-alive' # 和响应内容完全一样mode: 'same' # 指定匹配模式msg: '响应头不包含keep-alive' # 失败提示,非必填- response:status_code: 200headers:Connection: # 断言失败value: '*alive*' # 响应内容不一样,使用了通配符mode: 'same' # 指定匹配模式msg: '响应头不包含*alive*' # 失败提示,非必填
执行结果如下:

图 3. 对字段进行相同匹配通过

图 4. 对字段进行相同匹配失败
4. 正文断言的基本用法
如果觉得 glob 模式太过简单,无法较好的描述复杂断言
在 responses-validator 可以使用【re】模式,通过正则表达式来进行更复杂的匹配
使用【re】模式时,
mode 的写入 re
value 的写入具体的表达式
msg 为选填,可以写入失败提示
具体示例:
# tests/test_headers_re.yamltest_name: 断言响应头正则匹配steps:- request:method: geturl: https://www.baidu.com- response:status_code: 200headers:Connection: # 断言成功value: '(.*)-alive' # 使用正则匹配任意字符mode: 're' # 指定模式- response:status_code: 200headers:Connection: # 断言失败value: '(?)alive' # 使用正则表示单个字符串mode: 're' # 指定模式
执行结果如下:

图 5. 对字段进行正则匹配通过

图 6. 对字段进行正则匹配失败
5. 正文断言的基本用法
正向的断言和响应头断言非常相似。
在写法上,断言 response 中的 text,犹如断言 headers 中的 Connection
具体示例:
# tests/test_text_basic.yamltest_name: 使用glob模式断言响应正文steps:- request:method: geturl: https://www.baidu.com- response:status_code: 200text: "*baidu*" # 和响应头断言一样,默认使用glob模式- response:status_code: 200text:value: "abcd"mode: 'same' # 指定匹配模式msg: '响应内容不是abcd' # 失败提示,
执行结果如下:

图 7. 对正文使用包含匹配通过

图 8. 对正文使用相等匹配失败
对于文本(text)格式的响应正文,可以使用如请求头一样的包含、相等、正则等匹配方式,
对于 JSON 格式的响应正文,下一篇介绍了可以有更加便捷、灵活、强大的断言方式
原创不易,喜欢请星标+点赞+在看,关注公众号《测试开发研习社》,不错过技术干货,谢谢鼓励!

873

被折叠的 条评论
为什么被折叠?



