tdd-workflow

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

TDD Workflow

TDD 工作流

Write tests first, code second.

先写测试,再写代码。

1. The TDD Cycle

1. TDD 循环

🔴 RED → Write failing test
🟢 GREEN → Write minimal code to pass
🔵 REFACTOR → Improve code quality
   Repeat...

🔴 RED → 编写失败的测试
🟢 GREEN → 编写最少代码使测试通过
🔵 REFACTOR → 提升代码质量
   重复执行...

2. The Three Laws of TDD

2. TDD 的三大法则

  1. Write production code only to make a failing test pass
  2. Write only enough test to demonstrate failure
  3. Write only enough code to make the test pass

  1. 仅为修复失败的测试而编写生产代码
  2. 仅编写足够证明失败的测试代码
  3. 仅编写足够使测试通过的代码

3. RED Phase Principles

3. 红阶段(RED)原则

What to Write

编写内容

FocusExample
Behavior"should add two numbers"
Edge cases"should handle empty input"
Error states"should throw for invalid data"
关注点示例
行为"应能计算两数之和"
边界情况"应能处理空输入"
错误状态"对无效数据应抛出异常"

RED Phase Rules

红阶段规则

  • Test must fail first
  • Test name describes expected behavior
  • One assertion per test (ideally)

  • 测试必须首先失败
  • 测试名称需描述预期行为
  • 每个测试最好仅包含一个断言

4. GREEN Phase Principles

4. 绿阶段(GREEN)原则

Minimum Code

最简代码

PrincipleMeaning
YAGNIYou Aren't Gonna Need It
Simplest thingWrite the minimum to pass
No optimizationJust make it work
原则含义
YAGNI你不会需要它(You Aren't Gonna Need It)
最简实现编写最少代码使测试通过
不做优化只需让代码正常运行即可

GREEN Phase Rules

绿阶段规则

  • Don't write unneeded code
  • Don't optimize yet
  • Pass the test, nothing more

  • 不要编写多余代码
  • 暂不进行优化
  • 只需让测试通过,无需额外功能

5. REFACTOR Phase Principles

5. 重构阶段(REFACTOR)原则

What to Improve

优化方向

AreaAction
DuplicationExtract common code
NamingMake intent clear
StructureImprove organization
ComplexitySimplify logic
优化领域操作
代码重复提取公共代码
命名明确代码意图
结构优化组织结构
复杂度简化逻辑

REFACTOR Rules

重构规则

  • All tests must stay green
  • Small incremental changes
  • Commit after each refactor

  • 所有测试必须保持通过状态
  • 采用小步增量式修改
  • 每次重构后提交代码

6. AAA Pattern

6. AAA 模式

Every test follows:
StepPurpose
ArrangeSet up test data
ActExecute code under test
AssertVerify expected outcome

每个测试都遵循以下步骤:
步骤目的
准备(Arrange)设置测试数据
执行(Act)执行待测试代码
断言(Assert)验证预期结果

7. When to Use TDD

7. TDD 的适用场景

ScenarioTDD Value
New featureHigh
Bug fixHigh (write test first)
Complex logicHigh
ExploratoryLow (spike, then TDD)
UI layoutLow

场景TDD 价值
新功能开发
Bug 修复高(先写测试)
复杂逻辑实现
探索性开发低(先做原型,再用TDD)
UI 布局开发

8. Test Prioritization

8. 测试优先级

PriorityTest Type
1Happy path
2Error cases
3Edge cases
4Performance

优先级测试类型
1正常流程
2错误情况
3边界情况
4性能测试

9. Anti-Patterns

9. 反模式

❌ Don't✅ Do
Skip the RED phaseWatch test fail first
Write tests afterWrite tests before
Over-engineer initialKeep it simple
Multiple assertsOne behavior per test
Test implementationTest behavior

❌ 不要做✅ 应该做
跳过红阶段先确认测试失败
后写测试先写测试
过度设计初始代码保持代码简洁
一个测试多个断言每个测试对应一个行为
测试实现细节测试行为逻辑

10. AI-Augmented TDD

10. AI 增强型 TDD

Multi-Agent Pattern

多智能体模式

AgentRole
Agent AWrite failing tests (RED)
Agent BImplement to pass (GREEN)
Agent COptimize (REFACTOR)

Remember: The test is the specification. If you can't write a test, you don't understand the requirement.
智能体(Agent)角色
Agent A编写失败的测试(红阶段)
Agent B实现代码使测试通过(绿阶段)
Agent C优化代码(重构阶段)

记住: 测试就是需求说明书。如果你写不出测试,说明你还没理解需求。