tdd

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test-Driven Development

测试驱动开发

Philosophy

理念

Tests verify behavior through public interfaces, not implementation. Good tests survive refactors.
See
principles.md
for testing philosophy and anti-patterns.
测试通过公共接口验证行为,而非实现细节。优秀的测试在重构后仍能正常运行。
有关测试理念及反模式,请查看
principles.md

Workflow

工作流

1. Planning

1. 规划

  • Confirm interface design with user
  • List behaviors to test (prioritize critical paths)
  • Get approval before writing code
  • 与用户确认接口设计
  • 列出需要测试的行为(优先处理关键路径)
  • 编写代码前获得批准

2. Tracer Bullet

2. 探路测试

RED:   Write first test → fails
GREEN: Minimal code to pass → passes
RED:   Write first test → fails
GREEN: Minimal code to pass → passes

3. Incremental Loop

3. 增量循环

For each remaining behavior:
RED:   Write next test → fails
GREEN: Minimal code to pass → passes
Rules:
  • One test at a time
  • Minimal code to pass
  • No refactoring while RED
针对每个剩余的行为:
RED:   Write next test → fails
GREEN: Minimal code to pass → passes
规则:
  • 一次只编写一个测试
  • 编写刚好能通过测试的最简代码
  • 处于RED阶段时不进行重构

4. Refactor

4. 重构

Once all tests GREEN:
  • Remove duplication
  • Improve structure
  • Tests must stay GREEN
当所有测试都处于GREEN状态时:
  • 消除代码重复
  • 优化代码结构
  • 确保测试始终保持GREEN状态

Anti-Pattern: Horizontal Slices

反模式:横向切片

DO NOT write all tests first, then all implementation. DO use vertical slices: one test → one implementation → repeat.
See
examples.md
for workflow demonstrations.
请勿先编写所有测试,再编写全部实现代码。 采用纵向切片方式:编写一个测试 → 实现对应功能 → 重复此流程。
有关工作流演示,请查看
examples.md