How to make Claude Code write good unit tests: the five green tests that checked nothing
A good test written by Claude Code is a test that fails when the behaviour it claims is broken. Three checks make sure of it for me: the expected value comes from the spec, never from the current code; the agent writing the tests looks for which wrong implementation would still pass, and adds those cases; then another agent puts the defect back and watches the test go red. In a single run, five green tests checked nothing. The suite found none of them. The reviewer, by reintroducing the defects, found all five.
The problem: a green suite proves nothing
Ask an agent to write tests and it will write many, and they will pass. The report will be honest: everything is green. But a green test only says the code does what the test looks at. If the test looks in the wrong place, it stays green with the bug inside.
Writing the tests before the code, as I do in my agent chain, solves part of the problem: the test fails first, so it checks something. It does not tell you whether it checks the right thing. A test can fail before the code, pass after, and still let through exactly the defect it was meant to catch.
Five green tests that checked nothing
On 24 August 2026, on ResellStats, my tracking app for resellers, a single run of my chain produced five tests of this kind. All green, all written in good faith, all useless:
- The same constant on both sides. A display order test imported, as its expected value, the very list the component displays. Reordering the source moved both sides at once, and the test stayed green.
- A reassuring name. A local expected value was named after the export it was supposed to be independent of. That is what made the first shape look acceptable to its author.
- A single value tested. The accessibility “selected” state was checked for one choice only. Replacing the whole computation with
condition === "very_good"still passed. - An absence that was too easy. A test checked that an element did not appear. The component did not exist yet: the test passed before, passed after, and proved nothing.
- An impossible sequence of states. The test moved the screen from “idle” to “done”. The app always goes through “computing” in between. The test passed on the buggy build and on the fixed one.
None was found by reading. The reviewer found all of them the same way: it put the defect back into the source, ran the test, and saw it stay green.
The method: three checks, at three moments
My chain is a Claude Code plugin. The agent that writes the tests is a subagent, with its own context, and it follows a skill called “tests that bite”. The rules below come from it.
- Before writing: the oracle is the spec. Every expected value comes from the brief or the spec. A test whose expected value is “what the code returns today” freezes the current behaviour and will never reveal a bug. For a calculation, at least one test compares against a result computed by hand.
- Before handing back: self-critique. The agent must answer one question: which wrong implementation would still pass? Swapped arguments, a value arriving as text instead of a number, a constant returned, a window off by one day, an unhandled error. It adds those cases itself and lists the ones it did not add, with the reason. In the August run, when asked the question, it flagged two of the five shapes on its own: the single value and the absence that was too easy.
- After the code: proof by biting. For each new test, the reviewer asks whether a plausible wrong implementation would pass. And every fix must show that its test bites: the agent reverts its fix, runs that one test file, sees it fail, then restores the fix. The failing line is part of its report.
The skill also lists the shapes that pass on broken code unless you pin them down: two sides computed the same way, an element whose presence is checked but not what makes it visible, a purchase path tested on the screen rather than on the request it sends, a date window with no row exactly on the boundary.
For the simplest temptation, disabling a test that gets in the way, there is a hook. Before every Edit or Write on a test file, it counts the skip, only or todo markers. If there are more after than before, it blocks the write with exit code 2. It also blocks any change to a coverage threshold. I describe these guardrails in the post on hooks.
What still goes wrong
- A red that proves nothing. On 9 October, to add Polish to ResellStats, the tests were red for one reason only: the translation file did not exist. So no assertion had run, and they contradicted each other. The coding agent was blocked three times on the same task. Today the test agent must say when its red comes from a missing module, and the reviewer checks those assertions. The entry is still open: nothing runs them yet before the code exists.
- The opposite defect: a test that fails on correct code. Twice on the same day, tests looked up elements by the internal structure of the testing library, or by a text present in two places on the screen. The code was right, the tests blocked it. The rule became: query by text or by role, never by the internal shape of nodes.
- Tests that fail lint. The test agent proved its red, but did not run lint or type checks on its own files. The coding agent ended up blocked by files it was not allowed to touch. It now runs the full fast check before handing back.
- Rules reduce, they do not remove. On 29 October, on another ResellStats feature, the reviewer still flagged a weak test on reduced motion, and a tautological test deferred for later.
- The hook does not see everything. A write through a shell command, with
sedfor example, does not go throughEditorWrite. The reviewer is the one who checks that no test was weakened.
How to start
You do not need a full chain. Add these instructions to your CLAUDE.md, or to your test subagent's file in .claude/agents/:
## Tests
- The expected value comes from the spec, never from the current code.
- A test must fail if the behaviour it claims is broken or removed.
- Before handing back: which wrong implementation would still pass?
Add those cases, list the missing ones and why.
- If the red comes from a missing module, say so:
no assertion has run yet.
- Query elements by text or by role.Then do the biting proof yourself on the next bug you fix: revert the fix, run the one test file concerned, check that it fails, restore the fix. If the test stays green, it protects nothing. Finally, hand that check to a reviewer that did not see the writing, as I explain in the post on code review by an agent.
Article written with the help of AI, from my daily practice and my projects.
The whole notebook