tdd
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTDD — Test-Driven Development
TDD — 测试驱动开发
Build features and fix bugs using the test-first red-green-refactor cycle. Tests specify observable behavior through public interfaces — they survive internal refactors.
使用测试优先的红-绿-重构循环来构建功能和修复Bug。测试通过公共接口定义可观察的行为——这类测试在内部重构后依然有效。
Core philosophy
核心理念
Tests should verify behavior through public interfaces, not implementation details.
Good tests read like specifications. They tell you what the system does, not how it does it.
测试应通过公共接口验证行为,而非实现细节。
优秀的测试读起来就像规格说明。它们告诉你系统做什么,而不是怎么做。
When to use this skill
何时使用此技能
- User requests test-first development or red-green-refactor
- Building new features where behavior should drive design
- Fixing bugs where a regression test should come first
- Any work where test design should precede implementation
- 用户要求采用测试优先开发或红-绿-重构模式
- 构建需要由行为驱动设计的新功能
- 修复Bug时需先编写回归测试
- 任何需要先设计测试再进行实现的工作
When not to use this skill
何时不使用此技能
- Writing tests after implementation → use
testing-strategies - Debugging existing failures → use or
debuggingdiagnose - Broad test-policy decisions → use
testing-strategies
- 实现完成后再编写测试 → 使用
testing-strategies - 调试现有故障 → 使用或
debuggingdiagnose - 制定宽泛的测试策略决策 → 使用
testing-strategies
Anti-pattern: horizontal slices
反模式:水平切片
❌ Write all tests upfront before any implementation.
This produces tests that verify imagined behavior, not actual behavior. They become insensitive to real changes because they were written before design decisions were made.
❌ 在任何实现开始前编写所有测试。
这种方式生成的测试验证的是想象中的行为,而非实际的行为。由于它们是在设计决策确定前编写的,对真实的变更不敏感。
Correct approach: vertical slices
正确方法:垂直切片
✅ One test → minimal implementation → repeat.
Each slice is a thin cut through the full behavior. Completed slices are independently demoable.
✅ 编写一个测试 → 最小化实现 → 重复此流程。
每个切片都是对完整行为的一次精简切入。完成的切片可独立进行演示。
Workflow
工作流程
Step 1 — Planning
步骤1 — 规划
Before writing any code:
- Confirm the interface design with the user
- Identify which behaviors matter most (prioritize)
- Get approval on the test approach before coding
在编写任何代码之前:
- 与用户确认接口设计
- 确定最重要的行为(确定优先级)
- 在编码前获得测试方案的批准
Step 2 — Tracer bullet
步骤2 — 探路测试
Write the first test for the most important behavior:
RED: write a failing test for one behavior
GREEN: write the minimum code to make it pass
COMMIT: the behavior is now specified and verifiedThe tracer bullet proves the test infrastructure works and establishes the pattern.
为最重要的行为编写第一个测试:
RED: 为一个行为编写失败的测试
GREEN: 编写最少代码使测试通过
COMMIT: 该行为现已被定义并验证探路测试可验证测试基础设施是否正常工作,并确立后续模式。
Step 3 — Incremental loop
步骤3 — 增量循环
Repeat for each subsequent behavior:
RED → GREEN → (optional REFACTOR) → next REDRules:
- Write only enough code to pass the current test
- Do not anticipate future requirements
- Each test must fail before the implementation exists
- Each test must pass after the minimal implementation
为每个后续行为重复此流程:
RED → GREEN →(可选 REFACTOR)→ 下一个 RED规则:
- 仅编写足够通过当前测试的代码
- 不要预判未来需求
- 每个测试在实现前必须失败
- 每个测试在最小化实现后必须通过
Step 4 — Refactor
步骤4 — 重构
After all behaviors are tested and passing:
- Extract duplication into shared helpers
- Deepen modules (simple interface, rich behavior)
- Run tests after each refactor step
- Never refactor while tests are red
在所有行为都经过测试并通过后:
- 将重复代码提取到共享工具中
- 深化模块(简洁接口,丰富行为)
- 每一步重构后运行测试
- 测试失败时绝不要进行重构
Per-cycle checklist
循环检查清单
Before moving to the next cycle, verify the test:
- Describes observable behavior (not implementation internals)
- Uses only public interfaces
- Would survive an internal refactor of the implementation
- Fails for the right reason before implementation
- Passes with minimal, non-speculative code
进入下一个循环前,验证测试是否符合以下要求:
- 描述可观察的行为(而非实现内部细节)
- 仅使用公共接口
- 在实现内部重构后依然有效
- 在实现前因正确的原因失败
- 通过最少的、非投机性代码即可通过
Example
示例
typescript
// RED: write the failing test first
test('formats currency with symbol', () => {
expect(formatCurrency(1000, 'USD')).toBe('$1,000.00')
})
// GREEN: write minimal passing code
function formatCurrency(amount: number, currency: string): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
}).format(amount)
}
// REFACTOR if needed, then move to next behaviortypescript
// RED: 先编写失败的测试
test('formats currency with symbol', () => {
expect(formatCurrency(1000, 'USD')).toBe('$1,000.00')
})
// GREEN: 编写最小化的可通过代码
function formatCurrency(amount: number, currency: string): string {
return new Intl.NumberFormat('en-US', {
style: 'currency',
currency,
}).format(amount)
}
// REFACTOR(如有需要),然后转向下一个行为Instructions
说明
- Identify the task trigger and expected output.
- Follow the workflow steps in this skill from top to bottom.
- Validate outputs before moving to the next step.
- Capture blockers and fallback path if any step fails.
- 识别任务触发条件和预期输出。
- 从上到下遵循本技能中的工作流程步骤。
- 进入下一步前验证输出。
- 记录阻塞点,若某步骤失败则执行回退路径。
Examples
示例
- Example: Apply this skill to a small scope first, then scale to full scope after validation passes.
- 示例:先将此技能应用于小范围场景,验证通过后再扩展到完整范围。
Best practices
最佳实践
- Keep outputs deterministic and auditable.
- Prefer small reversible changes over broad risky edits.
- Record assumptions explicitly.
- 保持输出的确定性和可审计性。
- 优先选择小规模可逆变更,而非大范围高风险编辑。
- 明确记录假设条件。
References
参考资料
- Project standards:
.agent-skills/skill-standardization/SKILL.md - Validator script:
.agent-skills/skill-standardization/scripts/validate_skill.sh
- 项目标准:
.agent-skills/skill-standardization/SKILL.md - 验证脚本:
.agent-skills/skill-standardization/scripts/validate_skill.sh