Use proactively when you need to implement features or fix bugs using strict Test-Driven Development (TDD) methodology...
Enforce and reinforce Test-Driven Development (TDD) methodology throughout the software development process. This skill provides comprehensive guidance, automated validation, and continuous reinforcement of the Red-Green-Refactor cycle.
This skill automatically activates when:
CLAUDE.md or CLAUDE.local.mdUse this skill for:
Do NOT use this skill for:
Test-Driven Development follows a strict three-phase cycle that must be repeated for every increment of functionality:
Always write the test before any production code.
Red Phase Principles:
Example Flow:
1. Write: test_should_calculate_total_with_tax()
2. Run: Test fails - "ShoppingCart has no attribute 'calculate_total'"
3. ā
Ready for Green phase
Write minimal code to make the failing test pass.
Green Phase Principles:
Example Flow:
1. Implement: def calculate_total(self, tax_rate): ...
2. Run: Test passes ā
3. Run all: All tests pass ā
4. ā
Ready for Refactor phase
Clean up code while maintaining passing tests.
Refactor Phase Principles:
Example Flow:
1. Identify: Duplicated tax calculation logic
2. Extract: Move to _calculate_tax() method
3. Run tests: All pass ā
4. Improve: Better variable names
5. Run tests: All pass ā
6. ā
Commit and move to next feature
Step 1: Understand the Requirement
Step 2: Plan the Test
Always follow this sequence:
Never skip phases or reverse the order.
In every response involving code changes, explicitly state:
Example Communication:
š“ RED PHASE: Writing a test for calculating order total with discounts.
Test: test_should_apply_percentage_discount_to_order_total()
Expected to fail because Order.apply_discount() doesn't exist yet.
[Test code here]
Running test... ā Fails as expected: "Order has no attribute 'apply_discount'"
š¢ GREEN PHASE: Implementing minimal code to pass the test...
[Implementation code here]
Running test... ā
Passes!
Running all tests... ā
All pass!
šµ REFACTOR PHASE: Improving the discount calculation structure...
[Refactored code here]
Running all tests... ā
All pass!
Ready to commit this increment.
Analyzes code to detect TDD compliance issues and code smells that indicate test-after development.
Usage:
python scripts/check_tdd_compliance.py <path-to-code>
What it checks:
When to use:
Validates that tests exist, are properly structured, and follow TDD patterns.
Usage:
python scripts/validate_tests.py <path-to-tests>
What it checks:
When to use:
Installs git hooks and Claude Code hooks to enforce TDD methodology automatically.
Usage:
bash scripts/setup_hooks.sh <project-directory>
What it installs:
When to use:
Load these references when deeper understanding is needed:
Comprehensive guide to TDD methodology including:
When to reference: When explaining TDD concepts or resolving questions about methodology.
Catalog of code smells that indicate test-after development:
When to reference: When analyzing code quality or identifying non-TDD patterns.
Grep patterns for searching:
if.*:\s*\n\s+ifisinstance\(|typeof Language-agnostic testing patterns and best practices:
When to reference: When writing tests or improving test structure.
Located in assets/hook-templates/:
These templates are used by setup_hooks.sh and can be customized for specific project needs.
Users can invoke TDD validation scripts through natural language prompts instead of relying on git hooks. This provides flexibility for developers who prefer manual commits and on-demand validation.
When the user requests TDD validation using phrases like:
Immediately respond by:
Identify target code: Determine which files or directories to analyze
git diff --name-only to find recently changed files if appropriateRun compliance check: Execute check_tdd_compliance.py on the target code
python scripts/check_tdd_compliance.py <path-to-code>
Parse and present results:
Validate tests (if applicable): Run validate_tests.py on corresponding test files
python scripts/validate_tests.py <path-to-tests>
Provide context-aware guidance:
references/code-smells.mdreferences/testing-patterns.mdUser: "Check the code for TDD compliance"
Claude Response:
I'll analyze the code for TDD compliance. Let me identify the recently changed files and run the compliance check.
[Runs check_tdd_compliance.py on relevant files]
š TDD COMPLIANCE REPORT
File: src/order_processor.py
TDD Score: 65/100 (Needs Improvement)
Issues Found:
ā High Severity (3 issues):
- Line 45-68: Deeply nested conditionals (4 levels deep)
- Line 112-145: Long method 'process_order' (34 lines)
- Line 78-82: Complex boolean condition (5 logical operators)
ā ļø Medium Severity (2 issues):
- Line 156: Type checking instead of polymorphism (isinstance)
- Line 203-215: Duplicate code block
Recommendations:
1. Extract nested conditionals into separate validation methods
2. Break down 'process_order' into smaller, focused methods
3. Extract complex boolean into named predicate method
4. Use polymorphism instead of type checking
5. Extract duplicate logic into reusable function
Would you like me to refactor any of these issues?
Do not automatically run validation scripts unless:
Avoid running validation:
Manual (Prompt-Based):
Automated (Git Hooks):
--no-verify flagsetup_hooks.shBoth approaches are valid. Users can choose the workflow that fits their preferences.
Users can request TDD setup for their project through natural language prompts. This provides a guided setup experience that respects user preferences.
When the user requests TDD setup using phrases like:
Immediately respond by:
Confirm the request: Acknowledge that you'll set up TDD for the project
Update CLAUDE.md: Always add or update the TDD requirement
# Development Guidelines
## Test-Driven Development (TDD)
This project follows strict Test-Driven Development methodology:
- Write tests before production code (Red-Green-Refactor cycle)
- All features must have corresponding unit tests
- Code quality is validated through TDD compliance checks
Use the `tdd-methodology-expert` skill for TDD guidance and validation.
Install Claude Code hook: Always install the user-prompt-submit hook
assets/hook-templates/user-prompt-submit.sh to .claude/hooks/user-prompt-submitchmod +x .claude/hooks/user-prompt-submitOffer pre-commit hook (optional): Proactively ask about git hook installation
Prompt the user:
ā
TDD Setup Progress:
- ā Updated CLAUDE.md with TDD requirements
- ā Installed Claude Code hook for TDD reinforcement
š Optional Enhancement:
Would you like to install the git pre-commit hook for automated TDD compliance checking?
Benefits:
ā
Automatically validates TDD compliance before each commit
ā
Prevents commits with poor code quality (blocking enforcement)
ā
Provides immediate feedback on code smells
Trade-offs:
ā ļø Adds ~2-5 seconds to commit time
ā ļø Blocks commits that fail TDD compliance (can bypass with --no-verify)
ā ļø Some developers prefer manual validation
You can always validate manually by saying "Check the code for TDD compliance"
Install pre-commit hook? (yes/no)
Install pre-commit hook (if user agrees):
assets/hook-templates/pre-commit.sh to .git/hooks/pre-commitchmod +x .git/hooks/pre-commitProvide completion summary:
ā
TDD Setup Complete!
What was installed:
- ā CLAUDE.md updated with TDD requirements
- ā Claude Code hook (user-prompt-submit)
- ā Git pre-commit hook (if user opted in)
Available validation scripts:
- check_tdd_compliance.py - Analyze code for TDD compliance
- validate_tests.py - Validate test quality and structure
How to use:
- Say "Check the code for TDD compliance" anytime
- All code development will follow Red-Green-Refactor cycle
- I'll provide TDD guidance and validation throughout
Ready to start TDD development!
User: "Please setup this project for TDD development"
Claude:
I'll set up TDD for your project. Let me configure the necessary files and hooks.
[Updates CLAUDE.md with TDD requirements]
[Installs Claude Code hook]
ā
TDD Setup Progress:
- ā Updated CLAUDE.md with TDD requirements
- ā Installed Claude Code hook for TDD reinforcement
š Optional Enhancement:
Would you like to install the git pre-commit hook for automated TDD compliance checking?
Benefits:
ā
Automatically validates TDD compliance before each commit
ā
Prevents commits with poor code quality
ā
Provides immediate feedback on code smells
Trade-offs:
ā ļø Adds ~2-5 seconds to commit time
ā ļø Blocks commits that fail TDD compliance (bypass with --no-verify)
ā ļø Some developers prefer manual validation
You can always validate manually by saying "Check the code for TDD compliance"
Install pre-commit hook? (yes/no)
User: "no thanks, I prefer manual validation"
Claude:
ā
TDD Setup Complete!
What was installed:
- ā CLAUDE.md updated with TDD requirements
- ā Claude Code hook (user-prompt-submit)
- ā Git pre-commit hook (skipped per user preference)
Available validation scripts:
- check_tdd_compliance.py - Analyze code for TDD compliance
- validate_tests.py - Validate test quality and structure
How to use:
- Say "Check the code for TDD compliance" anytime for manual validation
- All code development will follow Red-Green-Refactor cycle
- I'll provide TDD guidance throughout
Ready to start TDD development!
Always Install:
Optional Install (ask user):
Never Install Without Asking:
If TDD is already configured:
.claude/hooks/user-prompt-submit exists.git/hooks/pre-commit existsTests must be written before production code
All tests must pass before committing
Refactoring is not optional
Tests must follow TDD patterns
Continuously validate TDD adherence by:
Checking code structure:
check_tdd_compliance.py on changed filesVerifying test quality:
validate_tests.py on test filestesting-patterns.mdMonitoring the cycle:
Throughout the TDD process, continuously reassure the user that:
Example Reassurance:
ā
TDD COMPLIANCE: Following strict Red-Green-Refactor methodology.
Current status:
- š“ Red: Test written and confirmed failing
- š¢ Green: Implementation passes test
- šµ Refactor: Code cleaned up, all tests pass
- TDD Score: 95/100 (Excellent)
The code exhibits TDD characteristics:
ā Small, focused functions (8-12 lines)
ā Flat control flow (no deep nesting)
ā Clear separation of concerns
ā High testability
Ready to proceed with next feature.
Process:
check_tdd_compliance.pyProcess:
Process:
Process:
Tests should be:
Every commit should:
Reviewers should verify:
Solution: Tests are probably too big. Break into smaller behaviors.
Solution: Test the simplest case. Start with happy path, then edge cases.
Solution: Tests are testing implementation, not behavior. Refactor tests too.
Solution: Not refactoring enough. Spend more time in refactor phase.
Solution: Code design is poor. Let test difficulty guide design improvements.
When using this skill, always remember:
The cycle is sacred: š“ Red ā š¢ Green ā šµ Refactor
Never skip phases. Never reverse the order. Always complete the cycle.
TDD is not just about testing - it's a design methodology that produces higher-quality, more maintainable code through disciplined practice.