Skip to content

Commit b27f9d6

Browse files
committed
✨ Feat(New interface automation plugin has been added): 新增接口自动化插件
1 parent ee6d9e4 commit b27f9d6

38 files changed

+7657
-0
lines changed
Lines changed: 295 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,295 @@
1+
# 接口自动化测试插件
2+
3+
基于httpx的接口自动化测试工具,支持请求发送、断言验证、SQL执行和测试报告生成。
4+
5+
## 功能特点
6+
7+
- **接口请求**: 支持各种HTTP方法(GET, POST, PUT, DELETE等)的API请求
8+
- **灵活断言**: 提供20+种断言类型,支持对响应状态码、头信息、JSON内容等进行验证
9+
- **SQL执行**: 支持MySQL和PostgreSQL数据库操作,用于测试前后数据验证
10+
- **变量提取**: 从响应或SQL结果中提取变量,用于后续测试步骤
11+
- **测试报告**: 生成美观的HTML或JSON格式测试报告,包含详细的失败分析
12+
- **失败重试**: 支持请求失败自动重试机制
13+
- **环境变量**: 多环境管理,支持全局、项目、环境、用例和临时变量
14+
- **Mock服务**: 提供接口Mock功能,支持条件匹配和响应定制
15+
- **数据驱动**: 支持CSV、Excel、JSON和数据库数据源的数据驱动测试
16+
- **历史记录**: 记录接口请求历史,支持统计分析和查询
17+
18+
## 安装使用
19+
20+
1. 将本插件目录复制到FastAPI项目的`backend/plugin/`目录下
21+
2. 安装依赖: `pip install httpx jsonpath-ng jinja2 pymysql psycopg pandas xlrd`
22+
3. 重启应用服务器
23+
24+
## API接口
25+
26+
### 请求接口
27+
28+
- **POST** `/v1/api_testing/requests/send`: 发送API请求
29+
30+
### 断言接口
31+
32+
- **POST** `/v1/api_testing/assertions/validate`: 执行单个断言验证
33+
- **POST** `/v1/api_testing/assertions/batch-validate`: 批量执行断言验证
34+
35+
### SQL接口
36+
37+
- **POST** `/v1/api_testing/sql/execute`: 执行SQL查询
38+
- **POST** `/v1/api_testing/sql/batch-execute`: 批量执行SQL查询
39+
40+
### 报告接口
41+
42+
- **POST** `/v1/api_testing/reports/generate`: 生成测试报告
43+
- **POST** `/v1/api_testing/reports/preview`: 预览HTML测试报告
44+
45+
### 环境变量接口
46+
47+
- **POST** `/v1/api_testing/environments`: 创建环境
48+
- **GET** `/v1/api_testing/environments/{environment_id}`: 获取环境信息
49+
- **GET** `/v1/api_testing/environments`: 获取环境列表
50+
- **POST** `/v1/api_testing/environments/variables`: 创建变量
51+
- **GET** `/v1/api_testing/environments/variables`: 获取变量列表
52+
53+
### Mock服务接口
54+
55+
- **POST** `/v1/api_testing/mocks/projects`: 创建Mock项目
56+
- **GET** `/v1/api_testing/mocks/projects`: 获取Mock项目列表
57+
- **POST** `/v1/api_testing/mocks/rules`: 创建Mock规则
58+
- **GET** `/v1/api_testing/mocks/rules`: 获取Mock规则列表
59+
60+
### 数据驱动接口
61+
62+
- **POST** `/v1/api_testing/data-driven/config`: 创建数据驱动配置
63+
- **POST** `/v1/api_testing/data-driven/upload/csv`: 上传CSV数据文件
64+
- **POST** `/v1/api_testing/data-driven/upload/excel`: 上传Excel数据文件
65+
66+
### 历史记录接口
67+
68+
- **POST** `/v1/api_testing/history`: 保存请求历史记录
69+
- **GET** `/v1/api_testing/history/{history_id}`: 获取请求历史记录
70+
- **GET** `/v1/api_testing/history`: 获取请求历史记录列表
71+
- **GET** `/v1/api_testing/history/statistics`: 获取历史记录统计信息
72+
73+
## 断言类型
74+
75+
| 断言类型 | 描述 |
76+
|---------|------|
77+
| equals | 等于 |
78+
| not_equals | 不等于 |
79+
| contains | 包含 |
80+
| not_contains | 不包含 |
81+
| starts_with | 以...开头 |
82+
| ends_with | 以...结尾 |
83+
| match_regex | 匹配正则表达式 |
84+
| less_than | 小于 |
85+
| less_than_or_equals | 小于或等于 |
86+
| greater_than | 大于 |
87+
| greater_than_or_equals | 大于或等于 |
88+
| exists | 存在 |
89+
| not_exists | 不存在 |
90+
| is_empty | 为空 |
91+
| is_not_empty | 不为空 |
92+
| is_null | 为null |
93+
| is_not_null | 不为null |
94+
| is_true | 为true |
95+
| is_false | 为false |
96+
| length_equals | 长度等于 |
97+
| length_greater_than | 长度大于 |
98+
| length_less_than | 长度小于 |
99+
100+
## 使用示例
101+
102+
### 发送请求
103+
104+
```python
105+
import requests
106+
107+
response = requests.post(
108+
"http://localhost:8000/v1/api_testing/requests/send",
109+
json={
110+
"url": "https://api.example.com/users",
111+
"method": "GET",
112+
"headers": {
113+
"Authorization": "Bearer token123",
114+
"Content-Type": "application/json"
115+
},
116+
"params": {
117+
"page": 1,
118+
"limit": 10
119+
}
120+
}
121+
)
122+
123+
print(response.json())
124+
```
125+
126+
### 执行断言
127+
128+
```python
129+
import requests
130+
131+
response = requests.post(
132+
"http://localhost:8000/v1/api_testing/assertions/validate",
133+
json={
134+
"assertion": {
135+
"source": "json",
136+
"type": "equals",
137+
"path": "$.data.id",
138+
"expected": 123,
139+
"message": "用户ID验证"
140+
},
141+
"response_data": {
142+
"status_code": 200,
143+
"json": {
144+
"data": {
145+
"id": 123,
146+
"name": "测试用户"
147+
}
148+
}
149+
}
150+
}
151+
)
152+
153+
print(response.json())
154+
```
155+
156+
### 执行SQL查询
157+
158+
```python
159+
import requests
160+
161+
response = requests.post(
162+
"http://localhost:8000/v1/api_testing/sql/execute",
163+
json={
164+
"name": "查询用户",
165+
"query": "SELECT id, username FROM users WHERE id = 1",
166+
"extract": {
167+
"user_id": "0.id",
168+
"username": "0.username"
169+
},
170+
"use_default_db": true
171+
}
172+
)
173+
174+
print(response.json())
175+
```
176+
177+
### 使用环境变量
178+
179+
```python
180+
import requests
181+
182+
# 创建环境变量
183+
response = requests.post(
184+
"http://localhost:8000/v1/api_testing/environments/variables",
185+
json={
186+
"name": "base_url",
187+
"value": "https://api.example.com",
188+
"scope": "project",
189+
"project_id": 1,
190+
"description": "API基础URL"
191+
}
192+
)
193+
194+
# 使用变量发送请求
195+
response = requests.post(
196+
"http://localhost:8000/v1/api_testing/requests/send",
197+
json={
198+
"url": "{{base_url}}/users", # 使用变量
199+
"method": "GET",
200+
}
201+
)
202+
203+
print(response.json())
204+
```
205+
206+
### 创建Mock服务
207+
208+
```python
209+
import requests
210+
211+
# 创建Mock项目
212+
response = requests.post(
213+
"http://localhost:8000/v1/api_testing/mocks/projects",
214+
json={
215+
"id": 1,
216+
"name": "用户API Mock",
217+
"base_path": "/api",
218+
"port": 3000
219+
}
220+
)
221+
222+
# 创建Mock规则
223+
response = requests.post(
224+
"http://localhost:8000/v1/api_testing/mocks/rules",
225+
json={
226+
"id": "user_rule_1",
227+
"name": "获取用户信息",
228+
"url": "/users/:id",
229+
"method": "GET",
230+
"project_id": 1,
231+
"responses": [{
232+
"id": "resp_1",
233+
"name": "成功响应",
234+
"status_code": 200,
235+
"response_type": "json",
236+
"content": '{"id": 1, "name": "Test User", "email": "[email protected]"}'
237+
}]
238+
}
239+
)
240+
```
241+
242+
### 数据驱动测试
243+
244+
```python
245+
import requests
246+
247+
# 创建数据驱动配置
248+
response = requests.post(
249+
"http://localhost:8000/v1/api_testing/data-driven/config",
250+
json={
251+
"enabled": True,
252+
"data_source": {
253+
"name": "用户数据",
254+
"type": "csv",
255+
"file_path": "data/users.csv"
256+
},
257+
"parameters": [
258+
{"name": "user_id", "value": "1"},
259+
{"name": "username", "value": "test"}
260+
],
261+
"loop_mode": "sequential"
262+
}
263+
)
264+
```
265+
266+
### 查询历史记录
267+
268+
```python
269+
import requests
270+
271+
# 获取历史记录列表
272+
response = requests.get(
273+
"http://localhost:8000/v1/api_testing/history?project_id=1&limit=10&sort_desc=true"
274+
)
275+
276+
# 获取历史统计信息
277+
response = requests.get(
278+
"http://localhost:8000/v1/api_testing/history/statistics?project_id=1"
279+
)
280+
281+
print(response.json())
282+
```
283+
284+
## 注意事项
285+
286+
1. 文件上传请确保服务器有权限访问指定的文件路径
287+
2. SQL执行时请注意权限控制,避免安全风险
288+
3. 大型测试报告可能会占用较多内存,请根据服务器配置合理使用
289+
4. Mock服务需要单独运行,默认监听端口为3000
290+
5. 环境变量中的敏感信息会进行简单加密存储
291+
292+
## 版本信息
293+
294+
- 版本: 0.1.0
295+
- 作者: ranyong
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""接口自动化测试插件"""
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""API路由模块"""
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
from fastapi import APIRouter
4+
5+
from backend.core.conf import settings
6+
from backend.plugin.api_testing.api.v1.request import router as request_router
7+
from backend.plugin.api_testing.api.v1.assertion import router as assertion_router
8+
from backend.plugin.api_testing.api.v1.sql import router as sql_router
9+
from backend.plugin.api_testing.api.v1.report import router as report_router
10+
from backend.plugin.api_testing.api.v1.environment import router as environment_router
11+
from backend.plugin.api_testing.api.v1.mock import router as mock_router
12+
from backend.plugin.api_testing.api.v1.data_driven import router as data_driven_router
13+
from backend.plugin.api_testing.api.v1.history import router as history_router
14+
15+
v1 = APIRouter(prefix=f'{settings.FASTAPI_API_V1_PATH}/api_testing', tags=['接口自动化测试'])
16+
17+
v1.include_router(request_router, prefix='/requests')
18+
v1.include_router(assertion_router, prefix='/assertions')
19+
v1.include_router(sql_router, prefix='/sql')
20+
v1.include_router(report_router, prefix='/reports')
21+
v1.include_router(environment_router, prefix='/environments')
22+
v1.include_router(mock_router, prefix='/mocks')
23+
v1.include_router(data_driven_router, prefix='/data-driven')
24+
v1.include_router(history_router, prefix='/history')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
"""V1 版本 API 模块"""

0 commit comments

Comments
 (0)