api-testing-en

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

API Testing (English)

API测试(英文版)

中文版: See skill
api-testing
.
Prompt: this directory's
prompts/api-testing_EN.md
.
中文版: 请查看技能
api-testing
Prompt:本目录下的
prompts/api-testing_EN.md

When 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

使用方法

  1. Open
    prompts/api-testing_EN.md
    , copy everything below the dashed line into the AI chat.
  2. Append your specific input.
  3. For Excel/CSV/JSON, append the request line from output-formats.md.
  1. 打开
    prompts/api-testing_EN.md
    ,复制分隔线以下的所有内容到AI对话窗口。
  2. 追加你的具体输入内容。
  3. 若需要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.sh
Test 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测试设计原则

  1. Test Pyramid
    • Unit tests: Test individual API endpoints
    • Integration tests: Test interactions between multiple endpoints
    • End-to-end tests: Test complete business workflows
  2. Test Data Management
    • Use environment variables for different environment configurations
    • Use dynamic variables to avoid hardcoding
    • Clean up data after tests
  3. Assertion Strategy
    • Verify status codes
    • Verify response structure (Schema Validation)
    • Verify response data
    • Verify response time
  4. Error Handling
    • Test various error scenarios (4xx, 5xx)
    • Verify error message format
    • Test boundary conditions
  1. 测试金字塔
    • 单元测试:测试单个API端点
    • 集成测试:测试多个端点间的交互
    • 端到端测试:测试完整业务流程
  2. 测试数据管理
    • 使用环境变量适配不同环境配置
    • 使用动态变量避免硬编码
    • 测试完成后清理数据
  3. 断言策略
    • 验证状态码
    • 验证响应结构(Schema校验)
    • 验证响应数据
    • 验证响应时间
  4. 错误处理
    • 测试各类错误场景(4xx、5xx状态码)
    • 验证错误消息格式
    • 测试边界条件

Tool Selection Guide

工具选择指南

ToolUse CaseAdvantages
Postman/NewmanREST API testingEasy to use, visual, CI/CD integration
REST AssuredJava projectsStrong typing, BDD style
Pytest + RequestsPython projectsFlexible, rich ecosystem
SuperTestNode.js projectsGood integration with Express
GraphQL PlaygroundGraphQL APIDesigned specifically for GraphQL
工具适用场景优势
Postman/NewmanREST API测试易于使用、可视化、支持CI/CD集成
REST AssuredJava项目强类型、BDD风格
Pytest + RequestsPython项目灵活、生态丰富
SuperTestNode.js项目与Express框架集成良好
GraphQL PlaygroundGraphQL API专为GraphQL设计

Troubleshooting

故障排查

Common Issues

常见问题

1. Newman Run Failure

1. Newman运行失败

Problem:
newman: command not found
Solution:
bash
undefined
问题:
newman: command not found
解决方案:
bash
undefined

Install Newman globally

全局安装Newman

npm install -g newman
npm install -g newman

Or use npx (no global installation needed)

或使用npx(无需全局安装)

npx newman run collection.json
undefined
npx newman run collection.json
undefined

2. Environment Variables Not Working

2. 环境变量不生效

Problem: Variables in tests show as
{{variable}}
Solution:
  • Ensure using
    -e
    parameter to specify environment file
  • 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.json

3. Certificate Verification Error

3. 证书验证错误

Problem:
SSL certificate problem
Solution:
bash
undefined
问题:
SSL certificate problem
解决方案:
bash
undefined

Temporarily 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
undefined
newman run collection.json --ssl-client-cert-list cert-list.json
undefined

4. Request Timeout

4. 请求超时

Problem:
Error: ETIMEDOUT
or
Error: ESOCKETTIMEDOUT
Solution:
bash
undefined
问题:
Error: ETIMEDOUT
Error: ESOCKETTIMEDOUT
解决方案:
bash
undefined

Increase 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
undefined
newman run collection.json --delay-request 500
undefined

5. 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
    pm.response.json()
    to print actual response
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
    --bail
    to stop on first failure
问题: 单个测试通过,但批量运行失败
解决方案:
  • 检查测试间的数据依赖关系
  • 确保每个测试可独立运行
  • 添加适当延迟:
    --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.