tdd-workflows-tdd-cycle

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Use this skill when

适用本技能的场景

  • Working on tdd workflows tdd cycle tasks or workflows
  • Needing guidance, best practices, or checklists for tdd workflows tdd cycle
  • 处理TDD工作流、TDD循环相关任务或工作流程时
  • 需要TDD工作流、TDD循环相关的指导、最佳实践或检查清单时

Do not use this skill when

不适用本技能的场景

  • The task is unrelated to tdd workflows tdd cycle
  • You need a different domain or tool outside this scope
  • 任务与TDD工作流、TDD循环无关时
  • 需要该范围之外的其他领域或工具时

Instructions

操作说明

  • Clarify goals, constraints, and required inputs.
  • Apply relevant best practices and validate outcomes.
  • Provide actionable steps and verification.
  • If detailed examples are required, open
    resources/implementation-playbook.md
    .
Execute a comprehensive Test-Driven Development (TDD) workflow with strict red-green-refactor discipline:
[Extended thinking: This workflow enforces test-first development through coordinated agent orchestration. Each phase of the TDD cycle is strictly enforced with fail-first verification, incremental implementation, and continuous refactoring. The workflow supports both single test and test suite approaches with configurable coverage thresholds.]
  • 明确目标、约束条件和所需输入。
  • 应用相关最佳实践并验证结果。
  • 提供可执行的步骤和验证方式。
  • 若需要详细示例,请打开
    resources/implementation-playbook.md
严格遵循红-绿-重构(RED-GREEN-REFACTOR)准则,执行完整的测试驱动开发(TDD)工作流:
[拓展思考:该工作流通过协调Agent编排,强制要求先编写测试再进行开发。TDD循环的每个阶段都通过“先失败验证”、“增量实现”和“持续重构”严格执行。工作流支持单测试和测试套件两种方式,并可配置覆盖率阈值。]

Configuration

配置

Coverage Thresholds

覆盖率阈值

  • Minimum line coverage: 80%
  • Minimum branch coverage: 75%
  • Critical path coverage: 100%
  • 最低行覆盖率:80%
  • 最低分支覆盖率:75%
  • 关键路径覆盖率:100%

Refactoring Triggers

重构触发条件

  • Cyclomatic complexity > 10
  • Method length > 20 lines
  • Class length > 200 lines
  • Duplicate code blocks > 3 lines
  • 圈复杂度 > 10
  • 方法长度 > 20行
  • 类长度 > 200行
  • 重复代码块 > 3行

Phase 1: Test Specification and Design

阶段1:测试规格与设计

1. Requirements Analysis

1. 需求分析

  • Use Task tool with subagent_type="comprehensive-review::architect-review"
  • Prompt: "Analyze requirements for: $ARGUMENTS. Define acceptance criteria, identify edge cases, and create test scenarios. Output a comprehensive test specification."
  • Output: Test specification, acceptance criteria, edge case matrix
  • Validation: Ensure all requirements have corresponding test scenarios
  • 使用Task工具,设置subagent_type="comprehensive-review::architect-review"
  • 提示词:“分析以下需求:$ARGUMENTS。定义验收标准,识别边缘情况,创建测试场景。输出完整的测试规格说明。”
  • 输出:测试规格说明、验收标准、边缘情况矩阵
  • 验证:确保所有需求都有对应的测试场景

2. Test Architecture Design

2. 测试架构设计

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Design test architecture for: $ARGUMENTS based on test specification. Define test structure, fixtures, mocks, and test data strategy. Ensure testability and maintainability."
  • Output: Test architecture, fixture design, mock strategy
  • Validation: Architecture supports isolated, fast, reliable tests
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“基于测试规格说明,为$ARGUMENTS设计测试架构。定义测试结构、测试夹具、模拟对象(mocks)和测试数据策略。确保可测试性和可维护性。”
  • 输出:测试架构、夹具设计、模拟策略
  • 验证:架构支持隔离、快速、可靠的测试

Phase 2: RED - Write Failing Tests

阶段2:红(RED)- 编写失败的测试

3. Write Unit Tests (Failing)

3. 编写单元测试(预期失败)

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Write FAILING unit tests for: $ARGUMENTS. Tests must fail initially. Include edge cases, error scenarios, and happy paths. DO NOT implement production code."
  • Output: Failing unit tests, test documentation
  • CRITICAL: Verify all tests fail with expected error messages
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“为$ARGUMENTS编写预期失败的单元测试。测试必须初始执行失败。包含边缘情况、错误场景和正常流程。请勿实现生产代码。”
  • 输出:预期失败的单元测试、测试文档
  • 关键要求:验证所有测试都能输出预期错误信息并失败

4. Verify Test Failure

4. 验证测试失败状态

  • Use Task tool with subagent_type="tdd-workflows::code-reviewer"
  • Prompt: "Verify that all tests for: $ARGUMENTS are failing correctly. Ensure failures are for the right reasons (missing implementation, not test errors). Confirm no false positives."
  • Output: Test failure verification report
  • GATE: Do not proceed until all tests fail appropriately
  • 使用Task工具,设置subagent_type="tdd-workflows::code-reviewer"
  • 提示词:“验证$ARGUMENTS的所有测试是否正确失败。确保失败原因合理(缺少实现,而非测试本身错误)。确认没有假阳性结果。”
  • 输出:测试失败验证报告
  • 关卡要求:所有测试未按预期失败前,不得进入下一阶段

Phase 3: GREEN - Make Tests Pass

阶段3:绿(GREEN)- 使测试通过

5. Minimal Implementation

5. 最小化实现

  • Use Task tool with subagent_type="backend-development::backend-architect"
  • Prompt: "Implement MINIMAL code to make tests pass for: $ARGUMENTS. Focus only on making tests green. Do not add extra features or optimizations. Keep it simple."
  • Output: Minimal working implementation
  • Constraint: No code beyond what's needed to pass tests
  • 使用Task工具,设置subagent_type="backend-development::backend-architect"
  • 提示词:“编写最小化代码使$ARGUMENTS的测试通过。仅专注于让测试变绿。不要添加额外功能或优化。保持代码简洁。”
  • 输出:可运行的最小化实现代码
  • 约束:代码不得超出通过测试所需的范围

6. Verify Test Success

6. 验证测试通过状态

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Run all tests for: $ARGUMENTS and verify they pass. Check test coverage metrics. Ensure no tests were accidentally broken."
  • Output: Test execution report, coverage metrics
  • GATE: All tests must pass before proceeding
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“运行$ARGUMENTS的所有测试并验证是否通过。检查测试覆盖率指标。确保没有测试意外被破坏。”
  • 输出:测试执行报告、覆盖率指标
  • 关卡要求:所有测试通过后,方可进入下一阶段

Phase 4: REFACTOR - Improve Code Quality

阶段4:重构(REFACTOR)- 提升代码质量

7. Code Refactoring

7. 代码重构

  • Use Task tool with subagent_type="tdd-workflows::code-reviewer"
  • Prompt: "Refactor implementation for: $ARGUMENTS while keeping tests green. Apply SOLID principles, remove duplication, improve naming, and optimize performance. Run tests after each refactoring."
  • Output: Refactored code, refactoring report
  • Constraint: Tests must remain green throughout
  • 使用Task工具,设置subagent_type="tdd-workflows::code-reviewer"
  • 提示词:“在保持测试通过的前提下,重构$ARGUMENTS的实现代码。应用SOLID原则,消除重复代码,优化命名,提升性能。每次重构后运行测试。”
  • 输出:重构后的代码、重构报告
  • 约束:重构全程需保持测试通过

8. Test Refactoring

8. 测试代码重构

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Refactor tests for: $ARGUMENTS. Remove test duplication, improve test names, extract common fixtures, and enhance test readability. Ensure tests still provide same coverage."
  • Output: Refactored tests, improved test structure
  • Validation: Coverage metrics unchanged or improved
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“重构$ARGUMENTS的测试代码。消除测试重复,优化测试命名,提取通用夹具,提升测试可读性。确保测试覆盖率保持不变。”
  • 输出:重构后的测试代码、优化后的测试结构
  • 验证:覆盖率指标未下降或有所提升

Phase 5: Integration and System Tests

阶段5:集成与系统测试

9. Write Integration Tests (Failing First)

9. 编写集成测试(先失败)

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Write FAILING integration tests for: $ARGUMENTS. Test component interactions, API contracts, and data flow. Tests must fail initially."
  • Output: Failing integration tests
  • Validation: Tests fail due to missing integration logic
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“为$ARGUMENTS编写预期失败的集成测试。测试组件交互、API契约和数据流。测试必须初始执行失败。”
  • 输出:预期失败的集成测试
  • 验证:测试因缺少集成逻辑而失败

10. Implement Integration

10. 实现集成逻辑

  • Use Task tool with subagent_type="backend-development::backend-architect"
  • Prompt: "Implement integration code for: $ARGUMENTS to make integration tests pass. Focus on component interaction and data flow."
  • Output: Integration implementation
  • Validation: All integration tests pass
  • 使用Task工具,设置subagent_type="backend-development::backend-architect"
  • 提示词:“实现$ARGUMENTS的集成代码,使集成测试通过。专注于组件交互和数据流。”
  • 输出:集成实现代码
  • 验证:所有集成测试通过

Phase 6: Continuous Improvement Cycle

阶段6:持续改进循环

11. Performance and Edge Case Tests

11. 性能与边缘场景测试

  • Use Task tool with subagent_type="unit-testing::test-automator"
  • Prompt: "Add performance tests and additional edge case tests for: $ARGUMENTS. Include stress tests, boundary tests, and error recovery tests."
  • Output: Extended test suite
  • Metric: Increased test coverage and scenario coverage
  • 使用Task工具,设置subagent_type="unit-testing::test-automator"
  • 提示词:“为$ARGUMENTS添加性能测试和额外边缘场景测试。包含压力测试、边界测试和错误恢复测试。”
  • 输出:扩展后的测试套件
  • 指标:提升测试覆盖率和场景覆盖率

12. Final Code Review

12. 最终代码评审

  • Use Task tool with subagent_type="comprehensive-review::architect-review"
  • Prompt: "Perform comprehensive review of: $ARGUMENTS. Verify TDD process was followed, check code quality, test quality, and coverage. Suggest improvements."
  • Output: Review report, improvement suggestions
  • Action: Implement critical suggestions while maintaining green tests
  • 使用Task工具,设置subagent_type="comprehensive-review::architect-review"
  • 提示词:“对$ARGUMENTS进行全面评审。验证是否遵循TDD流程,检查代码质量、测试质量和覆盖率。提出改进建议。”
  • 输出:评审报告、改进建议
  • 操作:在保持测试通过的前提下,实施关键改进建议

Incremental Development Mode

增量开发模式

For test-by-test development:
  1. Write ONE failing test
  2. Make ONLY that test pass
  3. Refactor if needed
  4. Repeat for next test
Use this approach by adding
--incremental
flag to focus on one test at a time.
针对逐测试用例开发:
  1. 编写一个预期失败的测试用例
  2. 仅实现使该测试用例通过的代码
  3. 如有需要进行重构
  4. 为下一个测试用例重复上述步骤
添加
--incremental
参数即可启用该模式,专注于逐个处理测试用例。

Test Suite Mode

测试套件模式

For comprehensive test suite development:
  1. Write ALL tests for a feature/module (failing)
  2. Implement code to pass ALL tests
  3. Refactor entire module
  4. Add integration tests
Use this approach by adding
--suite
flag for batch test development.
针对全面测试套件开发:
  1. 为某个功能/模块编写所有预期失败的测试用例
  2. 实现代码使所有测试用例通过
  3. 重构整个模块
  4. 添加集成测试
添加
--suite
参数即可启用该模式,进行批量测试开发。

Validation Checkpoints

验证检查点

RED Phase Validation

红阶段验证

  • All tests written before implementation
  • All tests fail with meaningful error messages
  • Test failures are due to missing implementation
  • No test passes accidentally
  • 所有测试在实现前编写完成
  • 所有测试输出有意义的错误信息并失败
  • 测试失败原因是缺少实现
  • 无测试意外通过

GREEN Phase Validation

绿阶段验证

  • All tests pass
  • No extra code beyond test requirements
  • Coverage meets minimum thresholds
  • No test was modified to make it pass
  • 所有测试通过
  • 无超出测试要求的额外代码
  • 覆盖率达到最低阈值
  • 未修改测试用例使其通过

REFACTOR Phase Validation

重构阶段验证

  • All tests still pass after refactoring
  • Code complexity reduced
  • Duplication eliminated
  • Performance improved or maintained
  • Test readability improved
  • 重构后所有测试仍通过
  • 代码复杂度降低
  • 重复代码已消除
  • 性能保持或提升
  • 测试可读性提升

Coverage Reports

覆盖率报告

Generate coverage reports after each phase:
  • Line coverage
  • Branch coverage
  • Function coverage
  • Statement coverage
每个阶段后生成覆盖率报告:
  • 行覆盖率
  • 分支覆盖率
  • 函数覆盖率
  • 语句覆盖率

Failure Recovery

故障恢复

If TDD discipline is broken:
  1. STOP immediately
  2. Identify which phase was violated
  3. Rollback to last valid state
  4. Resume from correct phase
  5. Document lesson learned
若违反TDD准则:
  1. 立即停止当前操作
  2. 确定违反了哪个阶段的要求
  3. 回滚到上一个有效状态
  4. 从正确的阶段恢复执行
  5. 记录经验教训

TDD Metrics Tracking

TDD指标跟踪

Track and report:
  • Time in each phase (Red/Green/Refactor)
  • Number of test-implementation cycles
  • Coverage progression
  • Refactoring frequency
  • Defect escape rate
跟踪并报告以下指标:
  • 每个阶段的耗时(红/绿/重构)
  • 测试-实现循环次数
  • 覆盖率提升进度
  • 重构频率
  • 缺陷逃逸率

Anti-Patterns to Avoid

需避免的反模式

  • Writing implementation before tests
  • Writing tests that already pass
  • Skipping the refactor phase
  • Writing multiple features without tests
  • Modifying tests to make them pass
  • Ignoring failing tests
  • Writing tests after implementation
  • 先实现代码再编写测试
  • 编写原本就会通过的测试
  • 跳过重构阶段
  • 未编写测试就开发多个功能
  • 修改测试用例使其通过
  • 忽略失败的测试
  • 实现代码后再编写测试

Success Criteria

成功标准

  • 100% of code written test-first
  • All tests pass continuously
  • Coverage exceeds thresholds
  • Code complexity within limits
  • Zero defects in covered code
  • Clear test documentation
  • Fast test execution (< 5 seconds for unit tests)
  • 100%的代码遵循“先测试后编写”原则
  • 所有测试持续保持通过状态
  • 覆盖率超过阈值
  • 代码复杂度在限制范围内
  • 已覆盖的代码零缺陷
  • 测试文档清晰明了
  • 测试执行速度快(单元测试耗时<5秒)

Notes

注意事项

  • Enforce strict RED-GREEN-REFACTOR discipline
  • Each phase must be completed before moving to next
  • Tests are the specification
  • If a test is hard to write, the design needs improvement
  • Refactoring is NOT optional
  • Keep test execution fast
  • Tests should be independent and isolated
TDD implementation for: $ARGUMENTS
  • 严格执行红-绿-重构(RED-GREEN-REFACTOR)准则
  • 每个阶段必须完成后才能进入下一个阶段
  • 测试即规格说明
  • 若某个测试难以编写,说明设计需要优化
  • 重构不是可选步骤
  • 保持测试执行速度
  • 测试应独立且相互隔离
针对$ARGUMENTS的TDD实现