# Test-Driven Development
<!--
ref: @skills/test-driven-development
tags: [tdd, testing, discipline, red-green-refactor]
triggers: [implementing feature, bug fix, wrote code before test, test passes immediately, skip tests just this once]
trigger_prompt: Use when implementing any feature or bugfix, before writing implementation code.
category: engineering
version: 1
updated_at: 2026-07-11T14:24:24.036Z
-->
Write the test first. Watch it fail. Write minimal code to pass.
**Core principle:** a test you never watched fail proves nothing — it might test the wrong thing, test the implementation instead of the behavior, or miss the edge cases that were forgotten.
**Violating the letter of the rules is violating the spirit of the rules.**
## When it applies
Always: new features, bug fixes, refactoring, behavior changes.
Exceptions require the operator's explicit sign-off: throwaway prototypes, generated code, configuration files. Thinking "skip TDD just this once" is rationalization, not an exception.
## The Iron Law
```
NO PRODUCTION CODE WITHOUT A FAILING TEST FIRST
```
Code written before its test gets deleted and rewritten. No exceptions: not kept as "reference", not "adapted" while writing tests, not looked at. Delete means delete; implement fresh from the tests.
## The cycle: Red → Green → Refactor
**RED — write one failing test.** One behavior, clear name, real code (mocks only when unavoidable). The test demonstrates the desired API.
**Verify RED — watch it fail.** Mandatory, never skipped. Run the test and confirm: it *fails* (not errors), the failure message is the expected one, and it fails because the feature is missing (not a typo). A test that passes immediately is testing existing behavior — fix the test. A test that errors needs the error fixed until it fails correctly.
**GREEN — minimal code.** The simplest code that passes the test. No extra features, no speculative options or parameters, no "improving" beyond what the test demands (YAGNI).
**Verify GREEN — watch it pass.** Mandatory. Confirm the test passes, all other tests still pass, and output is pristine (no errors or warnings). Test fails? Fix the code, not the test. Other tests fail? Fix now.
**REFACTOR — clean up, after green only.** Remove duplication, improve names, extract helpers. Tests stay green; no behavior added.
Repeat with the next failing test.
## Test quality
| Quality | Good | Bad |
|---------|------|-----|
| Minimal | One thing; "and" in the name means split it | `test('validates email and domain and whitespace')` |
| Clear | Name describes the behavior | `test('test1')` |
| Shows intent | Demonstrates the desired API | Obscures what the code should do |
## Why order matters
- **Tests written after code pass immediately** — which proves nothing. They're biased by the implementation: they test what was built, not what's required, and verify remembered edge cases rather than discovered ones.
- **Manual testing is ad-hoc** — no record of what was tested, can't re-run on change, cases get forgotten under pressure.
- **"Deleting X hours of work is wasteful" is sunk cost fallacy** — the real waste is keeping code that can't be trusted. Working code without real tests is technical debt.
- **TDD is the pragmatic option** — it finds bugs before commit, prevents regressions, documents behavior, and enables safe refactoring. "Pragmatic" shortcuts mean debugging in production, which is slower.
## Rationalization table
| Excuse | Reality |
|--------|---------|
| "Too simple to test" | Simple code breaks. The test takes 30 seconds. |
| "I'll test after" | Tests passing immediately prove nothing. |
| "Tests after achieve the same goals" | Tests-after answer "what does this do?"; tests-first answer "what should this do?" |
| "Already manually tested" | Ad-hoc ≠ systematic. No record, can't re-run. |
| "Deleting X hours is wasteful" | Sunk cost fallacy. Unverified code is debt. |
| "Keep it as reference while writing tests" | That becomes adapting it. That's testing after. Delete means delete. |
| "Need to explore first" | Fine — throw the exploration away, then start with TDD. |
| "The test is hard to write" | Hard to test = hard to use. The design is unclear; listen to the test. |
| "TDD will slow me down" | TDD is faster than debugging. |
| "Existing code has no tests" | You're improving it — add tests for existing code. |
## Red flags — stop and start over
Code before test · test written after implementation · test passes immediately · can't explain why the test failed · tests added "later" · "just this once" · "I already manually tested it" · "it's about spirit not ritual" · "keep as reference" · "already spent X hours" · "TDD is dogmatic, I'm being pragmatic" · "this is different because…"
All of these mean: delete the code, start over with TDD.
## Verification checklist (before marking work complete)
- Every new function/method has a test
- Each test was watched failing before implementing, for the expected reason
- Minimal code written to pass each test
- All tests pass; output pristine
- Tests use real code (mocks only when unavoidable)
- Edge cases and errors covered
Can't check every box? TDD was skipped. Start over.
## When stuck
| Problem | Solution |
|---------|----------|
| Don't know how to test | Write the wished-for API; write the assertion first; ask the operator |
| Test too complicated | Design too complicated — simplify the interface |
| Must mock everything | Code too coupled — use dependency injection |
| Test setup huge | Extract helpers; still complex? simplify the design |
## Debugging integration
Bug found → write a failing test that reproduces it → follow the cycle. The test proves the fix and prevents regression. Never fix a bug without a test.
## Final rule
```
Production code → a test exists and failed first
Otherwise → not TDD
```
---
**Source:** [test-driven-development](https://github.com/obra/superpowers/blob/main/skills/test-driven-development/SKILL.md) — Superpowers by Jesse Vincent (obra), MIT. Bundled `testing-anti-patterns.md` (mock pitfalls) listed-and-linked at source, not harvested.
**Converted via:** `@protocol/skill-to-protocol`
---
*Related: `@content/youtube/cole-medin/11-more-tiny-coding-agent-fixes-with-a-stupid-amount-of-payoff-learn-these-now` · `@content/youtube/nate-herk/i-made-codex-and-claude-code-build-the-same-app-one-clearly-won` · `@content/web/anthropic/2026-11-16-how-the-claude-code-team-thinks-about-verification` · `@protocol/root-cause` · `@content/youtube/yc-root-access/2026-06-24-what-it-actually-takes-to-deploy-a-voice-agent-to-a-fortune`*
---
*You're reading a protocol on [protocol.supply](https://protocol.supply) — hosted, searchable knowledge for AI agents. Think Agent Skills, as an API: bootstrap once (`curl -sL https://api.protocol.supply/bootstrap`), search (`/search?q=…`), fetch (`/@repo/slug`).*
*Any skill can become a protocol, and any protocol installs as a local skill — tell your agent "install this as a skill" or see [`@protocol/protocol-to-skill`](https://api.protocol.supply/@protocol/protocol-to-skill).*