skillfed

Test-Driven Development (TDD)

This skill walks you through the test-first workflow: write a failing test, verify it fails correctly, implement just enough code to pass, then refactor while keeping tests green. You'll learn why test order matters, how to recognize when you're rationalizing shortcuts, and how to apply TDD to features, bug fixes, and refactoring across any language.

Test-Driven Development (TDD) teaches you to write failing tests first, then minimal code to pass them, ensuring every feature is properly verified.

AI-generated summary based on this skill's SKILL.md

733 163 MIT updated by obra

Install

obra/superpowers-skills/test-driven-development · repository language: TypeScript

CLI (skillfed)coming soon
git clone https://github.com/obra/superpowers-skills
cp -r superpowers-skills/skills/testing/test-driven-development ~/.claude/skills/test-driven-development

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

What is the red-green-refactor cycle in Test-Driven Development?

Test-Driven Development's red-green-refactor cycle is the core workflow: first, write a failing test (red phase); second, implement just enough code to pass that test (green phase); third, refactor your code while keeping tests green. This cycle repeats for each feature or change, ensuring tests drive design and catch bugs early.

How do I start with TDD and write tests first?

Test-Driven Development begins by writing a failing test before any implementation. Define what your code should do, write a test that verifies that behavior, watch it fail, then write minimal code to make it pass. This test-first approach forces you to think about requirements upfront and creates a safety net for refactoring.

Why write tests first instead of testing after implementation?

Test-Driven Development's test-first approach catches design issues early, prevents over-engineering, and ensures every line of code has a test. Writing tests after implementation often leads to incomplete coverage and tests that merely validate existing code rather than driving better design. TDD makes refactoring safer and reduces debugging time.

Can I apply TDD to bug fixes and code changes?

Yes. Test-Driven Development applies directly to bug fixes: write a failing test that reproduces the bug, fix the code to pass it, then refactor. For code changes, write tests for the new behavior first, implement it, and refactor. This workflow ensures bugs stay fixed and changes don't break existing functionality.

What does Test-Driven Development mean by minimal code to pass tests?

Test-Driven Development's green phase requires writing only enough code to make the test pass—no extra features, no premature optimization. This constraint forces you to stay focused, prevents scope creep, and makes refactoring the next step. Once tests pass, you improve the code's design without changing its behavior.

How does Test-Driven Development help catch bugs and improve code quality?

Test-Driven Development improves quality by building a comprehensive test suite as you code, catching regressions immediately, and forcing you to think through edge cases. Tests serve as executable documentation and a safety net for refactoring, reducing bugs in production and making code easier to maintain and extend.

SKILL.md

rendered from the published skill — quoted content, verbatim

Test-Driven Development (TDD)

Overview

Write the test first. Watch it fail. Write minimal code to pass.

Core principle: If you didn't watch the test fail, you don't know if it tests the right thing.

Violating the letter of the rules is violating the spirit of the rules.

When to Use

Always: - New features - Bug fixes - Refactoring - Behavior changes

Exceptions (ask your human partner): - Throwaway prototypes - Generated code - Configuration files

Thinking "skip TDD just this once"? Stop. That's rationalization.

The Iron Law

NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST

Write code before the test? Delete it. Start over.

No exceptions: - Don't keep it as "reference" - Don't "adapt" it while writing tests - Don't look at it - Delete means delete

Implement fresh from tests. Period.

Red-Green-Refactor

```dot digraph tdd_cycle { rankdir=LR; red [label="RED\nWrite failing test",

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 1 file
skills/testing/test-driven-development/SKILL.md

Related skills

Tags

test-first-methodology red-green-refactor behavior-driven-design quality-assurance-process automated-verification code-confidence regression-prevention edge-case-discovery minimal-implementation