Tdd methodology expert

From rawveg/skillsforge-marketplace · View interactive version

Tdd methodology expert is an AI agent skill from the GitHub repository rawveg/skillsforge-marketplace. Use proactively when you need to implement features or fix bugs using strict Test-Driven Development (TDD) methodology. This agent should be activated for any coding task that requires writing new functionality, refactoring existing code, or ensuring comprehensive test coverage, but should not be us

How to use this skill

Skill file

TDD Methodology Expert

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.

When to Use This Skill

This skill automatically activates when:

Use this skill for:

Do NOT use this skill for:

Core TDD Methodology

Test-Driven Development follows a strict three-phase cycle that must be repeated for every increment of functionality:

🔴 Red Phase: Write a Failing Test

Always write the test before any production code.

  1. Write a test that expresses the desired behavior
  2. Run the test and verify it fails (for the right reason)
  3. Confirm the failure message indicates what's missing

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

🟢 Green Phase: Make the Test Pass

Write minimal code to make the failing test pass.

  1. Implement the simplest code that makes the test pass
  2. Run the test and verify it passes
  3. Run all tests to ensure nothing broke

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

🔵 Refactor Phase: Improve the Code

Clean up code while maintaining passing tests.

  1. Identify duplication, poor names, or structural issues
  2. Refactor incrementally, running tests after each change
  3. Verify all tests still pass
  4. Repeat until code is clean

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

TDD Workflow Integration

Before Starting Any Code Task

Step 1: Understand the Requirement

Step 2: Plan the Test

During Implementation

Always follow this sequence:

  1. 🔴 Red: Write failing test → Run → Verify failure
  2. 🟢 Green: Write minimal code → Run → Verify pass
  3. 🔵 Refactor: Improve code → Run → Verify still passes
  4. Commit: Save working, tested, clean code
  5. Repeat: Next test for next behavior

Never skip phases or reverse the order.

Communicating TDD Progress

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.

Bundled Tools and Resources

Scripts

check_tdd_compliance.py

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:

validate_tests.py

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:

setup_hooks.sh

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:

References

Load these references when deeper understanding is needed:

tdd-principles.md

Comprehensive guide to TDD methodology including:

When to reference: When explaining TDD concepts or resolving questions about methodology.

code-smells.md

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:

testing-patterns.md

Language-agnostic testing patterns and best practices:

When to reference: When writing tests or improving test structure.

Assets

Hook Templates

Located in assets/hook-templates/:

These templates are used by setup_hooks.sh and can be customized for specific project needs.

Prompt-Based Validation

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.

Recognizing Validation Requests

When the user requests TDD validation using phrases like:

Immediately respond by:

  1. Identify target code: Determine which files or directories to analyze
  1. Run compliance check: Execute check_tdd_compliance.py on the target code

```bash

python scripts/check_tdd_compliance.py <path-to-code>

```

  1. Parse and present results:
  1. Validate tests (if applicable): Run validate_tests.py on corresponding test files

```bash

python scripts/validate_tests.py <path-to-tests>

```

  1. Provide context-aware guidance:

Example Response Flow

User: "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?

When NOT to Run Validation

Do not automatically run validation scripts unless:

Avoid running validation:

Manual vs. Automated Validation

Manual (Prompt-Based):

Automated (Git Hooks):

Both approaches are valid. Users can choose the workflow that fits their preferences.

Prompt-Based Setup

Users can request TDD setup for their project through natural language prompts. This provides a guided setup experience that respects user preferences.

Recognizing Setup Requests

When the user requests TDD setup using phrases like:

Immediately respond by:

  1. Confirm the request: Acknowledge that you'll set up TDD for the project
  1. Update CLAUDE.md: Always add or update the TDD requirement

```markdown

# Development Guidelines

## Test-Driven Development (TDD)

This project follows strict Test-Driven Development methodology:

Use the tdd-methodology-expert skill for TDD guidance and validation.

```

  1. Install Claude Code hook: Always install the user-prompt-submit hook
  1. Offer pre-commit hook (optional): Proactively ask about git hook installation

Prompt the user:

```

✅ TDD Setup Progress:

📋 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)

```

  1. Install pre-commit hook (if user agrees):
  1. Provide completion summary:

```

✅ TDD Setup Complete!

What was installed:

Available validation scripts:

How to use:

Ready to start TDD development!

```

Example Setup Flow

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!

Setup Components

Always Install:

Optional Install (ask user):

Never Install Without Asking:

Handling Existing Setup

If TDD is already configured:

TDD Enforcement Rules

Mandatory Requirements

  1. Tests must be written before production code
  1. All tests must pass before committing
  1. Refactoring is not optional
  1. Tests must follow TDD patterns

Validation and Feedback

Continuously validate TDD adherence by:

  1. Checking code structure:
  1. Verifying test quality:
  1. Monitoring the cycle:

User Reassurance

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.

Common TDD Scenarios

Implementing a New Feature

Process:

  1. Break feature into small, testable behaviors
  2. For each behavior:
  1. Validate with check_tdd_compliance.py
  2. Commit clean, tested code

Fixing a Bug

Process:

  1. 🔴 Write a test that reproduces the bug (should fail)
  2. Confirm test fails with current code
  3. 🟢 Fix the bug (test should now pass)
  4. 🔵 Refactor if needed
  5. Validate no new code smells introduced
  6. Commit fix with test

Refactoring Existing Code

Process:

  1. Ensure existing tests pass (or add characterization tests)
  2. For each refactoring increment:
  1. Validate improvement with compliance check
  2. Commit refactored code

Adding Tests to Legacy Code

Process:

  1. Identify functionality to test
  2. Write characterization tests (document current behavior)
  3. Use tests to enable safe refactoring
  4. Gradually improve structure following TDD
  5. Future changes follow strict TDD

Integration with Development Workflow

IDE Integration

Tests should be:

Continuous Integration

Every commit should:

Code Review

Reviewers should verify:

Troubleshooting TDD Issues

"Tests are taking too long to write"

Solution: Tests are probably too big. Break into smaller behaviors.

"Can't figure out what to test first"

Solution: Test the simplest case. Start with happy path, then edge cases.

"Tests keep breaking during refactoring"

Solution: Tests are testing implementation, not behavior. Refactor tests too.

"Code is getting messy"

Solution: Not refactoring enough. Spend more time in refactor phase.

"Tests are hard to write"

Solution: Code design is poor. Let test difficulty guide design improvements.

Summary: TDD Mindset

When using this skill, always remember:

  1. Tests First: Never write production code without a failing test
  2. Small Steps: Keep Red-Green-Refactor cycles very short (2-10 minutes)
  3. Refactor Always: Code quality is not optional
  4. Trust the Process: TDD produces better design through discipline
  5. Verify Compliance: Use bundled tools to validate adherence

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.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.