test-driven-development

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Test-Driven Development

测试驱动开发(TDD)

The TDD Cycle

TDD循环

  1. Red: Write a failing test
  2. Green: Write minimal code to pass
  3. Refactor: Improve code while keeping tests green
  1. 红阶段:编写一个会失败的测试
  2. 绿阶段:编写最少的代码使测试通过
  3. 重构阶段:在保持测试通过的前提下优化代码

Workflows

工作流程

  • Write Test: Write a test that describes desired behavior
  • Run Test: Verify it fails (Red)
  • Implement: Write minimal code to pass
  • Run Test: Verify it passes (Green)
  • Refactor: Clean up while tests stay green
  • Repeat: Next test case
  • 编写测试:编写一个描述期望行为的测试
  • 运行测试:确认测试失败(红阶段)
  • 实现代码:编写最少的代码使测试通过
  • 运行测试:确认测试通过(绿阶段)
  • 重构代码:在保持测试通过的前提下清理优化代码
  • 重复执行:进行下一个测试用例

TDD Example

TDD示例

Step 1: Red - Write Failing Test

步骤1:红阶段 - 编写失败的测试

typescript
describe("Calculator", () => {
  test("adds two numbers", () => {
    const calc = new Calculator();
    expect(calc.add(2, 3)).toBe(5);
  });
});

// Run: FAIL - Calculator is not defined
typescript
describe("Calculator", () => {
  test("adds two numbers", () => {
    const calc = new Calculator();
    expect(calc.add(2, 3)).toBe(5);
  });
});

// 运行结果:失败 - Calculator未定义

Step 2: Green - Minimal Implementation

步骤2:绿阶段 - 最简实现

typescript
class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
}

// Run: PASS
typescript
class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
}

// 运行结果:通过

Step 3: Refactor (if needed)

步骤3:重构(如有需要)

Code is already clean, move to next test.
代码已经足够简洁,进入下一个测试。

Step 4: Next Test

步骤4:下一个测试

typescript
test("subtracts two numbers", () => {
  const calc = new Calculator();
  expect(calc.subtract(5, 3)).toBe(2);
});

// Run: FAIL - subtract is not defined
typescript
test("subtracts two numbers", () => {
  const calc = new Calculator();
  expect(calc.subtract(5, 3)).toBe(2);
});

// 运行结果:失败 - subtract未定义

TDD Benefits

TDD的优势

  • Design Feedback: Tests reveal design issues early
  • Documentation: Tests document expected behavior
  • Confidence: Refactor fearlessly with test safety net
  • Focus: One behavior at a time
  • 设计反馈:测试能尽早发现设计问题
  • 文档作用:测试本身就是对期望行为的文档
  • 增强信心:有测试作为安全网,可以放心重构
  • 聚焦目标:一次只关注一个行为

TDD Tips

TDD实践技巧

  1. Start Simple: Begin with the simplest test case
  2. One Assert: Each test should verify one behavior
  3. Descriptive Names: Test names are documentation
  4. No Logic in Tests: Tests should be obvious
  5. Fast Feedback: Tests should run in milliseconds
  1. 从简入手:从最简单的测试用例开始
  2. 单一断言:每个测试只验证一个行为
  3. 命名清晰:测试名称本身就是文档
  4. 测试无逻辑:测试代码应简单易懂
  5. 快速反馈:测试应在毫秒级内完成运行

When to Use TDD

TDD的适用场景

  • New features with clear requirements
  • Bug fixes (write failing test first)
  • Complex business logic
  • API contract development
  • 需求明确的新功能开发
  • Bug修复(先编写失败的测试)
  • 复杂的业务逻辑
  • API契约开发

When TDD is Less Useful

TDD的不适用场景

  • Exploratory/prototype code
  • UI layout changes
  • Simple CRUD operations
  • 探索性/原型代码
  • UI布局调整
  • 简单的CRUD操作