testing-patterns

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing 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:
  1. Test in production/staging with good logging
  2. Use agents to run tests (keeps main context clean)
  3. Define tests declaratively in YAML (human-readable, version-controlled)
  4. Focus on integration (real servers, real data)
并非传统的TDD测试方法,而是采用以下思路:
  1. 在生产/预发布环境测试,配合完善的日志系统
  2. 使用Agent运行测试(保持主上下文清洁)
  3. 通过YAML声明式定义测试(易读、可版本控制)
  4. 聚焦集成测试(真实服务器、真实数据)

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

命令列表

CommandPurpose
/create-tests
Discover project, generate test specs + testing agent
/run-tests
Execute tests via agent(s), report results
/coverage
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%
命令用途
/create-tests
发现项目,生成测试规范 + 测试Agent
/run-tests
通过Agent执行测试,报告结果
/coverage
生成覆盖率报告,识别未覆盖的代码路径
快速工作流:
/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":
  1. Discovery - Claude examines the project:
    • What MCP servers are configured?
    • What APIs or tools exist?
    • What does the code do?
  2. 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
  3. Structure - Using patterns from this skill:
    • YAML specs in
      tests/
      directory
    • Optional testing agent in
      .claude/agents/
    • Results saved to
      tests/results/
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会根据你的项目上下文设计具体的测试用例。
当你请求"为该项目创建测试"时的流程:
  1. 项目发现 - Claude会分析项目:
    • 配置了哪些MCP服务器?
    • 存在哪些API或工具?
    • 代码的功能是什么?
  2. 测试设计 - Claude创建项目专属测试:
    • 针对实际工具/端点的测试用例
    • 基于真实行为的预期值
    • 与领域相关的边缘场景
  3. 结构搭建 - 采用本技能提供的模式:
    • 测试规范存储在
      tests/
      目录下的YAML文件中
    • 可选的测试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 validate
yaml
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
undefined
defaults: 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
undefined

Validation Rules

验证规则

RuleDescriptionExample
contains
Response contains string
contains: "from:john"
not_contains
Response doesn't contain
not_contains: "error"
matches
Regex pattern match
matches: "after:\\d{4}"
json_path
Check value at JSON path
json_path: "$.results[0].name"
equals
Exact value match
equals: "success"
status
Check success/error
status: success
count_gte
Array length >= N
count_gte: 1
count_eq
Array length == N
count_eq: 5
type
Value type check
type: array
See
references/validation-rules.md
for complete documentation.
规则描述示例
contains
响应包含指定字符串
contains: "from:john"
not_contains
响应不包含指定字符串
not_contains: "error"
matches
匹配正则表达式
matches: "after:\\d{4}"
json_path
检查JSON路径对应的值
json_path: "$.results[0].name"
equals
完全匹配值
equals: "success"
status
检查成功/错误状态
status: success
count_gte
数组长度≥N
count_gte: 1
count_eq
数组长度==N
count_eq: 5
type
检查值的类型
type: array
完整文档请参考
references/validation-rules.md

Creating a Testing Agent

创建测试Agent

Testing agents inherit MCP tools from the session. Create an agent that:
  1. Reads YAML test specs
  2. Executes tool calls with params
  3. Validates responses against expectations
  4. Reports results
测试Agent会从会话中继承MCP工具。创建的Agent需要具备以下能力:
  1. 读取YAML测试规范
  2. 使用参数执行工具调用
  3. 根据预期验证响应
  4. 报告测试结果

Agent Template

Agent模板

CRITICAL: Do NOT specify a
tools
field if you need MCP access. When you specify ANY tools, it becomes an allowlist and
"*"
is interpreted literally (not as a wildcard). Omit
tools
entirely to inherit ALL tools from the parent session.
yaml
---
name: my-tester
description: |
  Tests [domain] functionality. Reads YAML test specs and validates responses.
  Use when: testing after changes, running regression tests.
重要提示:如果需要访问MCP,请不要指定
tools
字段。当你指定任何工具时,它会被视为允许列表,
"*"
会被解释为字面量而非通配符。完全省略
tools
字段即可从父会话继承所有工具。
yaml
---
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

工作原理

  1. Find test specs:
    tests/*.yaml
  2. Parse and execute each test
  3. Validate responses
  4. Report pass/fail summary
  1. 查找测试规范:
    tests/*.yaml
  2. 解析并执行每个测试
  3. 验证响应是否符合预期
  4. 报告通过/失败汇总

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:
  1. Call tool with params
  2. Capture response
  3. Apply validation rules
  4. Record PASS/FAIL
对于每个测试:
  1. 使用指定参数调用工具
  2. 捕获响应
  3. 应用验证规则
  4. 记录PASS/FAIL结果

Reporting

报告格式

Save results to
tests/results/YYYY-MM-DD-HHMMSS.md

See `templates/test-agent.md` for complete template.
测试结果会保存为Markdown格式,便于Git版本管理:
markdown
undefined

Results 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
undefined

Workflow

tests/search.yaml

1. Create Test Specs

yaml
undefined
name: 搜索功能测试 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

tests/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.md
并针对你的领域进行定制。

2. Create Testing Agent

3. 运行测试

Copy
templates/test-agent.md
and customise for your domain.
"运行搜索测试"
"我的变更完成后测试API"
"为gmail-mcp运行回归测试"

3. Run Tests

4. 查看结果

"Run the search tests"
"Test the API after my changes"
"Run regression tests for gmail-mcp"
结果保存至
tests/results/
目录,提交至Git以保留历史记录:
bash
git add tests/results/
git commit -m "测试结果:8/9 通过"

4. Review Results

并行测试执行

Results saved to
tests/results/
. Commit them for history:
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
undefined

MCP Testing

先配置MCP

For MCP servers, the testing agent inherits configured MCPs:
bash
undefined
claude 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: string

API 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" }
  1. 从冒烟测试开始:基础连通性与认证测试
  2. 测试边缘场景:空结果、错误、特殊字符
  3. 使用描述性名称:用
    search_with_date_filter
    而非
    test1
  4. 按功能分组测试:每个功能区域对应一个文件
  5. 修复Bug后添加测试:每个修复的Bug都要添加回归测试
  6. 提交测试结果:保留测试运行的历史记录

Tips

本方法的适用边界

  1. Start with smoke tests: Basic connectivity and auth
  2. Test edge cases: Empty results, errors, special characters
  3. Use descriptive names:
    search_with_date_filter
    not
    test1
  4. Group related tests: One file per feature area
  5. Add after bugs: Every fixed bug gets a regression test
  6. 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

相关资源

ScenarioUse ThisUse Traditional Testing
MCP server validationYesNo
API integrationYesComplement with unit tests
Browser workflowsYesComplement with component tests
Unit testingNoYes (Jest/Vitest)
Component testingNoYes (Testing Library)
Type checkingNoYes (TypeScript)
  • templates/test-spec.yaml
    - 通用测试规范模板
  • templates/test-agent.md
    - 测试Agent模板
  • references/validation-rules.md
    - 完整的验证规则参考文档

Related Resources

  • templates/test-spec.yaml
    - Generic test spec template
  • templates/test-agent.md
    - Testing agent template
  • references/validation-rules.md
    - Complete validation rule reference