📊 测试覆盖对比
| 接口复杂度 |
基础版本 |
增强版本 |
提升倍数 |
覆盖场景 |
简单接口 2-3个参数 |
1个测试 |
15-20个测试 |
15-20倍 |
正常+异常+安全+边界 |
中等接口 4-6个参数 |
1个测试 |
25-35个测试 |
25-35倍 |
全场景覆盖 |
复杂接口 7+个参数 |
1个测试 |
40-60个测试 |
40-60倍 |
深度测试矩阵 |
🔍 实际生成示例
对于一个用户注册接口 POST /api/users:
基础版本生成
test('POST /api/users', async () => {
const response = await request.post('/api/users');
expect(response.status()).toBeLessThan(500);
});
增强版本生成
test('POST /api/users - 正常请求');
test('POST /api/users - 不带可选参数');
test('POST /api/users - 缺少邮箱');
test('POST /api/users - 邮箱格式错误');
test('POST /api/users - 用户名过长');
test('POST /api/users - SQL注入防护');
test('POST /api/users - XSS防护');
test('POST /api/users - 无认证令牌');
test('POST /api/users - 特殊字符处理');
test('POST /api/users - 超大请求体');
test('POST /api/users - 响应时间验证');
🛠️ 使用方法
1. 生成增强版测试
node scripts/smart-api-generator.js ./openapi.json
node scripts/smart-api-generator.js ./openapi.json --force
node scripts/smart-api-generator.js ./openapi.json --output ./tests/my-api
2. 分类运行测试
npx playwright test --grep "@smoke"
npx playwright test --grep "@negative"
npx playwright test --grep "@security"
npx playwright test --grep "@P0|@P1"
3. 查看测试报告
npm run test:api:allure
npm run allure:serve
npm run test:api:html
npx playwright show-report