Tests as the Spec
Why This Matters
“Make it better” gives you a different result every time. A failing test gives the agent a definition of done that it cannot argue with, and that is a tester’s home ground.
Write the test, then ask
it("groups failures with the same error signature", () => {
const groups = groupFailures(run1099);
expect(groups.find((g) => g.signature.includes("ECONNREFUSED"))?.failures).toHaveLength(12);
});
Then:
Make this test pass. Do not change the test. Run
npm run checkand show the result.
The agent now has a target it can check itself against, and you have a way to verify the result that does not depend on reading every line.
In TestPilot this happened for real. Asked for a test that skipped runs do not count towards the failure rate, Claude Code wrote one, ran it, and reported that it fails: failureRate divides by every run, skipped ones included, so it returns 0.25 where the test expects 0.5. The test describes behaviour the code does not have yet: it is the spec.
Notice what it did not do: it did not change the test to 0.25 to make it pass. It explained the gap and asked. That is the behaviour this lesson is about, and the reason to tell Claude Code that the test is the specification.
Guard the test
Agents under pressure sometimes “fix” a failing test by changing the test. Three defences:
- Say it explicitly: do not change the test.
- Review the diff: test files should be untouched.
- For important tests, a PreToolUse hook (lesson 46) can block edits to specific test files during the task.
Examples beat adjectives
| Vague | Concrete |
|---|---|
| “Make the summary shorter” | “The summary must be at most 160 characters; here are two good examples” |
| “Handle errors better” | “When read_log returns not_found, the verdict must mention the missing log” |
| “Improve grouping” | “These 12 failures must be one group; these 3 must be separate” |
Let the agent interview you
When the requirement is genuinely unclear, ask Claude Code to ask you questions first:
Before writing code, ask me up to five questions about how grouping should work.
You will often find the gaps in your own requirement.
Several small issues
Five small, unrelated fixes: batch them in one request with a numbered list and one test each, or give each its own session? Batch when they touch different files and are independent; separate when they interact or when one is risky. Either way, one test per fix.
Exercise
Write the grouping test above, have Claude Code make it pass without changing the test, and check the diff for test changes.
Compare with solution/lesson-52-claude-code, which has the Claude Code setup for lessons 42–52. Its docs/claude-code-demo.md walks you through all of module 6 in one session.
