当前位置:网站首页>接口测试框架实战(一) | Requests 与接口请求构造
接口测试框架实战(一) | Requests 与接口请求构造
2022-06-27 00:05:00 【华为云】

Requests 是一个优雅而简单的 Python HTTP 库,其实 Python 内置了用于访问网络的资源模块,比如urllib,但是它远不如 Requests 简单优雅,而且缺少了许多实用功能。所以,更推荐掌握 Requests 接口测试实战技能,这也是互联网大厂流行的接口测试利器。
下面从安装 Requests 库开始,一步步掌握接口请求构造、接口请求断言、Schema 断言、Json/XML 请求、测试用例调试、HeadCookie 处理、Jsonpath 应用、认证体系等接口测试实战技能。
pip 命令安装 Requests。
Requests 官方文档:
https://2.python-requests.org/en/master/
接下来就会使用最流行的 Requests 进行接口测试。
Requests 提供了几乎所有的 HTTP 请求构造方法,以及通过传入参数的方法,对发送的请求进行定制化的配置,可以用来应对各种不同的请求场景。
发送 get 请求:
在请求中添加 data 参数,并发送 post 请求:
在请求中添加 data 参数,并发送 put 请求:
发送 delete 请求:
发送 head 请求:
发送 options 请求:
也可以直接使用 request 函数,传入不同的 method,例如使用这个方法发送 get 请求:
下面的参数都是非必须参数,但是如果需要对请求做额外的定制化,则需要掌握以下这些参数的作用。
header 参数
通过传入 dict 定制请求头:data 参数
发送编码为表单形式的数据单:files 参数
上传文件,dict 格式。
注意:建议用二进制模式(binary mode)打开文件。这是因为 Requests 可能会试图为你提供 Content-Length header,在它这样做的时候,这个值会被设为文件的字节数(bytes)。如果用文本模式(text mode)打开文件,就可能会发生错误。timeout参数
设定超时时间(秒),到达这个时间之后会停止等待响应:
注意:timeout 仅对连接过程有效,与响应体的下载无关。timeout 并不是整个下载响应的时间限制,而是如果服务器在 timeout 秒内没有应答,将会引发一个异常(更精确地说,是在 timeout 秒内没有从基础套接字上接收到任何字节的数据时),如果不设置 timeout,将一直等待。allow_redirects 参数
控制是否启用重定向,bool 类型,选择 True 为启用,选择 False 为禁用。proxies参数
设置代理,dict 格式,key 值为选择的协议,可以分别设置 HTTP 请求和 HTTPS 请求的代理。verify 参数
可以传入 bool 值或者 string,默认为 True。如果设置为 False 的即为忽略对 SSL 证书的验证;反之就是需要做验证;如果传入值为 string 的话,代表指定本地的证书作为客户端证书。
从本地传入证书:
忽略对SSL证书的验证:
另外三个重要参数 Json、Cookies、Auth 在后面的章节再进行详细的介绍。
边栏推荐
- 目前哪个证券公司炒股开户是最好最安全的?
- 手机能开户炒股吗 网上开户炒股安全吗
- com.fasterxml.jackson.databind.exc.MismatchedInputException: Expected array or string. at [Source:x
- How do new investors open accounts online? Is it safe to open accounts online and speculate in stocks
- 万字详解-MindArmour 小白教程!
- Batch generate folders based on file names
- [vscade] preview MD file
- Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
- 技术干货|极速、极智、极简的昇思MindSpore Lite:助力华为Watch更加智能
- [微服務]認識微服務
猜你喜欢

Big guys talk about the experience sharing of the operation of the cutting-edge mindspore open source community. Come up with a small notebook!

kubernetes可视化界面dashboard

Thesis study -- Analysis of the influence of rainfall field division method on rainfall control rate

Safe and cost-effective payment in Thailand

Oracle 數據庫基本知識概念

安利!如何提优质的ISSUE?学霸是这样写的!

Installation of xshell and xftp

05 | 规范设计(下):commit 信息风格迥异、难以阅读,如何规范?

Technical dry goods | top speed, top intelligence and minimalist mindspore Lite: help Huawei watch become more intelligent

Memorizing byte order of big and small end
随机推荐
Introduction to software engineering -- Chapter 4 -- formal description technology
The most complete hybrid precision training principle in the whole network
中信证券佣金 网上开户炒股安全吗
[test] the content of the hottest test development learning route has been updated again to help pass the customs and open the test of large factories
JS library for number formatting
Why does EDR need defense in depth to combat ransomware?
新型冠状病毒变异Delta毒株的模拟(MindSPONGE应用)
Your connection is not private
利用burp精准定位攻击者
MATLAB data type - character type
2022 Health Expo, Shandong health care exhibition, postpartum health and sleep health exhibition
互联网行业,常见含金量高的证书,看看你有几个?
Com. Faster XML. Jackson. DataBind. Exc.mismatchedinputexception: tableau ou chaîne attendu. At [Source: X
MindSpore新型轻量级神经网络GhostNet,在ImageNet分类、图像识别和目标检测等多个应用场景效果优异!
Great health industry annual must attend event, 2022 Shandong International Great Health Industry Expo
这3个并发编程的核心,竟然还有人不知道?
Moher College -x-forwarded-for injection vulnerability practice
The [MySQL] time field is set to the current time by default
Let agile return to its original source -- Some Thoughts on reading the way of agile neatness
Oracle 數據庫基本知識概念