Write tests that pin behaviour, test-first: red/green/refactor in vertical slices, what is worth testing, mocking only at system boundaries, fixing flaky tests. Use when building test-first, adding or fixing tests, or chasing a flaky suite.
Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Test Driven?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/k6w-test-driven)
---
name: test-driven
description: "Write tests that pin behaviour, test-first: red/green/refactor in vertical slices, what is worth testing, mocking only at system boundaries, fixing flaky tests. Use when building test-first, adding or fixing tests, or chasing a flaky suite."
keywords: [test, tests, tdd, unit test, integration test, red green, coverage, mock, stub, fake, fixture, flaky, assertion, spec]
---
# Test-driven work
A good test states a behaviour in the project's own words ("an expired token is refused") and keeps
passing through any refactor that keeps that behaviour. A test that breaks when nothing a user could
notice has changed is a cost, not a safety net.
## Before the first test
- Find how the project tests: the test runner, file naming, fixtures, helpers, the command in
`package.json` / `Makefile` / `pyproject`. Follow it exactly; do not bring in a second framework.
- Decide the **seams**: the public interfaces you will test through (an exported function, an HTTP
route, a CLI command). Test there and nowhere deeper. If no clean seam exists, the design is the
problem (see `lean-code`).
- List the behaviours to cover, most important first: the main path, the edge cases that bite
(empty, zero, one, many, unicode, very large, duplicate, out of order), the error paths.
## The loop, one slice at a time
1. **Red.** Write one test for the next behaviour. Run it and see it fail *for the right reason* (an
assertion about the behaviour, not a typo or a missing import).
2. **Green.** Write the least code that makes it pass. No code for tests you have not written yet.
3. **Refactor.** With everything green, tidy names and remove duplication in both the code and the
test. Behaviour does not change in this step; run the tests after each edit.
Do not write all the tests first and all the code after: tests written ahead of understanding check
the shape you imagined, not the behaviour you built. One test, one piece of code, repeat.
## What makes a test worth keeping
- **Asserts outcomes through the interface**: return values, responses, files written, events
emitted. Not private fields, call counts on your own functions, or internal state.
- **Expected values come from outside the code**: a literal you worked out by hand, an example from
the spec. Never recompute the expectation the same way the code does; that test can't fail.
- **Readable as a spec**: the name says the behaviour; the body is arrange, act, assert with only the
details that matter to this case.
- **Independent**: no ordering between tests, no shared mutable state, each cleans up what it made.
- **One reason to fail**: if it fails, the name tells you what broke.
## Mocking
Fake only what you don't own or can't control: the network, third-party APIs, the clock, randomness,
sometimes the filesystem or database (prefer a real temporary one when it is cheap). Never mock your
own modules to test another of your modules. Details and patterns:
`load_skill("test-driven", "references/mocking.md")`.
## Flaky tests
A test that sometimes fails is a bug in the test or the code, never noise to retry away. Diagnosis
and fixes: `load_skill("test-driven", "references/flaky-tests.md")`. Use `debugging` to find a real
race in the product code.
## Rules
- Never weaken, skip or delete a test to get green. If a test is wrong, say why in the report.
- Don't change production code beyond the task; report bugs you find to the lead instead.
- Finish by running the full relevant suite and the task's acceptance command; report the exact
commands and results.