python编写接口测试文档(以豆瓣搜索为例)
作者:测试baby 时间:2023-09-21 17:39:49
前言
很多人会使用postman工具,或者熟悉python,但不一定会使用python来编写测试用例脚本,postman里面可以完整的将python代码复制出来。
(以下所有内容以豆瓣网站搜索功能为例子)
一、postman接口用例转换为python测试用例
打开postman,点击右侧的</>图标,页面右边会显示脚本,顶部修改导出的语言,这边我使用的是Python-Reqyests
复制脚本,在PyCharm中打开即可,在导入使用之前如果没有reuqests库,可能会报错,我们需要安装reuqests库。
cmd命令窗口输入:pip install requests
导出后的脚本格式如下:
import requests
url = "<https://www.douban.com/search?">
payload={'q': '三体'}
files=[
]
headers = {
'Cookie': 'bid=5bBvkukAbvY'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
二、转换为pytest测试用例
1.下面就是转成pytest的测试用例
import requests
class TestDouban:
def test_douban(self):
url = "<https://www.douban.com/search?">
payload = {'q': '三体'}
files = []
headers = {
'Cookie': 'bid=5bBvkukAbvY'
}
response = requests.request("POST", url, headers=headers, data=payload, files=files)
print(response.text)
三、封装POST和GET方法
在一个项目中,根路由的路径是一样的,只是不同功能对应的具体的接口不一致,且POST和GET是目前测试用例中比较通用的方法,所以可以将根路由、POST和GET方法封装成一个通用的类,后面直接调用即可。
1.common.py—公共类封装
import requests
class Common:
def __init__(self):
# 豆瓣根路由
self.url_root = "<https://www.douban.com>"
# get请求,uri是接口具体地址,params是get请求的参数,如果没有,默认为空
def get(self, uri, params=''):
# 拼凑访问地址
url = self.url_root + uri + params
# 通过get请求访问对应地址
response = requests.get(url)
# 返回request的response结果,类型为requests的Response类型
return response
# post请求,uri是接口具体地址,params是post请求的参数,如果没有,默认为空
def post(self, uri, params=''):
# 拼凑访问地址
url = self.url_root + uri
# 有参数,则访问对应的url,并赋值给默认参数data
if len(params) > 0:
response = requests.post(url, data=params)
# 无参数,只需要访问对应的url即可
else:
response = requests.post(url)
# 返回request的response结果,类型为requests的Response类型
return response
2.具体接口测试用例
import requests
from common.common import Common
class TestDouban:
def setup(self):
self.com = Common()
def test_douban(self):
uri = "/search?"
payload = {'q': '三体'}
response = self.com.post(uri, payload)
# 由于file不需要,就将file删除了,至于hearder是否要添加可根据需求来定
执行结果如下:
来源:https://blog.csdn.net/m0_56736369/article/details/118523109
标签:python,接口,测试
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
云服务器centos8安装oracle19c的详细教程
2024-01-23 06:36:23
![](https://img.aspxhome.com/file/2023/7/67037_0s.png)
如何在一段文字里点一下就可以在里面插入一段文字?
2024-04-19 09:55:31
Python的Django框架中settings文件的部署建议
2023-07-08 21:39:43
python生成随机图形验证码详解
2023-11-20 22:28:10
选择一个优秀正文字体的15个技巧
2008-03-20 13:36:00
![](https://img.aspxhome.com/file/UploadPic/20083/20/2008320134124747s.jpg)
在Heroku云平台上部署Python的Django框架的教程
2022-02-28 08:46:49
Go中的条件语句Switch示例详解
2024-05-09 10:11:25
pyinstaller封装exe的操作
2021-02-12 03:21:16
![](https://img.aspxhome.com/file/2023/4/97694_0s.jpg)
HTTP头信息总结
2022-10-06 16:09:40
![](https://img.aspxhome.com/file/2023/5/131855_0s.jpg)
解析SQLServer2005的Top功能
2024-01-27 05:37:16
Numpy 中的矩阵求逆实例
2022-11-15 16:51:12
ubuntu系统下切换python版本的方法
2021-07-05 18:12:50
Transpose 数组行列转置的限制方式
2023-11-09 08:30:45
![](https://img.aspxhome.com/file/2023/2/65062_0s.jpg)
block 和 inline 的区别是?
2009-12-08 13:00:00
![](https://img.aspxhome.com/file/UploadPic/200912/8/203330321-54s.gif)
Javascript编写Asp时需要注意的一些地方
2008-04-06 14:20:00
不是原型继承那么简单!prototype的深度探索
2008-03-07 12:42:00
Python人脸识别初探
2023-01-24 09:39:58
![](https://img.aspxhome.com/file/2023/6/91936_0s.jpg)
SQL server 定时自动备份数据库的图文方法
2024-01-14 17:55:50
![](https://img.aspxhome.com/file/2023/5/110985_0s.jpg)
Python sklearn 中的 make_blobs() 函数示例详解
2022-07-24 21:45:48
python实现生成Word、docx文件的方法分析
2021-03-01 07:49:53