Works with the AI Agents you already use

    TDD Coach: Red-Green-Refactor, Tests That Fail Right

    by Nguyen Duc Dung

    1

    Turn your agent into a disciplined TDD partner: one test that fails for the right reason, the minimal code to pass, then refactor on green. Flags tautological asserts, over-mocking and brittle snapshots. Any language.

    $5

    Secure checkout via Stripe

    Money-back guarantee

    Converts to your local currency at checkout

    Security scanned
    TDD Coach: Red-Green-Refactor, Tests That Fail Right

    TDD Coach: Red-Green-Refactor, Tests That Fail Right

    Example session with this skill installed

    Apply TDD to create a function that calculates a loyalty discount. 5% for orders over $100, 10% for over $500. No discount below $100.

    • Read your context and instructions
    • Compiled the tdd coach

    I have followed the TDD loop to implement the loyalty discount logic. First, I wrote failing tests for each threshold, then implemented the minimal logic to pass, and finally refactored for clarity.

    Loyalty Discount Implementation

    import pytest
    
    # Production Code
    def get_loyalty_discount(amount: float) -> float:
        """Calculates discount based on order amount."""
        if amount >= 500:
            return 0.10
        if amount >= 100:
            return 0.05
        return 0.0
        
    # Test Suite
    def test_returns_zero_discount_below_one_hundred():
        assert get_loyalty_discount(99.99) == 0.0
    
    def test_returns_five_percent_at_one_hundred():
        assert get_loyalty_discount(100.0) == 0.05
    
    def test_returns_five_percent_between_one_hundred_and_five_hundred():
        assert get_loyalty_discount(450.0) == 0.05
    
    def test_returns_ten_percent_at_five_hundred():
        assert get_loyalty_discount(500.0) == 0.10
    
    def test_returns_ten_percent_above_five_hundred():
        assert get_loyalty_discount(1200.0) == 0.10
    

    Next steps

    1. Run uv run pytest tests/test_discounts.py -q to verify the full suite passes.
    2. Integrate this function into the checkout service layer.
    3. Add a test case for negative input values to handle error boundaries.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Writing tests after the fact leads to brittle suites that verify implementations rather than behaviors. This skill enforces a strict red-green-refactor loop to ensure every line of code is justified by a failing test.

    What it does

    • Enforces TDD cycles by requiring a failing test for the missing behavior before any production code is written.
    • Validates failure reasons to ensure tests fail due to missing logic rather than configuration or import errors.
    • Simplifies implementations by encouraging the minimal code necessary to pass the current test.
    • Guides refactoring to clean up duplication and naming only when the suite is in a green state.
    • Prevents anti-patterns like tautological assertions, mocking the system under test, or brittle snapshots.

    How it works

    1. Red phase: The skill writes a single behavior-focused test and runs it to confirm a specific, meaningful failure.
    2. Green phase: It writes the simplest possible production code, including hard-coded values if necessary, to pass.
    3. Refactor phase: It cleans up the implementation while maintaining green status, ensuring no regressions occur.
    4. Verification: It runs the specific test during iteration and the full suite before finalizing the task.

    Frameworks & tools

    This skill is optimized for Python environments using pytest and uv. It utilizes arrange-act-assert patterns and standard test runners.

    Why this beats prompting it yourself

    Generic prompts often result in the AI writing the code first and then a test that mirrors it, which misses the point of TDD. This skill forces a vertical slice approach where tests drive the architecture and every assertion is verified to fail before it passes.

    Use cases

    • Implementing a new API endpoint or business logic layer from scratch.
    • Fixing a bug by first reproducing it with a targeted failing test case.
    • Refactoring complex legacy functions while ensuring behavior remains unchanged.
    • Adding edge case handling for empty inputs, boundary values, or external API failures.

    Known limitations

    This skill focuses on behavioral testing through public interfaces and may not be suitable for UI-heavy visual testing or non-deterministic environments. It assumes a standard test runner like pytest is available.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before buying a skill.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Money-back guarantee

    Doesn't do what its page promises? You get your money back.

    Frequently Asked Questions

    This skill enforces a strict red-green-refactor loop where a failing test for a specific behavior must be confirmed before any production code is written. It prevents common issues like brittle test suites that mirror implementation rather than behavior.

    The skill is optimized for Python environments and expects the presence of pytest and uv. It assumes the project uses standard arrange-act-assert patterns and has a test runner available to verify results.

    This skill is designed for behavioral testing through public interfaces and is not suitable for UI-heavy visual testing or non-deterministic environments. It also requires a standard test runner like pytest to be accessible in the environment.

    You can trigger this skill by asking your agent to implement a feature using TDD, write tests first, or follow a red-green-refactor cycle for a specific Python task. It is also useful when you need to fix a bug by first reproducing it with a targeted failing test.

    The skill delivers clean, refactored Python code where every line is justified by a passing test. It specifically avoids anti-patterns such as tautological assertions, mocking the system under test, or changing assertions to match incorrect outputs.

    New in Testing & Debugging

    Find more skills like this on the Claude Skills Marketplace, the Codex Skills Marketplace and the Gemini Skills Marketplace, or browse Claude Skills for Testing & Debugging.