Test Driven Development
Core Principle
Tests are code, not an add-on. Every feature, bug fix, or refactoring should include tests that verify the behavior. Writing tests is part of implementing functionality, not a separate activity done after.
When to Use This Skill
- Planning new features or functionality
- Implementing any code change
- Fixing bugs
- Refactoring existing code
Trigger Conditions
This skill should be active when:
- Adding new functionality to the codebase
- Modifying existing behavior
- Fixing a bug
- Changing or improving existing code
How to Apply TDD in Agent Workflow
Feature Implementation
- Understand the feature - Read requirements or specifications
- Plan test cases - Identify all behaviors to verify
- Write tests first - Create tests that describe desired behavior
- Watch tests fail - Verify tests fail because behavior does not exist
- Write minimal code - Implement only what tests require
- Refactor - Clean up while keeping tests green
Bug Fixing
- Write a test that reproduces the bug
- Verify the test fails
- Fix the bug
- Verify the test passes
Refactoring
- Ensure tests exist for code being refactored
- Run tests to establish baseline
- Refactor in small steps
- Run tests after each step
- All tests must pass before considering refactoring complete
Key Principles
Tests Before Code
Write tests before writing production code. This drives cleaner design and ensures testability from the start.
Tests as Documentation
Test names and structure should clearly describe the expected behavior. Tests serve as executable documentation.
Minimal Test Scope
Each test should verify one behavior. This makes tests easier to understand, maintain, and debug when they fail.
References
For detailed guidance, see:
- TDD Principles - Three laws, Red-Green-Refactor cycle, test structure
- Test Planning - How to plan tests during feature development
- Anti-Patterns - Common mistakes to avoid