tdd-workflow
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTDD 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 的三大法则
- Write production code only to make a failing test pass
- Write only enough test to demonstrate failure
- Write only enough code to make the test pass
- 仅为修复失败的测试而编写生产代码
- 仅编写足够证明失败的测试代码
- 仅编写足够使测试通过的代码
3. RED Phase Principles
3. 红阶段(RED)原则
What to Write
编写内容
| Focus | Example |
|---|---|
| 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
最简代码
| Principle | Meaning |
|---|---|
| YAGNI | You Aren't Gonna Need It |
| Simplest thing | Write the minimum to pass |
| No optimization | Just 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
优化方向
| Area | Action |
|---|---|
| Duplication | Extract common code |
| Naming | Make intent clear |
| Structure | Improve organization |
| Complexity | Simplify logic |
| 优化领域 | 操作 |
|---|---|
| 代码重复 | 提取公共代码 |
| 命名 | 明确代码意图 |
| 结构 | 优化组织结构 |
| 复杂度 | 简化逻辑 |
REFACTOR Rules
重构规则
- All tests must stay green
- Small incremental changes
- Commit after each refactor
- 所有测试必须保持通过状态
- 采用小步增量式修改
- 每次重构后提交代码
6. AAA Pattern
6. AAA 模式
Every test follows:
| Step | Purpose |
|---|---|
| Arrange | Set up test data |
| Act | Execute code under test |
| Assert | Verify expected outcome |
每个测试都遵循以下步骤:
| 步骤 | 目的 |
|---|---|
| 准备(Arrange) | 设置测试数据 |
| 执行(Act) | 执行待测试代码 |
| 断言(Assert) | 验证预期结果 |
7. When to Use TDD
7. TDD 的适用场景
| Scenario | TDD Value |
|---|---|
| New feature | High |
| Bug fix | High (write test first) |
| Complex logic | High |
| Exploratory | Low (spike, then TDD) |
| UI layout | Low |
| 场景 | TDD 价值 |
|---|---|
| 新功能开发 | 高 |
| Bug 修复 | 高(先写测试) |
| 复杂逻辑实现 | 高 |
| 探索性开发 | 低(先做原型,再用TDD) |
| UI 布局开发 | 低 |
8. Test Prioritization
8. 测试优先级
| Priority | Test Type |
|---|---|
| 1 | Happy path |
| 2 | Error cases |
| 3 | Edge cases |
| 4 | Performance |
| 优先级 | 测试类型 |
|---|---|
| 1 | 正常流程 |
| 2 | 错误情况 |
| 3 | 边界情况 |
| 4 | 性能测试 |
9. Anti-Patterns
9. 反模式
| ❌ Don't | ✅ Do |
|---|---|
| Skip the RED phase | Watch test fail first |
| Write tests after | Write tests before |
| Over-engineer initial | Keep it simple |
| Multiple asserts | One behavior per test |
| Test implementation | Test behavior |
| ❌ 不要做 | ✅ 应该做 |
|---|---|
| 跳过红阶段 | 先确认测试失败 |
| 后写测试 | 先写测试 |
| 过度设计初始代码 | 保持代码简洁 |
| 一个测试多个断言 | 每个测试对应一个行为 |
| 测试实现细节 | 测试行为逻辑 |
10. AI-Augmented TDD
10. AI 增强型 TDD
Multi-Agent Pattern
多智能体模式
| Agent | Role |
|---|---|
| Agent A | Write failing tests (RED) |
| Agent B | Implement to pass (GREEN) |
| Agent C | Optimize (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 | 优化代码(重构阶段) |
记住: 测试就是需求说明书。如果你写不出测试,说明你还没理解需求。