tdd
Test-Driven Development patterns and red-green-refactor workflow. Auto-triggers when writing tests first, implementing TDD, or discussing test coverage.
安装与使用
请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。
查看来源 ↗使用指南技能说明
Test-Driven Development
The Red-Green-Refactor Cycle
- RED: Write a failing test that defines expected behavior
- GREEN: Write minimal code to make the test pass
- REFACTOR: Improve code quality while keeping tests green
Critical Rules
- Never write production code without a failing test first
- Only write enough code to make the current test pass
- Refactor only when tests are green
- Run tests after every change
Test Structure (AAA Pattern)
// Arrange - Set up test data and conditions
// Act - Execute the code under test
// Assert - Verify the expected outcome
Test Naming Convention
test_[unit]_[scenario]_[expected_result]
Examples:
- test_calculateTotal_emptyCart_returnsZero
- test_userLogin_invalidPassword_throwsAuthError
- test_parseDate_invalidFormat_returnsNull
Test Doubles
| Type | Purpose | Use When |
|---|---|---|
| Stub | Returns canned data | Testing with specific inputs |
| Mock | Verifies interactions | Checking method calls |
| Fake | Working implementation | Need realistic behavior |
| Spy | Records calls | Observing side effects |
Coverage Targets
- Unit tests: >90% line coverage
- Integration tests: Critical paths covered
- E2E tests: Happy paths + key error scenarios
Anti-Patterns to Avoid
- Testing implementation details (test behavior, not internals)
- Overly specific assertions (leads to brittle tests)
- Test interdependence (each test must be isolated)
- Testing trivial code (getters/setters)
- Ignoring flaky tests (fix or remove immediately)
TDD for Different Contexts
API Endpoints
- Test request validation
- Test business logic
- Test response format
- Test error handling
UI Components
- Test rendering with props
- Test user interactions
- Test state changes
- Test accessibility
Database Operations
- Test CRUD operations
- Test constraints/validation
- Test transactions
- Test edge cases (null, empty)
使用示例
查看来源仓库中的使用示例与配置说明。
查看来源 ↗来源历史
- 收录时间
- 2026-01-20
- 索引更新时间
- 2026-01-26
- Branch
- main
收录与同步日期属于本站记录。源码修改历史请查看来源仓库。
GitHub ↗chat Comments (0)
Sign in to join the discussion and leave a comment.
No comments yet. Be the first to share your thoughts!