Test-Driven Development (TDD)

Guide Python developers through the Red-Green-Refactor-Commit cycle using pytest.

1|Updated Jul 10, 2025
One-click install
npx skills add https://github.com/jzallen/fred_simulations --skill test-driven-development-tdd
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: Test-Driven Development (TDD)
Source: https://github.com/jzallen/fred_simulations/tree/main/.claude/skills/tdd
Command: npx skills add https://github.com/jzallen/fred_simulations --skill test-driven-development-tdd

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires pytest.

What problem does it solve?

This Skill guides you through the Test-Driven Development (TDD) methodology, ensuring you write robust, well-designed, and maintainable code. It helps prevent bugs, improves code quality, and provides a safety net for refactoring, ultimately saving development time and reducing technical debt.

Core Features & Use Cases

  • Red-Green-Refactor Cycle: Master the iterative process of writing failing tests, making them pass, and improving code design.
  • FIRST Principles: Apply principles for Fast, Isolated, Repeatable, Self-validating, and Timely tests.
  • pytest Best Practices: Utilize pytest fixtures, proper mocking, and descriptive test naming conventions.
  • Use Case: You need to implement a new ShoppingCart class. Use TDD to write a failing test for adding an item, then implement the minimal code to pass it, and finally refactor for better design, repeating the cycle for other features like removing items or calculating totals.

Quick Start

Explain the Red-Green-Refactor-Commit cycle for implementing a simple Calculator class with an add method, including an example of a failing test, passing implementation, and refactoring step.

Frequently Asked Questions about Test-Driven Development (TDD)

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I write tests before implementing code?

Test-Driven Development (TDD) inverts the typical workflow: write a failing test first, then implement minimal code to pass it, then refactor. This Red-Green-Refactor cycle with pytest ensures your code satisfies requirements from the start and remains maintainable as it evolves.

What are the FIRST principles for writing good unit tests?

FIRST principles ensure tests are Fast, Isolated, Repeatable, Self-validating, and Timely. TDD enforces these through pytest fixtures, proper mocking, and focused test scope, so your test suite runs quickly, catches regressions reliably, and provides immediate feedback during development.

Can I use TDD with pytest fixtures and mocking?

Yes. pytest fixtures provide reusable test setup, and mocking isolates the code under test from dependencies. TDD leverages both to write focused, repeatable tests that validate behavior independently, following the AAA structure (Arrange, Act, Assert) for clarity.

How does refactoring fit into the TDD cycle?

After tests pass (Green), refactoring improves code design without changing behavior because passing tests act as a safety net. The Red-Green-Refactor-Commit cycle ensures each incremental change is validated, preventing bugs and technical debt while maintaining code quality.

When should I apply TDD to Python projects?

TDD works for unit, integration, and end-to-end test workflows in Python-based projects using pytest. It's especially valuable when implementing new features, refactoring legacy code, or maintaining high quality standards where preventing bugs and enabling safe changes matter most.

What's the difference between writing tests first versus after implementation?

TDD (tests first) shapes design by forcing you to clarify requirements upfront and creates simpler, more testable code. Tests written after implementation often miss edge cases and don't influence design. TDD produces robust, well-designed code with fewer bugs and easier refactoring.