Test-Driven Development Workflow
Ensure all code development follows TDD principles with 80%+ code coverage.
When to Activate
- - Writing new features or functionality
- Fixing bugs or issues
- Refactoring existing code
- User says "write tests", "add specs", "how should I test this"
Quick Start
- 1. Write user journey in acceptance-test format ("As a [role], I want...")
- Generate test cases (happy path + edge cases + errors)
- Run tests → verify RED state (must fail before implementation)
- Implement minimal code to make tests pass → GREEN state
- Refactor while keeping tests green
- Verify 80%+ coverage achieved
Key Concepts
- - Tests before code — Write tests first, then implementation
- RED-GREEN-REFACTOR — Fail → Pass → Improve (no exceptions)
- 80% coverage minimum — Unit, integration, and E2E combined
- Unit-level isolation — Mock dependencies, test behavior not implementation
- Independent tests — No test ordering dependencies; each can run solo
Common Usage
Most frequent patterns:
- - Unit tests for functions and components
- Integration tests for API endpoints and database operations
- E2E tests for critical user flows
- Edge case and error path testing
- Fast execution (unit tests < 50ms each)
References
- -
references/workflow-steps.md — Detailed 7-step TDD cycle with git checkpoints - INLINECODE1 — Test patterns, common mistakes, success metrics
测试驱动开发工作流程
确保所有代码开发遵循TDD原则,代码覆盖率不低于80%。
何时激活
- - 编写新功能或新特性时
- 修复缺陷或问题时
- 重构现有代码时
- 用户提出编写测试、添加规格说明、该如何测试这个时
快速入门
- 1. 以验收测试格式编写用户旅程(作为[角色],我希望...)
- 生成测试用例(正常路径 + 边界情况 + 错误场景)
- 运行测试 → 验证RED状态(实现前必须失败)
- 编写最少代码使测试通过 → GREEN状态
- 在保持测试通过的前提下进行重构
- 验证覆盖率是否达到80%以上
核心概念
- - 先写测试再写代码 — 先编写测试,再进行实现
- 红-绿-重构 — 失败 → 通过 → 改进(无例外)
- 最低80%覆盖率 — 单元测试、集成测试和端到端测试合计
- 单元级隔离 — 模拟依赖关系,测试行为而非实现
- 独立测试 — 无测试顺序依赖;每个测试可独立运行
常见用法
最常用的模式:
- - 函数和组件的单元测试
- API端点和数据库操作的集成测试
- 关键用户流程的端到端测试
- 边界情况和错误路径测试
- 快速执行(每个单元测试 < 50ms)
参考资料
- - references/workflow-steps.md — 包含git检查点的详细7步TDD循环
- references/patterns-and-best-practices.md — 测试模式、常见错误、成功指标