forked from DxvLwRYF/apiAutoTest
接口录制功能
This commit is contained in:
parent
35023075e2
commit
25c2d83567
17
README.md
17
README.md
|
@ -13,6 +13,7 @@
|
|||
- 动态多断言: 可(多个)动态提取实际预期结果与指定的预期结果进行比较断言操作
|
||||
- 自定义扩展方法: 在用例中使用自定义方法(如:获取当前时间戳...)的返回值
|
||||
- 邮件发送:将allure报告压缩后已附件形式发送
|
||||
- 接口录制:录制指定包含url的接口,生成用例数据
|
||||
## 依赖库
|
||||
```
|
||||
allure-pytest==2.8.17 # allure报告
|
||||
|
@ -26,6 +27,8 @@ yagmail==0.11.224 # 发送邮件
|
|||
PyMySQL==0.10.1 # 连接mysql数据库
|
||||
pytest-rerunfailures==9.1.1 # 用例失败重跑
|
||||
paramiko==2.7.2 # SSH2 连接
|
||||
xlwt==1.3.0 # 写excel 用例文件
|
||||
mitmproxy==6.0.2 # 抓包工具
|
||||
```
|
||||
## 目录结构
|
||||
```shell
|
||||
|
@ -52,23 +55,12 @@ paramiko==2.7.2 # SSH2 连接
|
|||
│ ├─db.py # 数据库连接对象
|
||||
│ ├─hooks.py # 自定义扩展方法(可用于用例)文件
|
||||
│ ├─read_file.py # 用例、配置项读取
|
||||
│ ├─recording.py # 接口录制,写入用例文件
|
||||
│ └─send_email.py # 邮件发送、报告压缩
|
||||
├─项目实战接口文档.md # 配套项目相关接口文档
|
||||
├─requirements.txt # 项目依赖库文件
|
||||
└─run.py # 主启动文件
|
||||
```
|
||||
|
||||
## 安装教程
|
||||
|
||||
1. git clone https://gitee.com/zy7y/apiAutoTest.git / https://github.com/zy7y/apiAutoTest.git
|
||||
2. 安装Java与allure,https://www.cnblogs.com/zy7y/p/13403699.html
|
||||
3. 使用pycharm打开项目使用Terminal 输入 python3 -m venv venv 新建虚拟环境 (可选)
|
||||
4. 执行pip install -r requirements.txt 安装依赖库(若下载超时:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt)
|
||||
5. 修改config.ymal文件中email文件配置邮箱,request_header配置初始请求头,database 配置数据库信息
|
||||
6. 运行 `run.py`文件
|
||||
## 用例说明文档
|
||||
![case_data.xlsx用例说明文档](https://gitee.com/zy7y/blog_images/raw/master/img/用例说明文档.png)
|
||||
|
||||
## 使用说明
|
||||
|
||||
1. 本项目直接使用的requests.Session理论上实现了cookie请求的管理,不用单独提取cookie,支持前后端分离项目,兼容Restful接口规范。
|
||||
|
@ -126,6 +118,7 @@ https://www.bilibili.com/video/BV1EE411B7SU?p=10
|
|||
|
||||
2021/05/19 统一使用`${}`来包裹变量名/方法 替代先前版本的`&&`,`@@`,新增提取参数栏,意为从当前接口响应中提取指定参数
|
||||
2021/05/19 移除保存响应,增加提取参数栏{参数名: jsonpath} jsonpath为当前用例响应结果中提取并把结果给参数名,其他用中`${参数名}`使用,`${方法名()}`,`${方法名(参数1)}`
|
||||
2021/05/22 新增接口录制功能,可生成用例文件, 具体操作见[apiAutoTest在线帮助文档](http://49.232.203.244:21519/), [视频演示](https://www.bilibili.com/video/BV1W64y1y7Lw)
|
||||
## 博客园首发
|
||||
https://www.cnblogs.com/zy7y/p/13426816.html
|
||||
|
||||
|
|
|
@ -8,4 +8,6 @@ xlrd==1.2.0
|
|||
yagmail==0.11.224
|
||||
PyMySQL==0.10.1
|
||||
pytest-rerunfailures==9.1.1
|
||||
paramiko==2.7.2
|
||||
paramiko==2.7.2
|
||||
xlwt==1.3.0
|
||||
mitmproxy==6.0.2
|
|
@ -0,0 +1,152 @@
|
|||
#!/usr/bin/env/ python3
|
||||
# -*- coding:utf-8 -*-
|
||||
"""
|
||||
@Project: apiAutoTest
|
||||
@File :recording.py
|
||||
@Author:zy7y
|
||||
@Date :2021/5/21 22:07
|
||||
@Desc : 录制接口,生成用例文件
|
||||
基于mitmproxy实现
|
||||
参考资料:
|
||||
https://blog.wolfogre.com/posts/usage-of-mitmproxy/
|
||||
https://www.cnblogs.com/liuwanqiu/p/10697373.html
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
import mitmproxy.http
|
||||
import xlwt
|
||||
|
||||
# 上传文件接口不能录入文件参数 , excel单元格限制: Exception: String longer than 32767 characters
|
||||
from mitmproxy import ctx
|
||||
|
||||
|
||||
class Counter:
|
||||
def __init__(self, filter_url: str, filename: str = "data/case_data1.xls"):
|
||||
"""
|
||||
基于mitmproxy抓包生成用例数据
|
||||
:param filter_url: 需要过滤的url
|
||||
:param filename: 生成用例文件路径
|
||||
"""
|
||||
self.url = filter_url
|
||||
self.excel_row = [
|
||||
'编号',
|
||||
'用例标题',
|
||||
'请求头',
|
||||
'接口地址',
|
||||
'是否执行',
|
||||
'请求方式',
|
||||
'入参关键字',
|
||||
'上传文件',
|
||||
'请求数据',
|
||||
'提取参数',
|
||||
'后置sql',
|
||||
'预期结果']
|
||||
self.cases = [self.excel_row]
|
||||
self.counter = 1
|
||||
self.file = filename
|
||||
|
||||
def response(self, flow: mitmproxy.http.HTTPFlow):
|
||||
"""
|
||||
mitmproxy抓包处理响应,在这里汇总需要数据
|
||||
:param flow:
|
||||
:return:
|
||||
"""
|
||||
if self.url in flow.request.url:
|
||||
# 编号
|
||||
number = "C" + str(self.counter)
|
||||
# 标题
|
||||
title = "mitmproxy录制接口" + str(self.counter)
|
||||
try:
|
||||
token = flow.request.headers["Authorization"]
|
||||
except KeyError:
|
||||
token = ''
|
||||
header = json.dumps({"Authorization": token})
|
||||
data = flow.request.text
|
||||
# 请求地址,config.yaml 里面基准环境地址 写 空字符串
|
||||
method = flow.request.method.lower()
|
||||
url = flow.request.url
|
||||
try:
|
||||
content_type = flow.request.headers['Content-Type']
|
||||
except KeyError:
|
||||
content_type = ''
|
||||
if 'form' in content_type:
|
||||
data_type = "data"
|
||||
elif 'json' in content_type:
|
||||
data_type = 'json'
|
||||
else:
|
||||
data_type = 'params'
|
||||
if '?' in url:
|
||||
data = url.split('?')[1]
|
||||
data = self.handle_form(data)
|
||||
# 预期结果
|
||||
expect = json.dumps(
|
||||
{".": json.loads(flow.response.text)}, ensure_ascii=False)
|
||||
|
||||
# 日志
|
||||
ctx.log.info(url)
|
||||
ctx.log.info(header)
|
||||
ctx.log.info(content_type)
|
||||
ctx.log.info(method)
|
||||
ctx.log.info(data)
|
||||
ctx.log.info(flow.response.text)
|
||||
case = [
|
||||
number,
|
||||
title,
|
||||
header,
|
||||
url.split('?')[0],
|
||||
"是",
|
||||
method,
|
||||
data_type,
|
||||
"",
|
||||
data,
|
||||
"",
|
||||
"",
|
||||
expect]
|
||||
self.cases.append(case)
|
||||
self.counter += 1
|
||||
# 文件末尾追加
|
||||
self.excel_cases()
|
||||
|
||||
def excel_cases(self):
|
||||
"""
|
||||
对二维列表cases进行循环并将内容写入单元格中
|
||||
:return:
|
||||
"""
|
||||
workbook = xlwt.Workbook()
|
||||
worksheet = workbook.add_sheet('用例数据')
|
||||
for x in range(len(self.cases)):
|
||||
for y in range(len(self.cases[x])):
|
||||
worksheet.write(x, y, self.cases[x][y])
|
||||
try:
|
||||
workbook.save(self.file)
|
||||
except Exception as e:
|
||||
print(e)
|
||||
|
||||
def handle_form(self, data: str):
|
||||
"""
|
||||
处理 Content-Type: application/x-www-form-urlencoded
|
||||
默认生成的数据 username=admin&password=123456
|
||||
:param data: 获取的data 类似这样 username=admin&password=123456
|
||||
:return:
|
||||
"""
|
||||
data_dict = {}
|
||||
if data.startswith('{') and data.endswith('}'):
|
||||
return data
|
||||
try:
|
||||
for i in data.split('&'):
|
||||
data_dict[i.split('=')[0]] = i.split('=')[1]
|
||||
return json.dumps(data_dict)
|
||||
except IndexError:
|
||||
return ''
|
||||
|
||||
|
||||
addons = [
|
||||
Counter("http://www.ysqorz.top:8888/api/private/v1/")
|
||||
]
|
||||
|
||||
"""
|
||||
|
||||
mitmweb -stools\recording 启动
|
||||
ctrl + C 停止 并生成完整用例
|
||||
"""
|
Loading…
Reference in New Issue