使用说明
API自动测试框架
YAML声明式API测试框架,针对AI编码助手进行优化
为什么是这个框架?
当要求人工智能编写API测试时,您可能会遇到以下问题:
场景1:重复性工作
每次你要求人工智能生成测试时,你都需要重新描述项目结构、身份验证方法和断言风格。对于10个API测试,生成10次相同的fixture和设置代码。
场景2:令牌黑洞
一个简单的登录API测试生成200行代码。你发现一个断言是错误的,请人工智能修复它,它会再生成200行。经过3次修订,你已经消耗了2000多个代币,但你仍然需要手动修复它。
场景3:调试死循环
AI生成的测试无法运行。你粘贴了错误消息,AI修复了它,但仍然是错误的。经过5轮对话,问题仍然存在,你已经烧毁了5000多个代币。
该框架的解决方案:
Traditional: Natural Language -> AI Generates Full Code -> Run Error -> Paste Error -> AI Regenerates -> Loop...
This Framework: Natural Language -> AI Generates YAML -> Framework Executes -> Locate Issue -> Fix 1 Line YAML
| 度量 | 传统人工智能 | 此框架 |
|---|---|---|
| 测试1 API | ~200行代码 | ~20行YAML |
| 修改断言 | 重新生成所有代码 | 修复1-2行YAML |
| 10 API测试 | 重复设置10x | 共享配置,0重复 |
| 调试问题 | 平均3-5轮 | 通常为1轮 |
主要特点
| 特性 | 描述 |
|---|---|
| YAML声明性测试 | 测试逻辑与执行代码分离,AI仅生成结构化数据 |
| MCP服务器 | 与Claude/Cursor和其他AI编辑器无缝集成 |
| API工作流编排 | 单个文件中的多步骤API调用,在步骤之间传递数据和断言 |
| 可变分辨率引擎 | 支持跨步骤数据传输、全局变量和动态函数调用 |
| 自动身份验证 | 框架处理令牌获取和刷新 |
| 数据工厂 | 内置模拟数据生成,无Java依赖 |
| 多格式报告 | Allure(离线/在线),pytest html(独立html,样式化) |
| 多渠道通知 | 钉钉、飞书、微信 |
| 单元测试 | 使用自动模拟依赖注入进行Python代码单元测试 |
快速开始
安装
# 1. Install uv (if not installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# 2. Install MCP server (recommended: install as a tool)
uv tool install git+https://github.com/GalaxyXieyu/Api-Test-MCP.git
# Verify
api-auto-test-mcp --help
# Manage tools
uv tool list
uv tool uninstall api-auto-test # use the tool name shown by `uv tool list`
运行而不安装(uvx):
uvx --from git+https://github.com/GalaxyXieyu/Api-Test-MCP.git api-auto-test-mcp --help
常见错误: uvx install ... 是 错误. uvx 将其后的第一个单词视为工具名称,因此它将尝试解析一个名为 install 并失败。
配置编辑器
将以下内容添加到编辑器的MCP设置中:
{
"mcpServers": {
"api-auto-test": {
"command": "api-auto-test-mcp"
}
}
}
| 编辑器 | 配置位置 |
|---|---|
| 克劳德桌面 | ~/Library/Application Support/Claude/claude_desktop_config.json |
| 光标 | 设置->MCP服务器 |
| VSCode+继续 | .vscode/mcp.json |
本地开发
# Recommended with uv
uv pip install -r requirements.txt
# Or with pip
pip install -r requirements.txt
创建测试用例
# tests/cases/user_login.yaml
testcase:
name: user_login
description: User login API test
host: http://localhost:8000
steps:
- id: login
path: /api/auth/login
method: POST
data:
username: "test_user"
password: "123456"
assert:
- type: status_code
expected: 200
- type: equals
field: data.code
expected: 0
生成并运行
# Generate pytest scripts
python -m atf.case_generator
# Run tests
pytest tests/scripts/ -v
# Generate Allure report
pytest tests/scripts/ --alluredir=tests/allure-results
allure serve tests/allure-results
# Generate pytest-html report
pytest tests/scripts/ --html=report.html
MCP服务器集成
通过MCP,AI编辑器可以直接调用框架工具来生成和执行测试。
效率比较
| 度量 | 无MCP | 有MCP | 改进 |
|---|---|---|---|
| 总成本 | 0.0214美元 | 0.0099美元 | -54% |
| API延迟时间 | 11秒 | 4秒 | -64% |
| 输出令牌 | 585 | 238 | -59% |
| 缓存读取 | 42.0k | 21.0k | -50% |
测试场景:相同的API测试生成任务(纯粹咨询/分析对话)
核心优势:
- 成本降低54%:MCP直接调用工具,避免冗长的代码生成上下文
- API响应速度快64%:工具调用比自然语言交互更有效
- 代币消费减少59%:只需要必要的参数,不需要重复项目结构
可用工具
| 工具 | 说明 |
|---|---|
list_testcases |
列出测试用例 |
get_testcase |
阅读测试用例内容 |
write_testcase |
创建/更新测试用例并生成pytest脚本 |
write_unittest |
创建单元测试 |
delete_testcase |
删除测试用例 |
run_tests |
执行测试 |
get_test_results |
获取测试执行历史记录 |
health_check |
服务健康检查 |
使用示例
告诉AI:
Create a test for /api/users interface, verify returned user list length > 0
AI将呼叫 write_testcase 生成YAML和相应的pytest脚本。
项目结构
api-auto-test/
├── atf/ # Framework core
│ ├── core/ # Request, assertion, variable resolution modules
│ ├── mcp/ # MCP Server implementation
│ └── handlers/ # Notification, report handlers
├── tests/
│ ├── cases/ # YAML test cases
│ └── scripts/ # Generated pytest scripts
├── config.yaml # Project config (environment, database, notifications)
└── pyproject.toml
YAML测试用例规范
基本结构
testcase:
name: test_name # Case name, used for filename
description: Description # Optional
host: http://localhost:8000 # API host, can also be configured globally in config.yaml
steps:
- id: step1 # Step ID, used for later reference
path: /api/endpoint
method: POST
headers:
Authorization: "Bearer {{ login.data.token }}" # Reference response from other step
data:
key: value
assert:
- type: status_code
expected: 200
- type: equals
field: data.id
expected: 1
断言类型
| 类型 | 描述 | 示例 |
|---|---|---|
status_code |
HTTP状态码 | expected: 200 |
equals |
完全匹配 | field: data.id, expected: 1 |
contains |
包含 | field: data.name, expected: "test" |
length |
数组/字符串长度 | field: data.list, expected: 10 |
regex |
正则表达式匹配 | field: data.email, expected: "^\\w+@" |
变量引用
# Reference response data from other steps
token: "{{ login.data.token }}"
# Reference global config
host: "{{ merchant.host }}"
# Call built-in functions
timestamp: "{{ tools.get_timestamp() }}"
uuid: "{{ tools.generate_uuid() }}"
拆除
testcase:
name: create_and_delete_user
steps:
- id: create_user
path: /api/users
method: POST
data:
name: "test_user"
teardowns:
- id: delete_user
operation_type: api
path: /api/users/{{ create_user.data.id }}
method: DELETE
单元测试
支持为Python代码编写单元测试,通过MCP工具自动生成测试用例。
单元测试YAML格式
unittest:
name: UserService Test
target:
module: app.services.user_service
class: UserService
function: get_user
fixtures:
setup:
- type: patch
target: app.services.user_service.UserRepository
return_value:
id: 1
name: "test_user"
cases:
- id: test_get_user_success
description: Test get user success
inputs:
args: [1]
kwargs: {}
assert:
- type: equals
field: result.id
expected: 1
- type: equals
field: result.name
expected: "test_user"
断言类型
| 类型 | 描述 |
|---|---|
equals |
完全匹配 |
not_equals |
不相等 |
contains |
包含 |
raises |
预计会引发异常 |
is_none |
结果为无 |
is_not_none |
结果不是无 |
called_once |
Mock打过一次电话 |
called_with |
使用特定参数调用模拟 |
配置文件
# config.yaml
projects:
merchant:
test:
host: http://192.168.1.100:8080
is_need_login: true
login:
url: http://192.168.1.100:8080/login
method: POST
data:
username: admin
password: "123456"
online:
host: https://api.example.com
is_need_login: true
notifications:
dingtalk:
webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxx"
secret: "SECxxx"
许可证
MIT许可证
