tdd

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Community Test-Driven Development Best Practices

社区版测试驱动开发最佳实践

Comprehensive guide to Test-Driven Development practices, designed for AI agents and LLMs. Contains 42 rules across 8 categories, prioritized by impact to guide test writing, refactoring, and code generation.
面向AI Agent和大语言模型的测试驱动开发(TDD)实践综合指南。包含8个分类下的42条规则,按影响优先级排序,用于指导测试编写、代码重构和代码生成。

When to Apply

适用场景

Reference these guidelines when:
  • Writing new tests using TDD workflow
  • Implementing the red-green-refactor cycle
  • Designing test structure and organization
  • Creating test data and fixtures
  • Reviewing or refactoring existing test suites
在以下场景中参考本指南:
  • 使用TDD工作流编写新测试
  • 执行红-绿-重构循环
  • 设计测试结构与组织方式
  • 创建测试数据与测试固件
  • 评审或重构现有测试套件

TDD Workflow

TDD工作流

  1. RED: Write a failing test that defines desired behavior
  2. GREEN: Write minimal code to make the test pass
  3. REFACTOR: Clean up code while keeping tests green
  4. Repeat for each new behavior
  1. 红(RED):编写一个失败的测试,定义期望的行为
  2. 绿(GREEN):编写最少的代码使测试通过
  3. 重构(REFACTOR):在保持测试通过的前提下清理代码
  4. 为每个新行为重复上述步骤

Rule Categories by Priority

按优先级划分的规则分类

PriorityCategoryImpactPrefix
1Red-Green-Refactor CycleCRITICAL
cycle-
2Test Design PrinciplesCRITICAL
design-
3Test Isolation & DependenciesHIGH
isolate-
4Test Data ManagementHIGH
data-
5Assertions & VerificationMEDIUM
assert-
6Test Organization & StructureMEDIUM
org-
7Test Performance & ReliabilityMEDIUM
perf-
8Test Pyramid & StrategyLOW
strat-
优先级分类影响级别前缀
1红-绿-重构循环关键
cycle-
2测试设计原则关键
design-
3测试隔离与依赖管理
isolate-
4测试数据管理
data-
5断言与验证
assert-
6测试组织与结构
org-
7测试性能与可靠性
perf-
8测试金字塔与策略
strat-

Quick Reference

快速参考

1. Red-Green-Refactor Cycle (CRITICAL)

1. 红-绿-重构循环(关键)

  • cycle-write-test-first
    - Write the Test Before the Implementation
  • cycle-minimal-code-to-pass
    - Write Only Enough Code to Pass the Test
  • cycle-refactor-after-green
    - Refactor Immediately After Green
  • cycle-verify-test-fails-first
    - Verify the Test Fails Before Writing Code
  • cycle-small-increments
    - Take Small Incremental Steps
  • cycle-maintain-test-list
    - Maintain a Test List
  • cycle-write-test-first
    - 先编写测试,再实现功能
  • cycle-minimal-code-to-pass
    - 仅编写足够使测试通过的代码
  • cycle-refactor-after-green
    - 测试通过后立即进行重构
  • cycle-verify-test-fails-first
    - 在编写代码前先验证测试会失败
  • cycle-small-increments
    - 采用小步增量式开发
  • cycle-maintain-test-list
    - 维护测试清单

2. Test Design Principles (CRITICAL)

2. 测试设计原则(关键)

  • design-test-behavior-not-implementation
    - Test Behavior Not Implementation
  • design-one-assertion-per-test
    - One Logical Assertion Per Test
  • design-descriptive-test-names
    - Use Descriptive Test Names
  • design-aaa-pattern
    - Follow the Arrange-Act-Assert Pattern
  • design-test-edge-cases
    - Test Edge Cases and Boundaries
  • design-avoid-logic-in-tests
    - Avoid Logic in Tests
  • design-test-behavior-not-implementation
    - 测试行为而非实现细节
  • design-one-assertion-per-test
    - 每个测试仅包含一个逻辑断言
  • design-descriptive-test-names
    - 使用描述性的测试名称
  • design-aaa-pattern
    - 遵循Arrange-Act-Assert模式
  • design-test-edge-cases
    - 测试边界情况与边缘场景
  • design-avoid-logic-in-tests
    - 避免在测试中加入业务逻辑

3. Test Isolation & Dependencies (HIGH)

3. 测试隔离与依赖管理(高)

  • isolate-mock-external-dependencies
    - Mock External Dependencies
  • isolate-no-shared-state
    - Avoid Shared Mutable State Between Tests
  • isolate-deterministic-tests
    - Write Deterministic Tests
  • isolate-prefer-stubs-over-mocks
    - Prefer Stubs Over Mocks for Queries
  • isolate-use-dependency-injection
    - Use Dependency Injection for Testability
  • isolate-mock-external-dependencies
    - 模拟外部依赖
  • isolate-no-shared-state
    - 避免测试间共享可变状态
  • isolate-deterministic-tests
    - 编写确定性测试
  • isolate-prefer-stubs-over-mocks
    - 查询类操作优先使用Stub而非Mock
  • isolate-use-dependency-injection
    - 使用依赖注入提升可测试性

4. Test Data Management (HIGH)

4. 测试数据管理(高)

  • data-use-factories
    - Use Factories for Test Data Creation
  • data-minimal-setup
    - Keep Test Setup Minimal
  • data-avoid-mystery-guests
    - Avoid Mystery Guests
  • data-unique-identifiers
    - Use Unique Identifiers Per Test
  • data-builder-pattern
    - Use Builder Pattern for Complex Objects
  • data-use-factories
    - 使用工厂模式创建测试数据
  • data-minimal-setup
    - 保持测试初始化代码最简
  • data-avoid-mystery-guests
    - 避免“神秘客”(未声明的外部依赖)
  • data-unique-identifiers
    - 为每个测试使用唯一标识符
  • data-builder-pattern
    - 使用构建器模式创建复杂对象

5. Assertions & Verification (MEDIUM)

5. 断言与验证(中)

  • assert-specific-assertions
    - Use Specific Assertions
  • assert-error-messages
    - Assert on Error Messages and Types
  • assert-no-assertions-antipattern
    - Every Test Must Have Assertions
  • assert-custom-matchers
    - Create Custom Matchers for Domain Assertions
  • assert-snapshot-testing
    - Use Snapshot Testing Judiciously
  • assert-specific-assertions
    - 使用特定的断言方法
  • assert-error-messages
    - 对错误信息和类型进行断言
  • assert-no-assertions-antipattern
    - 每个测试必须包含断言
  • assert-custom-matchers
    - 为领域特定断言创建自定义匹配器
  • assert-snapshot-testing
    - 谨慎使用快照测试

6. Test Organization & Structure (MEDIUM)

6. 测试组织与结构(中)

  • org-group-by-behavior
    - Group Tests by Behavior Not Method
  • org-file-structure
    - Follow Consistent Test File Structure
  • org-setup-teardown
    - Use Setup and Teardown Hooks Appropriately
  • org-test-utilities
    - Extract Reusable Test Utilities
  • org-parameterized-tests
    - Use Parameterized Tests for Variations
  • org-group-by-behavior
    - 按行为而非方法分组测试
  • org-file-structure
    - 遵循一致的测试文件结构
  • org-setup-teardown
    - 合理使用初始化与清理钩子
  • org-test-utilities
    - 提取可复用的测试工具
  • org-parameterized-tests
    - 使用参数化测试处理多场景用例

7. Test Performance & Reliability (MEDIUM)

7. 测试性能与可靠性(中)

  • perf-fast-unit-tests
    - Keep Unit Tests Under 100ms
  • perf-avoid-network-calls
    - Eliminate Network Calls in Unit Tests
  • perf-fix-flaky-tests
    - Fix Flaky Tests Immediately
  • perf-parallelize-tests
    - Parallelize Independent Tests
  • perf-avoid-sleep
    - Avoid Arbitrary Sleep Calls
  • perf-fast-unit-tests
    - 保持单元测试执行时间在100ms以内
  • perf-avoid-network-calls
    - 单元测试中避免网络调用
  • perf-fix-flaky-tests
    - 立即修复不稳定测试
  • perf-parallelize-tests
    - 并行执行独立测试
  • perf-avoid-sleep
    - 避免使用任意休眠等待

8. Test Pyramid & Strategy (LOW)

8. 测试金字塔与策略(低)

  • strat-test-pyramid
    - Follow the Test Pyramid
  • strat-mutation-testing
    - Use Mutation Testing to Validate Test Quality
  • strat-coverage-targets
    - Set Meaningful Coverage Targets
  • strat-integration-boundaries
    - Test Integration at Service Boundaries
  • strat-e2e-critical-paths
    - Limit E2E Tests to Critical User Paths
  • strat-test-pyramid
    - 遵循测试金字塔原则
  • strat-mutation-testing
    - 使用突变测试验证测试质量
  • strat-coverage-targets
    - 设置有意义的测试覆盖率目标
  • strat-integration-boundaries
    - 在服务边界处进行集成测试
  • strat-e2e-critical-paths
    - 仅对核心用户路径进行端到端测试

How to Use

使用方式

Read individual reference files for detailed explanations and code examples:
  • Section definitions - Category structure and impact levels
  • Rule template - Template for adding new rules
  • cycle-write-test-first - Write the Test Before the Implementation
  • design-aaa-pattern - Follow the Arrange-Act-Assert Pattern
阅读单独的参考文档获取详细说明与代码示例:
  • Section definitions - 分类结构与影响级别说明
  • Rule template - 添加新规则的模板
  • cycle-write-test-first - 先编写测试,再实现功能
  • design-aaa-pattern - 遵循Arrange-Act-Assert模式

Related Skills

相关技能

  • For Vitest framework specifics, see
    vitest
    skill
  • For API mocking with MSW, see
    msw
    skill
  • 关于Vitest框架的具体内容,请查看
    vitest
    技能
  • 关于基于MSW的API模拟,请查看
    msw
    技能

Full Compiled Document

完整编译文档

For the complete guide with all rules expanded:
AGENTS.md
包含所有规则详细说明的完整指南:
AGENTS.md