tdd
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCommunity 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工作流
- RED: Write a failing test that defines desired behavior
- GREEN: Write minimal code to make the test pass
- REFACTOR: Clean up code while keeping tests green
- Repeat for each new behavior
- 红(RED):编写一个失败的测试,定义期望的行为
- 绿(GREEN):编写最少的代码使测试通过
- 重构(REFACTOR):在保持测试通过的前提下清理代码
- 为每个新行为重复上述步骤
Rule Categories by Priority
按优先级划分的规则分类
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Red-Green-Refactor Cycle | CRITICAL | |
| 2 | Test Design Principles | CRITICAL | |
| 3 | Test Isolation & Dependencies | HIGH | |
| 4 | Test Data Management | HIGH | |
| 5 | Assertions & Verification | MEDIUM | |
| 6 | Test Organization & Structure | MEDIUM | |
| 7 | Test Performance & Reliability | MEDIUM | |
| 8 | Test Pyramid & Strategy | LOW | |
| 优先级 | 分类 | 影响级别 | 前缀 |
|---|---|---|---|
| 1 | 红-绿-重构循环 | 关键 | |
| 2 | 测试设计原则 | 关键 | |
| 3 | 测试隔离与依赖管理 | 高 | |
| 4 | 测试数据管理 | 高 | |
| 5 | 断言与验证 | 中 | |
| 6 | 测试组织与结构 | 中 | |
| 7 | 测试性能与可靠性 | 中 | |
| 8 | 测试金字塔与策略 | 低 | |
Quick Reference
快速参考
1. Red-Green-Refactor Cycle (CRITICAL)
1. 红-绿-重构循环(关键)
- - Write the Test Before the Implementation
cycle-write-test-first - - Write Only Enough Code to Pass the Test
cycle-minimal-code-to-pass - - Refactor Immediately After Green
cycle-refactor-after-green - - Verify the Test Fails Before Writing Code
cycle-verify-test-fails-first - - Take Small Incremental Steps
cycle-small-increments - - Maintain a Test List
cycle-maintain-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. 测试设计原则(关键)
- - Test Behavior Not Implementation
design-test-behavior-not-implementation - - One Logical Assertion Per Test
design-one-assertion-per-test - - Use Descriptive Test Names
design-descriptive-test-names - - Follow the Arrange-Act-Assert Pattern
design-aaa-pattern - - Test Edge Cases and Boundaries
design-test-edge-cases - - Avoid Logic in Tests
design-avoid-logic-in-tests
- - 测试行为而非实现细节
design-test-behavior-not-implementation - - 每个测试仅包含一个逻辑断言
design-one-assertion-per-test - - 使用描述性的测试名称
design-descriptive-test-names - - 遵循Arrange-Act-Assert模式
design-aaa-pattern - - 测试边界情况与边缘场景
design-test-edge-cases - - 避免在测试中加入业务逻辑
design-avoid-logic-in-tests
3. Test Isolation & Dependencies (HIGH)
3. 测试隔离与依赖管理(高)
- - Mock External Dependencies
isolate-mock-external-dependencies - - Avoid Shared Mutable State Between Tests
isolate-no-shared-state - - Write Deterministic Tests
isolate-deterministic-tests - - Prefer Stubs Over Mocks for Queries
isolate-prefer-stubs-over-mocks - - Use Dependency Injection for Testability
isolate-use-dependency-injection
- - 模拟外部依赖
isolate-mock-external-dependencies - - 避免测试间共享可变状态
isolate-no-shared-state - - 编写确定性测试
isolate-deterministic-tests - - 查询类操作优先使用Stub而非Mock
isolate-prefer-stubs-over-mocks - - 使用依赖注入提升可测试性
isolate-use-dependency-injection
4. Test Data Management (HIGH)
4. 测试数据管理(高)
- - Use Factories for Test Data Creation
data-use-factories - - Keep Test Setup Minimal
data-minimal-setup - - Avoid Mystery Guests
data-avoid-mystery-guests - - Use Unique Identifiers Per Test
data-unique-identifiers - - Use Builder Pattern for Complex Objects
data-builder-pattern
- - 使用工厂模式创建测试数据
data-use-factories - - 保持测试初始化代码最简
data-minimal-setup - - 避免“神秘客”(未声明的外部依赖)
data-avoid-mystery-guests - - 为每个测试使用唯一标识符
data-unique-identifiers - - 使用构建器模式创建复杂对象
data-builder-pattern
5. Assertions & Verification (MEDIUM)
5. 断言与验证(中)
- - Use Specific Assertions
assert-specific-assertions - - Assert on Error Messages and Types
assert-error-messages - - Every Test Must Have Assertions
assert-no-assertions-antipattern - - Create Custom Matchers for Domain Assertions
assert-custom-matchers - - Use Snapshot Testing Judiciously
assert-snapshot-testing
- - 使用特定的断言方法
assert-specific-assertions - - 对错误信息和类型进行断言
assert-error-messages - - 每个测试必须包含断言
assert-no-assertions-antipattern - - 为领域特定断言创建自定义匹配器
assert-custom-matchers - - 谨慎使用快照测试
assert-snapshot-testing
6. Test Organization & Structure (MEDIUM)
6. 测试组织与结构(中)
- - Group Tests by Behavior Not Method
org-group-by-behavior - - Follow Consistent Test File Structure
org-file-structure - - Use Setup and Teardown Hooks Appropriately
org-setup-teardown - - Extract Reusable Test Utilities
org-test-utilities - - Use Parameterized Tests for Variations
org-parameterized-tests
- - 按行为而非方法分组测试
org-group-by-behavior - - 遵循一致的测试文件结构
org-file-structure - - 合理使用初始化与清理钩子
org-setup-teardown - - 提取可复用的测试工具
org-test-utilities - - 使用参数化测试处理多场景用例
org-parameterized-tests
7. Test Performance & Reliability (MEDIUM)
7. 测试性能与可靠性(中)
- - Keep Unit Tests Under 100ms
perf-fast-unit-tests - - Eliminate Network Calls in Unit Tests
perf-avoid-network-calls - - Fix Flaky Tests Immediately
perf-fix-flaky-tests - - Parallelize Independent Tests
perf-parallelize-tests - - Avoid Arbitrary Sleep Calls
perf-avoid-sleep
- - 保持单元测试执行时间在100ms以内
perf-fast-unit-tests - - 单元测试中避免网络调用
perf-avoid-network-calls - - 立即修复不稳定测试
perf-fix-flaky-tests - - 并行执行独立测试
perf-parallelize-tests - - 避免使用任意休眠等待
perf-avoid-sleep
8. Test Pyramid & Strategy (LOW)
8. 测试金字塔与策略(低)
- - Follow the Test Pyramid
strat-test-pyramid - - Use Mutation Testing to Validate Test Quality
strat-mutation-testing - - Set Meaningful Coverage Targets
strat-coverage-targets - - Test Integration at Service Boundaries
strat-integration-boundaries - - Limit E2E Tests to Critical User Paths
strat-e2e-critical-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 skill
vitest - For API mocking with MSW, see skill
msw
- 关于Vitest框架的具体内容,请查看技能
vitest - 关于基于MSW的API模拟,请查看技能
msw
Full Compiled Document
完整编译文档
For the complete guide with all rules expanded:
AGENTS.md包含所有规则详细说明的完整指南:
AGENTS.md