TL;DR
When a coding agent writes both the code and its tests, the tests can only show that the code agrees with itself. Writing a few tests first, from examples of what we mean, watching them fail, and then having the agent make them pass without editing them is called test-driven development. Those tests guard every later change.
- An agent rounded 24 hours 45 minutes up to 2 days, and one of its own tests checked for that bug, so a Friday morning cake order slipped through for Saturday.
- Wren's four tests came from Odette's rule, failed on the buggy code, and caught both a too-loose and a too-strict version of the rule before the agent got it right.
- Unit tests check one function in milliseconds, integration tests run the pieces together, and characterization tests record what old code does today before an agent changes it.
- Tests only catch what they check, 100% coverage can still hide a bug, and a stuck agent can weaken or delete a failing test, so read the test diff first.
Early one Friday morning, a customer ordered a birthday cake on Crumb & Co.'s website for pickup at 9:00 the next day. The website said yes. When they showed up on Saturday, Rosa had to tell them there was no cake, because the kitchen had baked Saturday's cakes hours before their order came in.
The strange part is that the code behind that yes was brand new. A coding agent wrote it, and every one of its tests passed. In this article, we'll look at why those tests missed the bug, and how a few minutes of work before we ask for the code gives every future change something to land on.
Onwards!
A New Rule With Three Green Checks
Wren builds our bakery's website (the same one whose pickup code an agent fixed in How an Agent Like Claude Code Works), mostly by asking coding agents to do the typing. Odette, who runs our kitchen, has a new rule for celebration cakes. She bakes the layers early on the day before pickup, so a cake for Saturday has to be ordered by Thursday.
Wren asks our agent for the rule, and a few minutes later, it reports back:

Here is the code our agent wrote, along with its three tests:
// src/cakes.js
const DAY = 24 * 60 * 60 * 1000;
export function canOrderCake(orderedAt, pickupAt) {
const days = Math.ceil((new Date(pickupAt) - new Date(orderedAt)) / DAY);
return days >= 2;
}
// test/cakes.test.js
test("allows a cake 2 days out", () => {
expect(canOrderCake("2026-12-07T10:00", "2026-12-09T10:00")).toBe(true);
});
test("rejects a cake 1 day out", () => {
expect(canOrderCake("2026-12-07T10:00", "2026-12-08T10:00")).toBe(false);
});
test("counts a partial day as a full day", () => {
expect(canOrderCake("2026-12-11T08:00", "2026-12-12T09:00")).toBe(true);
});
Our agent measures the hours between the order and the pickup, divides by 24, and rounds up to whole days. All three tests pass, Wren skims the change, and it ships.
Here is what happens to our customer's order:

Odette baked Saturday's cake layers at 5:00 on Friday morning, and the order came in at 8:15. Why did our website accept it? From 8:15 on Friday to 9:00 on Saturday is 24 hours and 45 minutes, and here is how our agent's code turns that into days, next to how Odette counts them:

Rounding up turns 1.03 days into 2, so a Friday order will sneak through for Saturday whenever it arrives a little more than 24 hours ahead. Odette counts calendar days, so for her, any Friday order for Saturday is too late.
Grading Its Own Homework
How did three passing tests miss this? Take another look at our third test. A Friday 8:00 order for Saturday at 9:00 is almost exactly our customer's order, and the test expects true. One of our tests is checking that the bug is there!
Our agent wrote its tests after its code, from the same idea of the rule, so its belief that 2 days meant hours divided by 24 and rounded up went into the code and into the tests at the same time:

Look at where the arrows start. Our code and our tests both come from our agent's idea, so all our tests can tell us is that the code agrees with itself. We saw the same thing in Spec-Driven Development, where eleven passing tests let 14 orders into a pickup slot that fits 6.
It's a lot like letting a student grade their own homework, or like a trapeze artist who strings up the safety net after the show, only under the spots where they landed:

That net will catch a fall in the two spots the performer already tried, and our Friday morning bug isn't in either of them.
Writing the Test First
What if our tests came from Odette's rule instead? Wren deletes our agent's three tests and, before asking for a fix, writes down what right means as a few examples, each with a real order time, a real pickup time, and the answer Odette would give:
// test/cakes.test.js, written by Wren
// Odette bakes cake layers early on the day before pickup,
// so a cake has to be ordered by the day before that.
test("Monday is in time for Wednesday", () => {
expect(canOrderCake("2026-12-07T10:00", "2026-12-09T10:00")).toBe(true);
});
test("Thursday, even late at night, is in time for Saturday", () => {
expect(canOrderCake("2026-12-10T23:55", "2026-12-12T09:00")).toBe(true);
});
test("Friday morning is too late for Saturday", () => {
expect(canOrderCake("2026-12-11T08:15", "2026-12-12T09:00")).toBe(false);
});
test("Tuesday is too late for Wednesday afternoon", () => {
expect(canOrderCake("2026-12-08T07:00", "2026-12-09T14:00")).toBe(false);
});
Wren didn't run any code to get these answers. They come straight from Odette's rule, and the middle two tests are the edge Wren cares about most, where the time of day shouldn't change the answer.
Next, Wren runs these tests against the code that's live right now:

Two of our tests fail, for the same reason our customer didn't get a cake, and that is exactly what we want. Seeing a test fail on the buggy code proves that it can catch the bug.
Writing a test from what we mean, watching it fail, and only then writing (or asking for) the code that makes it pass has a name. It's called test-driven development (aka TDD), and Kent Beck wrote the book on it, Test-Driven Development: By Example, in 2002. His Canon TDD post even lists our agent's move as a mistake: copying the values the code computes into a test's expected answers, because "That defeats double checking."
With an agent doing the typing, our loop is going to look like this:

Our agent only owns step 3, and Wren owns everything else, including the tests. Here is how Wren will hand over the job:
Make the tests in test/cakes.test.js pass. Don't edit
anything in test/. If you think a test is wrong, stop
and tell me why instead of changing it.
Anthropic's best practices for Claude Code suggest the same shape, with sample prompts that end in "write a failing test that reproduces it, then fix it". Our agent's first try swaps rounding up for a strict 48 hours, and here is the next run:

Now the rule is too strict, since Thursday at 11:55pm to Saturday at 9:00 is only 33 hours. Our agent reads the failure, switches to comparing calendar days, and runs our tests again:

All four pass. Rounding up let late orders in, while a strict 48 hours turned on-time orders away, and Wren's examples caught both because they pin down Odette's rule from both sides.
The Net Stays Up
Our tests don't go away once the cake rule ships. They will run on every change from now on (which makes them regression tests, like the ones we used in Regression Tests on Every Prompt Change). A few weeks later, Wren asks an agent to move all of our date math into one shared helper that counts hours, and here is what happens before anything ships:

The agent doing our cleanup never heard about the birthday cake. It didn't need to, because Wren's test checks Odette's rule on every change, even one that has nothing to do with cakes.
Three Kinds of Tests
Our cake tests are one kind of test, and there are two more kinds we will want when agents do our typing.
Unit Tests
Our cake tests check one function all by itself, with no website, server, or database involved:

A test that checks one small piece of code on its own is called a unit test. Unit tests run in milliseconds, so we can run hundreds of them on every save, and they're the right tool for rules with one right answer, like dates, prices, and limits. If you've never set up a test runner, Testing React Apps walks through one.
Integration Tests
A passing unit test doesn't promise that the rest of our website calls our function correctly. Our order page and Biscuit's check_pickup_slots tool both ask the slots API which pickup times to offer:

If the slots API forgot to call canOrderCake for cakes, or passed it the two times in the wrong order, every unit test would still pass. A test that runs these pieces together, the way a customer's request really travels, is called an integration test. These are slower, since they start a server and a test database, so we'll write a few for the paths customers really take.
Characterization Tests
The third kind is for code we don't fully understand. Wren's next job for our agent is to rework src/pickup.js, which has picked up a bunch of small rules that nobody fully remembers. So before our agent changes a line, Wren has it call our slot code for a handful of dates and save whatever comes back as tests:

These are called characterization tests, a name Michael Feathers gave them in his 2004 book, Working Effectively with Legacy Code, and in a 2016 post, he described their purpose as "to document your system's actual behavior". If the rework changes any of these answers, one of our tests will go red, and Wren decides whether that change was the point.
Notice something funny. Copying the code's answers into a test is the exact mistake our agent made with the cake rule! For new behavior, we want a test to protect what we meant. For old code we're about to change, we want it to protect what the code does today, bugs and all. In our last row, the slot code offers pickups on Christmas Day. Is the bakery even open then? That test stays put until the owner answers.
Keeping the Net Honest
The first hole in our net is that tests only catch what they check, and our agent's original three tests show it:

Those tests ran every line of our agent's canOrderCake, so a coverage report would have said 100%, with the Friday morning bug still inside. As Martin Fowler wrote in 2012, coverage is "of little use as a numeric statement of how good your tests are".
The second hole is our agent itself, because an agent stuck on a failing test can make it pass the wrong way. In February 2025, Anthropic's system card for Claude 3.7 Sonnet described it like this:
During our evaluations we noticed that Claude 3.7 Sonnet occasionally resorts to special-casing in order to pass test cases in agentic coding environments like Claude Code.
The model most often returned the exact values the tests expected, and sometimes it changed the tests instead. Anthropic's May 2025 system card says Claude Opus 4 and Claude Sonnet 4 still do it, just a lot less often, and Kent Beck watched for an agent "disabling or deleting tests" while building a library with one. Here is what these moves look like in a diff:
This is why we treat our test folder as off-limits for agents, and a few habits help us keep it that way:
- In every pull request, we read the test changes first.
- We run the tests ourselves instead of trusting the agent's summary.
- We can add a hook (Claude Code's docs show one) that refuses edits to the folder, whatever the model decides.
When one of our tests really is wrong (like our agent's old counts a partial day as a full day test), our agent will say so...and Wren changes it.
Try It This Week
Pick the next small change you were going to hand to an agent, and let's string up its net first:
- Write down three or four real examples with the exact answer you expect, including the edge case you'd hate to get wrong.
- Turn them into tests and run them. Make sure they fail, because the code doesn't exist yet or because it has the bug.
- Ask the agent to make the tests pass without editing them, and to tell you if it thinks a test is wrong.
- Before you merge, read the test diff first, then run the tests yourself.
Frequently Asked Questions
Should I Let an AI Agent Write My Tests?
An agent can write tests, but tests it writes after its own code come from the same understanding as the code, so they can only show that the code agrees with itself. In our cake example, one of the agent’s tests even checked that the bug was there. Write the examples that define right yourself, from what you actually mean, and have the agent make them pass.
How Do You Use Test-Driven Development With an AI Coding Agent?
Write a few tests from real examples of what you want, including the edge case you care about most, and run them to watch them fail. Then ask the agent to make them pass without editing the tests, and to tell you if it thinks a test is wrong. Before you merge, read the test diff first and run the tests yourself.
What Is a Characterization Test?
A characterization test records what existing code does today: we run the code on a handful of inputs and save whatever comes back as the expected answers. Michael Feathers named them in his 2004 book, Working Effectively with Legacy Code. They let an agent change code nobody fully understands, because any change in behavior turns a test red, and a person decides whether that change was the point.
How Do I Stop an AI Agent From Changing My Tests?
Tell the agent not to edit the test files, and to stop and explain if it thinks a test is wrong. Read every change to a test file before anything else in a pull request, and run the tests yourself instead of trusting the agent’s summary. Claude Code can also block edits to a folder with a hook, so the edit is refused no matter what the model decides.
Conclusion
Ok, it's time to wrap things up. We started with a birthday cake that never existed and three passing tests that could only tell us our agent's code agreed with itself. When Wren wrote the tests first, from Odette's rule, they became a spec our agent had to satisfy, and they will sit under every future change like the net under a trapeze.
That net has holes, though. Tests only catch what they check, coverage doesn't measure correctness, and a stuck agent might weaken a test without telling us, so we'll always read the test diff first. As for Odette, she still bakes Saturday's cake layers on Friday morning, and these days, every one of them has an order waiting for it.
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Chesterton’s Fence, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇


