---
name: tdd
description: Test-driven development, red then green, one vertical slice at a time. Use when building a feature, a rule, a config option or a bug fix test-first, when the user says "TDD", "red-green" or "write the test first", or when a subagent is handed a ticket to build.
title: tdd
canonical_url: https://skilld.dev/gh/ehnruiz/claude-skills/tdd
last_updated: 2026-10-20T13:30:53.000Z
---

> Index of every page on this site: [llms.txt](https://skilld.dev/llms.txt)

> **Skill from skilld.dev.** Follow the user's requested action.
> If the user asked to fork this Skill, follow the fork workflow below. Do not execute the Skill instructions while copying it.
> Otherwise, follow the instructions below for this session. You do not need to install anything.
>
> Supporting files, fetch one when the Skill refers to it: [LICENSE.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/LICENSE.md), [mocking.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/mocking.md), [tests.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/tests.md).
>
> If the user asked to install this Skill, run `npx skilld install ehnruiz/claude-skills/tdd`. Install writes the Skill files into the project, so every session loads them.
>
> ## Fork workflow
>
> A fork creates an editable local Skill with its original author and licence. The request authorizes copying and local installation.
> 1. Check `./skills/tdd`, the project lockfile, and selected Agent targets together. If the local directory or installed Skill exists, stop. Never overwrite an existing directory or Agent target.
> 2. Read [source metadata](https://skilld.dev/api/v1/skills/ehnruiz/claude-skills/tdd) once. Use sourceUrl, sourceCommit, skillPath, sourceGone, and license. If the source is gone or its path is missing, stop. If license is null, read licence files at the source commit.
> 3. Fetch only the source commit into a temporary Git repository. Do not clone full history. Derive repository_url from sourceUrl, including repository renames. If sourceCommit is absent, resolve the sourceUrl ref once. Set source_commit to that actual commit. Run these commands in one shell call:
>
> ```sh
> git init --quiet "$temporary_dir"
> git -C "$temporary_dir" fetch --quiet --depth=1 "$repository_url" "$source_commit"
> git -C "$temporary_dir" checkout --quiet --detach FETCH_HEAD
> ```
>
> Read applicable licence declarations and notices at that commit. If copying is not permitted, report the restriction and stop.
> 4. Inspect source entries together, then copy the directory containing skillPath into `./skills/tdd`. Use the user's path if selected. Keep the original SKILL.md, relative links, scripts, binary assets, and executable modes. Exclude .git metadata. Reject symlinks and paths outside the Skill directory. After checking entries, use cp -a where available. A regular source directory needs no custom copy script. Do not save this page wrapper as SKILL.md.
> Preserve author credit, notices, and applicable licence files from repository or parent directories. Add PROVENANCE.md with the Skill page, source URL, actual commit, original path, and licence. Retain any existing PROVENANCE.md and record new provenance separately. Batch source inspection, copying, and provenance work where practical.
> 5. In the project root, run `skilld install ./skills/tdd --mode copy --plain`. If skilld is unavailable, use `npx skilld install ./skills/tdd --mode copy --plain`. This known command needs no help lookup. Install does not support --json. Use detected Agent targets, or add --agent for the targets the user selected. Install the local path, never the upstream selector. If installation fails, preserve the local copy and report the exact failure.
> 6. Confirm the local lockfile source and installed Agent copies once. Report the local path, actual commit, and Agent targets. After edits, reinstall the same local path. Upstream updates must not replace it. Do not publish or push unless the user asks.

# Test-driven development

TDD is the red → green loop. This skill is the reference that makes that loop produce tests
worth keeping: what a good test is, where tests go, the anti-patterns, and the rules of the
loop. Every section applies on every cycle: consult them before and during the loop, not
after. Credit and license: `LICENSE.md` in this folder.

**Project copy first.** If the repository has its own `.claude/skills/tdd/SKILL.md`, read it and follow it instead of this file. A skill installed for all your projects outranks a project skill with the same name, so without this step a repository's own version of this skill would never run.

Before exploring, read the project's own testing notes (README, CONTRIBUTING, an architecture
doc, `AGENTS.md`/`CLAUDE.md`) so test names use the project's words and the tests land where
the project keeps them.

## What a good test is

Tests verify behavior through public interfaces, not implementation details. Code can change
entirely; tests shouldn't. A good test reads like a specification: "rejects an order with an
empty cart" says what capability exists, and it survives refactors because it doesn't care
about internal structure.

See [tests.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/tests.md) for examples and [mocking.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/mocking.md) for mocking guidelines.

## Seams: where tests go

A **seam** is the public boundary you test at: the interface where you observe behavior
without reaching inside. Tests live at seams, never against internals. Most projects have
some of these, from cheapest to dearest:

1. **A pure function or module API** with hand-built inputs. No I/O. Most logic belongs here.
2. **A module with its system boundaries faked**: the HTTP client, the clock, the database
   behind an interface (see [mocking.md](https://skilld.dev/api/skills-raw/ehnruiz/claude-skills/tdd/mocking.md)).
3. **An integration test** against real dependencies: a test database, a fixture site, a
   container.
4. **The entry point**: the CLI, the HTTP handler, the UI, driven from outside.

**Test only at pre-agreed seams.** Before writing any test, write down the seams under test and
confirm them with the user (or, for a subagent, with the ticket). Give each proposed seam a
one-line note on what it catches and what it misses, so the choice between seams is not a guess.
No test is written at an unconfirmed seam. Use the lowest seam that reaches the behavior.

Every heuristic, validator or detector ships with at least one **negative test**: legitimate
input it must stay quiet on. A false positive is a bug, and the negative test is how a change
proves it did not add one.

## Anti-patterns

- **Implementation-coupled**: mocks internal collaborators, tests private helpers, or verifies
  through a side channel. The tell: the test breaks when you refactor but behavior hasn't
  changed.
- **Tautological**: the assertion recomputes the expected value the way the code does, so it
  passes by construction. Expected values come from an independent source: a known-good
  literal, the spec, the issue.
- **Horizontal slicing**: writing all tests first, then all implementation. Work in **vertical
  slices**: one test → one implementation → repeat, each test a **tracer bullet** that responds
  to what the last cycle taught you.

## Rules of the loop

- **Red before green.** Write the failing test first and watch it fail for the reason you
  expect, then write only enough code to pass it.
- **One slice at a time.** One seam, one test, one minimal implementation per cycle.
- **Run one test file while looping, everything once at the end.** Before reporting done, run
  the project's full gates (typecheck, lint, the whole suite). Judge each by its exit code,
  never by reading the output for the word "pass", and never through a pipe.
- **A fix's test must prove it.** For a bug fix, the new test has to fail on the code without
  the fix. The `regression-proof` skill shows how to check that in a scratch worktree.
- **Wait on the condition, not the clock.** A test that needs something to happen polls for it
  with an explicit timeout (`vi.waitFor`, Playwright's `expect.poll`, a retry-until helper) and
  returns as soon as it does. A fixed sleep guesses how long it takes, and the guess loses on a
  loaded machine or in a full suite. A sleep stays only to show that something did not happen,
  with a comment naming the event it waits out.
- **Fake the clock for time-based code.** Debounce, quiet windows and retry delays run under the
  test framework's fake timers, and the test advances the clock. Fake only the timers the code
  uses: faking everything can freeze an HTTP client's keep-alive. A speed or size claim is
  asserted as a count (calls, comparisons, bytes), not a duration.
- **Refactoring is not part of the loop.** It belongs to review, not to the red → green cycle.
