testing-patterns
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTesting Patterns
测试模式
A pragmatic approach to testing that emphasises:
- Live testing over mocks
- Agent execution to preserve context
- YAML specs as documentation and tests
- Persistent results committed to git
一种务实的测试方法,核心强调:
- 优先实时测试而非模拟测试
- 通过Agent执行以保留上下文
- YAML规范兼具文档与测试双重作用
- 持久化测试结果并提交至Git
Philosophy
设计理念
This is not traditional TDD. Instead:
- Test in production/staging with good logging
- Use agents to run tests (keeps main context clean)
- Define tests declaratively in YAML (human-readable, version-controlled)
- Focus on integration (real servers, real data)
这并非传统的TDD测试方法,而是采用以下思路:
- 在生产/预发布环境测试,配合完善的日志系统
- 使用Agent运行测试(保持主上下文清洁)
- 通过YAML声明式定义测试(易读、可版本控制)
- 聚焦集成测试(真实服务器、真实数据)
Why Agent-Based Testing?
为何选择基于Agent的测试?
Running 50 tests in the main conversation would consume your entire context window. By delegating to a sub-agent:
- Main context stays clean for development
- Agent can run many tests without context pressure
- Results come back as a summary
- Failed tests get detailed investigation
在主对话中运行50个测试会耗尽整个上下文窗口。通过委托给子Agent:
- 主上下文可保持清洁,专注于开发工作
- Agent可在无上下文压力的情况下运行大量测试
- 结果以汇总形式返回
- 失败的测试会得到详细排查
Commands
命令列表
| Command | Purpose |
|---|---|
| Discover project, generate test specs + testing agent |
| Execute tests via agent(s), report results |
| Generate coverage report and identify uncovered code paths |
Quick workflow:
/create-tests → Generates tests/specs/*.yaml + .claude/agents/test-runner.md
/run-tests → Spawns agent, runs all tests, saves results
/run-tests api → Run only specs matching "api"
/run-tests --failed → Re-run only failed tests
/coverage → Run tests with coverage, analyse gaps
/coverage --threshold 80 → Fail if below 80%| 命令 | 用途 |
|---|---|
| 发现项目,生成测试规范 + 测试Agent |
| 通过Agent执行测试,报告结果 |
| 生成覆盖率报告,识别未覆盖的代码路径 |
快速工作流:
/create-tests → 生成tests/specs/*.yaml + .claude/agents/test-runner.md
/run-tests → 启动Agent,运行所有测试并保存结果
/run-tests api → 仅运行匹配"api"的测试规范
/run-tests --failed → 仅重新运行失败的测试
/coverage → 运行测试并统计覆盖率,分析缺口
/coverage --threshold 80 → 覆盖率低于80%则判定失败Getting Started in a New Project
新项目快速上手
This skill provides the pattern and format. Claude designs the actual tests based on your project context.
What happens when you ask "Create tests for this project":
-
Discovery - Claude examines the project:
- What MCP servers are configured?
- What APIs or tools exist?
- What does the code do?
-
Test Design - Claude creates project-specific tests:
- Test cases for the actual tools/endpoints
- Expected values based on real behavior
- Edge cases relevant to this domain
-
Structure - Using patterns from this skill:
- YAML specs in directory
tests/ - Optional testing agent in
.claude/agents/ - Results saved to
tests/results/
- YAML specs in
Example:
You: "Create tests for this MCP server"
Claude: [Discovers this is a Google Calendar MCP]
[Sees tools: calendar_events, calendar_create, calendar_delete]
[Designs test cases:]
tests/calendar-events.yaml:
- list_upcoming_events (expect: array, count_gte 0)
- search_by_keyword (expect: contains search term)
- invalid_date_range (expect: error status)
tests/calendar-mutations.yaml:
- create_event (expect: success, returns event_id)
- delete_nonexistent (expect: error, contains "not found")The skill teaches Claude:
- How to structure YAML test specs
- What validation rules are available
- How to create testing agents
- When to use parallel execution
Your project provides:
- What to actually test
- Expected values and behaviors
- Domain-specific edge cases
本技能提供测试模式与格式规范,Claude会根据你的项目上下文设计具体的测试用例。
当你请求"为该项目创建测试"时的流程:
-
项目发现 - Claude会分析项目:
- 配置了哪些MCP服务器?
- 存在哪些API或工具?
- 代码的功能是什么?
-
测试设计 - Claude创建项目专属测试:
- 针对实际工具/端点的测试用例
- 基于真实行为的预期值
- 与领域相关的边缘场景
-
结构搭建 - 采用本技能提供的模式:
- 测试规范存储在目录下的YAML文件中
tests/ - 可选的测试Agent存储在目录
.claude/agents/ - 测试结果保存至目录
tests/results/
- 测试规范存储在
示例:
你: "为这个MCP服务器创建测试"
Claude: [识别出这是Google Calendar MCP]
[发现工具:calendar_events、calendar_create、calendar_delete]
[设计测试用例:]
tests/calendar-events.yaml:
- list_upcoming_events (预期:数组类型,数量≥0)
- search_by_keyword (预期:包含搜索关键词)
- invalid_date_range (预期:返回错误状态)
tests/calendar-mutations.yaml:
- create_event (预期:成功,返回event_id)
- delete_nonexistent (预期:错误,包含"not found")本技能向Claude传授:
- 如何结构化YAML测试规范
- 可用的验证规则
- 如何创建测试Agent
- 何时使用并行执行
你的项目需要提供:
- 实际需要测试的内容
- 预期值与行为
- 领域特定的边缘场景
YAML Test Spec Format
YAML测试规范格式
yaml
name: Feature Tests
description: What these tests validateyaml
name: 功能测试
description: 这些测试的验证目标Optional: defaults applied to all tests
可选:所有测试的默认配置
defaults:
tool: my_tool_name
timeout: 5000
tests:
- name: test_case_name description: Human-readable purpose tool: tool_name # Override default if needed params: action: search query: "test input" expect: contains: "expected substring" not_contains: "should not appear" status: success
undefineddefaults:
tool: my_tool_name
timeout: 5000
tests:
- name: test_case_name description: 易读的测试目的说明 tool: tool_name # 可覆盖默认配置 params: action: search query: "test input" expect: contains: "预期子字符串" not_contains: "不应出现的内容" status: success
undefinedValidation Rules
验证规则
| Rule | Description | Example |
|---|---|---|
| Response contains string | |
| Response doesn't contain | |
| Regex pattern match | |
| Check value at JSON path | |
| Exact value match | |
| Check success/error | |
| Array length >= N | |
| Array length == N | |
| Value type check | |
See for complete documentation.
references/validation-rules.md| 规则 | 描述 | 示例 |
|---|---|---|
| 响应包含指定字符串 | |
| 响应不包含指定字符串 | |
| 匹配正则表达式 | |
| 检查JSON路径对应的值 | |
| 完全匹配值 | |
| 检查成功/错误状态 | |
| 数组长度≥N | |
| 数组长度==N | |
| 检查值的类型 | |
完整文档请参考。
references/validation-rules.mdCreating a Testing Agent
创建测试Agent
Testing agents inherit MCP tools from the session. Create an agent that:
- Reads YAML test specs
- Executes tool calls with params
- Validates responses against expectations
- Reports results
测试Agent会从会话中继承MCP工具。创建的Agent需要具备以下能力:
- 读取YAML测试规范
- 使用参数执行工具调用
- 根据预期验证响应
- 报告测试结果
Agent Template
Agent模板
CRITICAL: Do NOT specify a field if you need MCP access. When you specify ANY tools, it becomes an allowlist and is interpreted literally (not as a wildcard). Omit entirely to inherit ALL tools from the parent session.
tools"*"toolsyaml
---
name: my-tester
description: |
Tests [domain] functionality. Reads YAML test specs and validates responses.
Use when: testing after changes, running regression tests.重要提示:如果需要访问MCP,请不要指定字段。当你指定任何工具时,它会被视为允许列表,会被解释为字面量而非通配符。完全省略字段即可从父会话继承所有工具。
tools"*"toolsyaml
---
name: my-tester
description: |
测试[领域]功能。读取YAML测试规范并验证响应。
适用场景:变更后测试、运行回归测试。tools field OMITTED - inherits ALL tools from parent (including MCP)
省略tools字段 - 从父会话继承所有工具(包括MCP)
model: sonnet
model: sonnet
[Domain] Tester
[领域]测试器
How It Works
工作原理
- Find test specs:
tests/*.yaml - Parse and execute each test
- Validate responses
- Report pass/fail summary
- 查找测试规范:
tests/*.yaml - 解析并执行每个测试
- 验证响应是否符合预期
- 报告通过/失败汇总
Test Spec Location
测试规范位置
tests/
├── feature-a.yaml
├── feature-b.yaml
└── results/
└── YYYY-MM-DD-HHMMSS.md
tests/
├── feature-a.yaml
├── feature-b.yaml
└── results/
└── YYYY-MM-DD-HHMMSS.md
Execution
执行流程
For each test:
- Call tool with params
- Capture response
- Apply validation rules
- Record PASS/FAIL
对于每个测试:
- 使用指定参数调用工具
- 捕获响应
- 应用验证规则
- 记录PASS/FAIL结果
Reporting
报告格式
Save results to
tests/results/YYYY-MM-DD-HHMMSS.md
See `templates/test-agent.md` for complete template.测试结果会保存为Markdown格式,便于Git版本管理:
markdown
undefinedResults Format
测试结果:feature-name
Test results are saved as markdown for git history:
markdown
undefined日期: 2026-02-02 14:30
提交哈希: abc1234
汇总: 8/9 通过(89%)
Test Results: feature-name
详细结果
Date: 2026-02-02 14:30
Commit: abc1234
Summary: 8/9 passed (89%)
- test_basic_search - 通过(0.3s)
- test_with_filter - 通过(0.4s)
- test_edge_case - 失败
Results
失败测试详情
—
test_edge_case
- test_basic_search - PASSED (0.3s)
- test_with_filter - PASSED (0.4s)
- test_edge_case - FAILED
- 预期: 包含"expected value"
- 实际: 响应为空
- 参数:
{ action: search, query: "" }
保存路径:`tests/results/YYYY-MM-DD-HHMMSS.md`Failed Test Details
工作流
test_edge_case
1. 创建测试规范
- Expected: Contains "expected value"
- Actual: Response was empty
- Params:
{ action: search, query: "" }
Save to: `tests/results/YYYY-MM-DD-HHMMSS.md`yaml
undefinedWorkflow
tests/search.yaml
1. Create Test Specs
—
yaml
undefinedname: 搜索功能测试
defaults:
tool: my_search_tool
tests:
-
name: basic_search params: { query: "hello" } expect: { status: success, count_gte: 0 }
-
name: filtered_search params: { query: "hello", filter: "recent" } expect: { contains: "results" }
undefinedtests/search.yaml
2. 创建测试Agent
name: Search Tests
defaults:
tool: my_search_tool
tests:
-
name: basic_search params: { query: "hello" } expect: { status: success, count_gte: 0 }
-
name: filtered_search params: { query: "hello", filter: "recent" } expect: { contains: "results" }
undefined复制并针对你的领域进行定制。
templates/test-agent.md2. Create Testing Agent
3. 运行测试
Copy and customise for your domain.
templates/test-agent.md"运行搜索测试"
"我的变更完成后测试API"
"为gmail-mcp运行回归测试"3. Run Tests
4. 查看结果
"Run the search tests"
"Test the API after my changes"
"Run regression tests for gmail-mcp"结果保存至目录,提交至Git以保留历史记录:
tests/results/bash
git add tests/results/
git commit -m "测试结果:8/9 通过"4. Review Results
并行测试执行
Results saved to . Commit them for history:
tests/results/bash
git add tests/results/
git commit -m "Test results: 8/9 passed"可同时运行多个测试Agent以加速大型测试套件的执行:
"并行运行以下测试套件:
- Agent 1: tests/auth/*.yaml
- Agent 2: tests/search/*.yaml
- Agent 3: tests/api/*.yaml"每个Agent的特性:
- 拥有独立上下文(不会占用主对话的上下文空间)
- 可独立运行10-50个测试
- 完成后返回汇总结果
- 从父会话继承MCP工具
为何使用并行Agent?
- 在主上下文中运行50个测试会导致上下文耗尽
- 50个测试分配给5个Agent = 上下文清洁 + 执行速度更快
- 每个Agent仅报告通过/失败汇总,而非每个测试的详细信息
批量策略:
- 按功能区域或MCP服务器分组测试
- 每个Agent运行10-20个测试为最佳
- 测试数量过少:启动Agent的开销得不偿失
- 测试数量过多:Agent的上下文会被占满
Parallel Test Execution
MCP测试
Run multiple test agents simultaneously to speed up large test suites:
"Run these test suites in parallel:
- Agent 1: tests/auth/*.yaml
- Agent 2: tests/search/*.yaml
- Agent 3: tests/api/*.yaml"Each agent:
- Has its own context (won't bloat main conversation)
- Can run 10-50 tests independently
- Returns a summary when done
- Inherits MCP tools from parent session
Why parallel agents?
- 50 tests in main context = context exhaustion
- 50 tests across 5 agents = clean context + faster execution
- Each agent reports pass/fail summary, not every test detail
Batching strategy:
- Group tests by feature area or MCP server
- 10-20 tests per agent is ideal
- Too few = overhead of spawning not worth it
- Too many = agent context fills up
对于MCP服务器,测试Agent会继承已配置的MCP:
bash
undefinedMCP Testing
先配置MCP
For MCP servers, the testing agent inherits configured MCPs:
bash
undefinedclaude mcp add --transport http gmail https://gmail.mcp.example.com/mcp
Configure MCP first
然后运行测试
claude mcp add --transport http gmail https://gmail.mcp.example.com/mcp
"为gmail MCP运行测试"
MCP测试规范示例:
```yaml
name: Gmail搜索测试
defaults:
tool: gmail_messages
tests:
- name: search_from_person
params: { action: search, searchQuery: "from John" }
expect: { contains: "from:john" }
- name: search_with_date
params: { action: search, searchQuery: "emails from January 2026" }
expect: { matches: "after:2026" }Then test
API测试
"Run tests for gmail MCP"
Example MCP test spec:
```yaml
name: Gmail Search Tests
defaults:
tool: gmail_messages
tests:
- name: search_from_person
params: { action: search, searchQuery: "from John" }
expect: { contains: "from:john" }
- name: search_with_date
params: { action: search, searchQuery: "emails from January 2026" }
expect: { matches: "after:2026" }对于REST API,可使用Bash工具:
yaml
name: API测试
defaults:
timeout: 5000
tests:
- name: health_check
command: curl -s https://api.example.com/health
expect: { contains: "ok" }
- name: get_user
command: curl -s https://api.example.com/users/1
expect:
json_path: "$.name"
type: stringAPI Testing
浏览器测试
For REST APIs, use Bash tool:
yaml
name: API Tests
defaults:
timeout: 5000
tests:
- name: health_check
command: curl -s https://api.example.com/health
expect: { contains: "ok" }
- name: get_user
command: curl -s https://api.example.com/users/1
expect:
json_path: "$.name"
type: string对于浏览器自动化,可使用Playwright工具:
yaml
name: UI测试
tests:
- name: login_page_loads
steps:
- navigate: https://app.example.com/login
- snapshot: true
expect: { contains: "Sign In" }
- name: form_submission
steps:
- navigate: https://app.example.com/form
- type: { ref: "#email", text: "test@example.com" }
- click: { ref: "button[type=submit]" }
expect: { contains: "Success" }Browser Testing
技巧
For browser automation, use Playwright tools:
yaml
name: UI Tests
tests:
- name: login_page_loads
steps:
- navigate: https://app.example.com/login
- snapshot: true
expect: { contains: "Sign In" }
- name: form_submission
steps:
- navigate: https://app.example.com/form
- type: { ref: "#email", text: "test@example.com" }
- click: { ref: "button[type=submit]" }
expect: { contains: "Success" }- 从冒烟测试开始:基础连通性与认证测试
- 测试边缘场景:空结果、错误、特殊字符
- 使用描述性名称:用而非
search_with_date_filtertest1 - 按功能分组测试:每个功能区域对应一个文件
- 修复Bug后添加测试:每个修复的Bug都要添加回归测试
- 提交测试结果:保留测试运行的历史记录
Tips
本方法的适用边界
- Start with smoke tests: Basic connectivity and auth
- Test edge cases: Empty results, errors, special characters
- Use descriptive names: not
search_with_date_filtertest1 - Group related tests: One file per feature area
- Add after bugs: Every fixed bug gets a regression test
- Commit results: Create history of test runs
- 并非Jest/Vitest的替代品(这些工具适用于单元测试)
- 不强制TDD(使用适合你的方法)
- 并非测试运行器库(Agent本身就是运行器)
- 不涉及模拟测试(我们测试真实系统)
What This Is NOT
适用场景对比
- Not a Jest/Vitest replacement (use those for unit tests)
- Not enforcing TDD (use what works for you)
- Not a test runner library (the agent IS the runner)
- Not about mocking (we test real systems)
| 场景 | 使用本方法 | 使用传统测试 |
|---|---|---|
| MCP服务器验证 | 是 | 否 |
| API集成测试 | 是 | 可配合单元测试 |
| 浏览器工作流测试 | 是 | 可配合组件测试 |
| 单元测试 | 否 | 是(Jest/Vitest) |
| 组件测试 | 否 | 是(Testing Library) |
| 类型检查 | 否 | 是(TypeScript) |
When to Use
相关资源
| Scenario | Use This | Use Traditional Testing |
|---|---|---|
| MCP server validation | Yes | No |
| API integration | Yes | Complement with unit tests |
| Browser workflows | Yes | Complement with component tests |
| Unit testing | No | Yes (Jest/Vitest) |
| Component testing | No | Yes (Testing Library) |
| Type checking | No | Yes (TypeScript) |
- - 通用测试规范模板
templates/test-spec.yaml - - 测试Agent模板
templates/test-agent.md - - 完整的验证规则参考文档
references/validation-rules.md
Related Resources
—
- - Generic test spec template
templates/test-spec.yaml - - Testing agent template
templates/test-agent.md - - Complete validation rule reference
references/validation-rules.md
—