当前位置:网站首页>Interface testing framework of actual combat (2) | interface request assertion
Interface testing framework of actual combat (2) | interface request assertion
2022-08-03 04:33:00 【Ye He that hermione】
接口请求断言是指在发起请求之后,对返回的响应内容去做判断,用来查看是否响应内容是否与规定的返回值相符.
接口请求断言
响应内容
在发起请求后,我们使用一个变量 r
存储响应的内容,也就是 Response
对象.
>>> import requests
>>> r = requests.get('http://httpbin.org/get')
>>> print(r)
<Response [200]>
Response 对象有很多功能强大的方法可以调用,比如直接获取响应头,获取 Unicode 编码后的响应内容,获取二进制的响应内容,获取原始的响应内容等等.
获得响应头
>>> r.headers
{'Date': 'Sun, 05 Apr 2020 16:38:09 GMT', \
'Content-Type': 'application/json', \
'Content-Length': '308', 'Connection': 'keep-alive',\
'Server': 'gunicorn/19.9.0', \
'Access-Control-Allow-Origin': '*', \
'Access-Control-Allow-Credentials': 'true'}
获得编码后的响应值:
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"hogwarts": [
"a",
"b",
"c"
]
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "32",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.22.0",
"X-Amzn-Trace-Id": "Root=1-5e8a01e3-0913fca09ac605ccc911ccde"
},
"json": null,
"origin": "113.118.101.232",
"url": "http://httpbin.org/post"
}
还可以使用 r.raw
获得原始响应内容, r.content
获得二进制的响应内容,另外还有编码为 JSON 格式的响应内容,会在后面的章节进行详述.
环境安装
安装 JSON 库:
pip install json
状态码断言
响应状态码断言:
assert
是 Python 的内置函数,用来判断表达式,当表达式条件为 False 的时候就会触发异常.
r.status_code
是 Response 对象内的一个方法,用于获得返回值的状态码.
assert r.status_code==200
就是在判断状态码是否等于200,如果不等于200则会抛出异常.
反例:响应状态码断言,判断响应状态码是否为400
>>> import requests
>>> r = requests.get('http://httpbin.org/get')
>>> assert r.status_code==400
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
从上个例子可以知道,这个响应状态码应该是 200,因为与 400 不相等,所以抛出了异常.
JSON 响应断言
在测试过程中,大部分接口的返回值都为 JSON 格式.所以,掌握如何对 JSON 响应值进行断言这一技能,可以更轻松的完善接口自动化测试用例.
on the response content JSON 编码
r.json()
对于响应值 r 先进行 JSON 编码:
>>> import requests
>>> r = requests.post('http://httpbin.org/post', data = {'hogwarts':["a","b","c"]})
>>> r.json()
{'args': {}, 'data': '', 'files': {}, 'form': {'hogwarts': ['a', 'b', 'c']},\
'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate',\
'Content-Length': '32', 'Content-Type': 'application/x-www-form-urlencoded',\
'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0',\
'X-Amzn-Trace-Id': 'Root=1-5e8a01e3-0913fca09ac605ccc911ccde'},\
'json': None, 'origin': '113.118.101.232', 'url': 'http://httpbin.org/post'}
Multiple types of response value assertion cases
对于字典格式,可以通过 dict["key"]
的方式拿到 value 值.
对于列表格式,可以通过 list[index]
拿到对应索引的 value 值.
在 JSON 的断言之中,主要应用的就是字典和列表自带的查找方法.如果碰到混合或者嵌套的情况,只需要一层一层拨开,直到找到需要进行断言的字段即可.
{'args': {}, 'data': '', 'files': {}, \
'form': {'hogwarts': ['a', 'b', 'c']}, \
'headers': {'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', \
'Content-Length': '32', 'Content-Type': 'application/x-www-form-urlencoded',\
'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.22.0', \
'X-Amzn-Trace-Id': 'Root=1-5e8a01e3-0913fca09ac605ccc911ccde'}, \
'json': None, 'origin': '113.118.101.232', \
'url': 'http://httpbin.org/post'}
字典格式断言,判断 headers 中的 Host 为 httpbin.org
>>> import requests
>>> r = requests.post('http://httpbin.org/post', data = {'hogwarts':["a","b","c"]})
>>> assert r.json()['headers']["Host"] == "httpbin.org"
- 第一层是 key 值为 “header” 的 value
- 第二层是 key 值为 “Host” 的 value
- 判断 key 值为 “Host” 的 value 值是否与 “httpbin.org” 相等
字典混合列表格式断言,判断 hogwarts 对应的列表的第一位是 ‘a’
>>> import requests
>>> r = requests.post('http://httpbin.org/post', data = {'hogwarts':["a","b","c"]})
>>> assert r.json()['form']["hogwarts"][0] == "a"
- 第一层是 key 值为 ‘form’ 的 value
- 第二层是 key 值为 ‘hogwarts’ 的 value
- 第三层是索引为 0 的 value
- 判断上一步索引为 0 的 value 是否等于 “a”
更多内容,我们在后续章节详细解读.
边栏推荐
猜你喜欢
【Harmony OS】【ARK UI】ets使用startAbility或startAbilityForResult方式调起Ability
探索性测试的概念及方法
9.新闻分类:多分类问题
社交电商如何做粉丝运营?云平台怎么选择商业模式?
Dialog manager in the fourth chapter: the dialog message loop
三丁基-巯基膦烷「tBuBrettPhos Pd(allyl)」OTf),1798782-17-8
【生物素叠氮化物|cas:908007-17-0】价格_厂家
软件开发的最大的区别是什么?
OSI的分层特点、传输过程与三次握手、四次挥手、tcp与udp包头的描述
【Harmony OS】【ArkUI】ets开发 基础页面布局与数据连接
随机推荐
13.机器学习基础:数据预处理与特征工程
数据库基本概述与SQL概述
接口测试如何准备测试数据
普乐蛙VR台风体验馆厂家VR防震减灾模拟VR沉浸式体验设备
DOM破环和两个实验的复现
2022河南萌新联赛第(四)场:郑州轻工业大学 G - 迷宫
redis键值出现 xacxedx00x05tx00&的解决方法
js的垃圾回收机制
【翻译】开发与生产中的Kubernetes修复成本对比
接口管理工具YApi怎么用?颜值高、易管理、超好用
Shell编程的条件语句
The flink sql task is changed, and after adding several fields to the sql, an error occurs when restoring from the previously saved savepoint.
索引创建、删除与使用
Windows 安装PostgreSQL
【Harmony OS】【FAQ】鸿蒙问题合集1
MySQL 入门:Case 语句很好用
计网试卷概念
OpenFOAM提取等职面并计算面积
Browser listens for tab closing
ORACLE中文乱码