api-testing-en
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAPI Testing (English)
API测试(英文版)
中文版: See skill .
api-testingPrompt: this directory's .
prompts/api-testing_EN.md中文版: 请查看技能。
api-testingPrompt:本目录下的。
prompts/api-testing_EN.mdWhen to Use
适用场景
- User mentions API testing, api-testing
- Need to execute this testing type or produce deliverables per Standard-version
- Trigger examples: "Generate/design/write API test cases for the following"
- 用户提及API测试、api-testing
- 需要执行该类型测试或按照标准版本生成交付物
- 触发示例: "为以下内容生成/设计/编写API测试用例"
Output Format Options
输出格式选项
Markdown by default. For Excel / CSV / JSON, add at the end of your request; see output-formats.md.
默认输出Markdown格式。若需要Excel/CSV/JSON格式,请在请求末尾添加对应说明;详情请查看**output-formats.md**。
How to Use
使用方法
- Open , copy everything below the dashed line into the AI chat.
prompts/api-testing_EN.md - Append your specific input.
- For Excel/CSV/JSON, append the request line from output-formats.md.
- 打开,复制分隔线以下的所有内容到AI对话窗口。
prompts/api-testing_EN.md - 追加你的具体输入内容。
- 若需要Excel/CSV/JSON格式,追加中对应的请求语句。
output-formats.md
Code Examples
代码示例
1. Postman + Newman REST API Testing
1. Postman + Newman REST API测试
Complete user management API test example with 10 test cases.
Location:
examples/postman-rest-api/Includes:
- Postman collection file (10 test cases)
- Environment variable configuration
- Newman automation run script
- Detailed README documentation
Quick Start:
bash
cd examples/postman-rest-api
npm install -g newman
./newman-run.shTest Coverage:
- User CRUD operations (Create, Read, Update, Delete)
- Authentication and authorization testing
- Error handling and boundary conditions
- Response time validation
- Data format validation
See: examples/postman-rest-api/README.md
包含10个测试用例的完整用户管理API测试示例。
位置:
examples/postman-rest-api/包含内容:
- Postman集合文件(10个测试用例)
- 环境变量配置
- Newman自动化运行脚本
- 详细的README文档
快速开始:
bash
cd examples/postman-rest-api
npm install -g newman
./newman-run.sh测试覆盖范围:
- 用户CRUD操作(创建、读取、更新、删除)
- 身份认证与授权测试
- 错误处理与边界条件
- 响应时间验证
- 数据格式验证
详情请见:examples/postman-rest-api/README.md
2. REST Assured (Java)
2. REST Assured(Java)
Coming soon - Java-based REST API testing with REST Assured framework.
Location:
examples/rest-assured-java/即将推出——基于Java语言、使用REST Assured框架的REST API测试方案。
位置:
examples/rest-assured-java/3. SuperTest (Node.js)
3. SuperTest(Node.js)
Coming soon - Node.js API testing with SuperTest framework.
Location:
examples/supertest-nodejs/即将推出——基于Node.js语言、使用SuperTest框架的API测试方案。
位置:
examples/supertest-nodejs/Best Practices
最佳实践
API Test Design Principles
API测试设计原则
-
Test Pyramid
- Unit tests: Test individual API endpoints
- Integration tests: Test interactions between multiple endpoints
- End-to-end tests: Test complete business workflows
-
Test Data Management
- Use environment variables for different environment configurations
- Use dynamic variables to avoid hardcoding
- Clean up data after tests
-
Assertion Strategy
- Verify status codes
- Verify response structure (Schema Validation)
- Verify response data
- Verify response time
-
Error Handling
- Test various error scenarios (4xx, 5xx)
- Verify error message format
- Test boundary conditions
-
测试金字塔
- 单元测试:测试单个API端点
- 集成测试:测试多个端点间的交互
- 端到端测试:测试完整业务流程
-
测试数据管理
- 使用环境变量适配不同环境配置
- 使用动态变量避免硬编码
- 测试完成后清理数据
-
断言策略
- 验证状态码
- 验证响应结构(Schema校验)
- 验证响应数据
- 验证响应时间
-
错误处理
- 测试各类错误场景(4xx、5xx状态码)
- 验证错误消息格式
- 测试边界条件
Tool Selection Guide
工具选择指南
| Tool | Use Case | Advantages |
|---|---|---|
| Postman/Newman | REST API testing | Easy to use, visual, CI/CD integration |
| REST Assured | Java projects | Strong typing, BDD style |
| Pytest + Requests | Python projects | Flexible, rich ecosystem |
| SuperTest | Node.js projects | Good integration with Express |
| GraphQL Playground | GraphQL API | Designed specifically for GraphQL |
| 工具 | 适用场景 | 优势 |
|---|---|---|
| Postman/Newman | REST API测试 | 易于使用、可视化、支持CI/CD集成 |
| REST Assured | Java项目 | 强类型、BDD风格 |
| Pytest + Requests | Python项目 | 灵活、生态丰富 |
| SuperTest | Node.js项目 | 与Express框架集成良好 |
| GraphQL Playground | GraphQL API | 专为GraphQL设计 |
Troubleshooting
故障排查
Common Issues
常见问题
1. Newman Run Failure
1. Newman运行失败
Problem:
newman: command not foundSolution:
bash
undefined问题:
newman: command not found解决方案:
bash
undefinedInstall Newman globally
全局安装Newman
npm install -g newman
npm install -g newman
Or use npx (no global installation needed)
或使用npx(无需全局安装)
npx newman run collection.json
undefinednpx newman run collection.json
undefined2. Environment Variables Not Working
2. 环境变量不生效
Problem: Variables in tests show as
{{variable}}Solution:
- Ensure using parameter to specify environment file
-e - Check if variable names in environment file are correct
- Verify variables are correctly set in Postman
bash
newman run collection.json -e environment.json问题: 测试中的变量显示为
{{variable}}解决方案:
- 确保使用参数指定环境文件
-e - 检查环境文件中的变量名称是否正确
- 验证Postman中变量是否已正确设置
bash
newman run collection.json -e environment.json3. Certificate Verification Error
3. 证书验证错误
Problem:
SSL certificate problemSolution:
bash
undefined问题:
SSL certificate problem解决方案:
bash
undefinedTemporarily disable SSL verification for dev environment (not recommended for production)
开发环境下临时禁用SSL验证(不推荐生产环境使用)
newman run collection.json --insecure
newman run collection.json --insecure
Or specify CA certificate
或指定CA证书
newman run collection.json --ssl-client-cert-list cert-list.json
undefinednewman run collection.json --ssl-client-cert-list cert-list.json
undefined4. Request Timeout
4. 请求超时
Problem: or
Error: ETIMEDOUTError: ESOCKETTIMEDOUTSolution:
bash
undefined问题: 或
Error: ETIMEDOUTError: ESOCKETTIMEDOUT解决方案:
bash
undefinedIncrease timeout (milliseconds)
增加超时时间(毫秒)
newman run collection.json --timeout-request 30000
newman run collection.json --timeout-request 30000
Add request delay
添加请求延迟
newman run collection.json --delay-request 500
undefinednewman run collection.json --delay-request 500
undefined5. Response Data Format Mismatch
5. 响应数据格式不匹配
Problem: JSON Schema validation fails
Solution:
- Use Postman's Schema generation feature
- Check API documentation to confirm data structure
- Use to print actual response
pm.response.json()
javascript
// Add debug info in Tests
console.log(pm.response.json());问题: JSON Schema校验失败
解决方案:
- 使用Postman的Schema生成功能
- 查阅API文档确认数据结构
- 使用打印实际响应内容
pm.response.json()
javascript
// 在Tests中添加调试信息
console.log(pm.response.json());6. Dynamic Data Dependency Issues
6. 动态数据依赖问题
Problem: Subsequent requests depend on data from previous request
Solution:
javascript
// Save data in first request's Tests
pm.environment.set("userId", pm.response.json().id);
// Use in subsequent requests
// URL: {{baseUrl}}/users/{{userId}}问题: 后续请求依赖于前序请求的数据
解决方案:
javascript
// 在第一个请求的Tests中保存数据
pm.environment.set("userId", pm.response.json().id);
// 在后续请求中使用
// URL: {{baseUrl}}/users/{{userId}}7. Batch Test Run Failures
7. 批量测试运行失败
Problem: Individual tests pass, batch run fails
Solution:
- Check data dependencies between tests
- Ensure each test can run independently
- Add appropriate delays:
--delay-request 200 - Use to stop on first failure
--bail
问题: 单个测试通过,但批量运行失败
解决方案:
- 检查测试间的数据依赖关系
- 确保每个测试可独立运行
- 添加适当延迟:
--delay-request 200 - 使用参数在第一个失败时停止运行
--bail
Reference Files
参考文件
- prompts/api-testing_EN.md — API testing Standard-version prompt
- output-formats.md — Markdown / Excel / CSV / JSON request instructions
- examples/postman-rest-api/ — Complete Postman + Newman example
- quick-start.md — 5-minute quick start guide
Related skills: automation-testing-en, performance-testing-en, security-testing-en, functional-testing-en.
- prompts/api-testing_EN.md — API测试标准版本提示词
- output-formats.md — Markdown/Excel/CSV/JSON格式请求说明
- examples/postman-rest-api/ — 完整的Postman + Newman示例
- quick-start.md — 5分钟快速入门指南
相关技能: automation-testing-en, performance-testing-en, security-testing-en, functional-testing-en.