tdd-obra
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseTest-Driven Development (TDD) Skill
测试驱动开发(TDD)Skill
Overview
概述
This skill enforces strict test-first development methodology. The core principle: "Write the test first. Watch it fail. Write minimal code to pass." If you didn't watch the test fail, you don't know if it tests the right thing.
本Skill严格遵循测试先行的开发方法论。核心原则:"先编写测试,看着它失败,再编写最少的代码让测试通过。"如果没有看到测试失败,你就无法确认它是否测试了正确的内容。
Quick Start
快速开始
- Write one failing test - Minimal test demonstrating desired behavior
- Verify RED - Run test, confirm it fails (not errors)
- Write minimal code - Just enough to pass
- Verify GREEN - All tests pass
- Refactor - Clean up while maintaining green
- 编写一个失败的测试 - 能演示预期行为的最简测试
- 验证RED阶段 - 运行测试,确认测试失败(而非报错)
- 编写最少代码 - 仅编写足够让测试通过的代码
- 验证GREEN阶段 - 所有测试通过
- 重构 - 在保持测试全绿的前提下优化代码
When to Use
适用场景
- Starting new features or modules
- Fixing bugs (write failing test reproducing bug first)
- Refactoring existing code
- Any production code development
- Code review verification
- 开发新功能或模块时
- 修复Bug时(先编写能复现Bug的失败测试)
- 重构现有代码时
- 任何生产代码开发场景
- 代码评审验证时
Iron Law
铁则
Produce no production code without a failing test first.
Any code written before tests must be deleted entirely - no exceptions for keeping it as reference material.
未先编写失败的测试,不得产出任何生产代码。
任何先于测试编写的代码必须完全删除——即使作为参考资料也不例外。
Red-Green-Refactor Cycle
红-绿-重构循环
RED Phase
RED阶段
Write one minimal test demonstrating desired behavior:
- Use clear, descriptive test names
- Test real behavior, not mocks
- One assertion focus per test
编写一个能演示预期行为的最简测试:
- 使用清晰、描述性的测试名称
- 测试真实行为,而非模拟行为
- 每个测试仅聚焦一个断言
Verify RED (Mandatory)
验证RED阶段(强制要求)
Run tests and confirm:
- Test fails (not errors)
- Failure message matches expectations
- Failure occurs because feature is missing, not due to typos
运行测试并确认:
- 测试失败(而非报错)
- 失败信息符合预期
- 失败原因是功能缺失,而非拼写错误
GREEN Phase
GREEN阶段
Write simplest code passing the test:
- No over-engineering or feature additions
- No refactoring other code
- Implement only what satisfies the test
编写最简代码使测试通过:
- 不要过度设计或添加额外功能
- 不要重构其他代码
- 仅实现满足测试要求的功能
Verify GREEN (Mandatory)
验证GREEN阶段(强制要求)
Confirm:
- Target test passes
- All existing tests remain passing
- No errors or warnings appear
确认:
- 目标测试通过
- 所有现有测试仍保持通过状态
- 无错误或警告出现
REFACTOR Phase
重构阶段
After green only, clean up implementation:
- Remove duplication
- Improve naming
- Extract helpers
- Maintain all passing tests
仅在测试全绿后,对实现代码进行优化:
- 消除代码重复
- 优化命名
- 提取辅助函数
- 保持所有测试通过
Why Tests-After Fails
事后测试为何失效
| Approach | Validates |
|---|---|
| Tests-first | "What should this do?" |
| Tests-after | "What did I build?" |
Tests written after code:
- Pass immediately, proving nothing
- May test the wrong thing
- Verify implementation rather than behavior
- Miss edge cases forgotten during coding
- Never demonstrate the test catching real bugs
| 方法 | 验证内容 |
|---|---|
| 测试先行 | "这个功能应该做什么?" |
| 事后测试 | "我构建了什么?" |
在代码编写完成后再写测试:
- 测试会立即通过,无法证明任何问题
- 可能测试了错误的内容
- 验证的是实现细节而非行为
- 会遗漏编码过程中忘记的边缘场景
- 永远无法证明测试能发现真实Bug
Common Rationalizations (All Invalid)
常见错误理由(均无效)
These are "Red Flags" requiring complete restart with proper TDD:
- "Too simple to test"
- "I'll test after"
- "Already manually tested"
- "Tests slow me down"
- "I know this works"
以下是需要完全重启并遵循正确TDD流程的"危险信号":
- "功能太简单,无需测试"
- "我之后再写测试"
- "已经手动测试过了"
- "测试拖慢我的进度"
- "我知道这个功能没问题"
Bug Fix Pattern
Bug修复流程
1. Write failing test reproducing the bug
2. Verify test fails for expected reason
3. Implement minimal fix
4. Verify all tests pass
5. Refactor if needed
6. Commit with test and fix together1. 编写能复现Bug的失败测试
2. 验证测试因预期原因失败
3. 编写最简修复代码
4. 验证所有测试通过
5. 如有需要进行重构
6. 将测试与修复代码一起提交Verification Checklist
验证检查清单
Before completing work:
- Every function has a test
- Each test was watched failing first
- Tests failed for expected reasons
- Minimal code passes each test
- All tests pass without warnings
- Tests employ real code with minimal mocking
- Edge cases and errors are covered
完成工作前需确认:
- 每个函数都有对应的测试
- 每个测试都经过了失败验证
- 测试因预期原因失败
- 最简代码即可通过每个测试
- 所有测试无警告通过
- 测试使用真实代码,仅少量模拟
- 覆盖了边缘场景和错误处理
Best Practices
最佳实践
Do
建议
- Write test name as behavior specification
- Use AAA pattern: Arrange, Act, Assert
- Test edge cases explicitly
- Keep tests independent
- Run full suite frequently
- Commit test and implementation together
- 将测试名称写为行为规范
- 使用AAA模式:准备(Arrange)、执行(Act)、断言(Assert)
- 显式测试边缘场景
- 保持测试独立性
- 频繁运行完整测试套件
- 将测试与实现代码一起提交
Don't
不建议
- Write tests after implementation
- Test implementation details
- Use excessive mocking
- Skip verification steps
- Batch multiple behaviors in one test
- Ignore flaky tests
- 在实现完成后再写测试
- 测试实现细节
- 过度使用模拟
- 跳过验证步骤
- 单个测试包含多个行为
- 忽略不稳定测试
Error Handling
错误处理
| Error | Cause | Solution |
|---|---|---|
| Test passes immediately | Wrote code first | Delete code, start over with test |
| Test errors instead of fails | Syntax or setup issue | Fix test setup, not production code |
| Tests pass but feature broken | Testing wrong thing | Review test assertions |
| Flaky tests | Async or state issues | Fix test isolation |
| 错误 | 原因 | 解决方案 |
|---|---|---|
| 测试立即通过 | 先编写了代码 | 删除代码,从编写测试重新开始 |
| 测试报错而非失败 | 语法或设置问题 | 修复测试设置,而非生产代码 |
| 测试通过但功能异常 | 测试了错误的内容 | 检查测试断言 |
| 不稳定测试 | 异步或状态问题 | 修复测试隔离性 |
Metrics
指标
| Metric | Target | Description |
|---|---|---|
| Test-First Rate | 100% | Tests written before code |
| RED Verification | 100% | All tests watched failing |
| Coverage | >80% | Line coverage baseline |
| Critical Path Coverage | 100% | Auth, payments, security |
| Test Independence | 100% | No test order dependencies |
| 指标 | 目标 | 描述 |
|---|---|---|
| 测试先行率 | 100% | 代码编写前先写测试 |
| RED阶段验证率 | 100% | 所有测试都经过失败验证 |
| 代码覆盖率 | >80% | 代码行覆盖率基准 |
| 关键路径覆盖率 | 100% | 认证、支付、安全等关键模块 |
| 测试独立性 | 100% | 测试无执行顺序依赖 |
Related Skills
相关Skill
- systematic-debugging - Debug with methodology
- writing-plans - Plan implementations
- subagent-driven - Structured development
- systematic-debugging - 基于方法论的调试技巧
- writing-plans - 实现方案规划
- subagent-driven - 结构化开发
Version History
版本历史
- 1.0.0 (2026-01-19): Initial release adapted from obra/superpowers
- 1.0.0 (2026-01-19): 初始版本,改编自obra/superpowers