I read Matteo Vaccari’s “Tests as Data” on martinfowler.com this weekend, and since it touches the same problem as my earlier post about TDD and mutation testing with agents, I wanted to see how much of it I could turn into the prompt of the agent that writes and reviews tests in my own setup. This is a write-up of that, mostly because the part that needed the most work was one the article doesn’t spend much time on, which is where the reviewer gets its idea of what kind of change it’s looking at.
What the article asks for
The short version, as I understood it, is that when an agent writes both the code and the tests, the tests end up confirming whatever the code does, bugs included, and they move every time the code moves. Among the things the article suggests against that, the one that stuck with me is a check on which kinds of files each kind of task is allowed to touch. A refactor changes production code and leaves the test assertions alone, coverage work adds tests and leaves production code alone, and so on, so if a “refactor” also edits an expected value, something is off.
When I compared that with what I already had, a couple of things were covered (mutation testing, and a rule that a test is never rewritten to match the code), but the test agent itself was the generic persona most of us start with, an “elite Testing and Automation Specialist” whose job was to create “comprehensive” test suites covering happy paths, edge cases, performance and security, with a framework list at the bottom. Nothing in it said where an expected value should come from, or what a test should never change.
The first rewrite
The rewrite turned the persona into a description of what a good test is, roughly:
- Black-box. Drive the unit through its public surface and assert on what it returns, stores or emits, “input X gives output Y”, never “A calls B”.
- An independent oracle. Every expected value comes from somewhere other than the code under test (the spec, docs, a hand calculation shown in the test, a reference implementation), and never from running the code and pasting its output in.
- Data over code when cases share a shape, so one table with a row per case, which makes the missing case visible and a new case cost one row.
- Readable output for large results, compared against a checked-in expected file so a failure reads as a diff.
- Properties beside the examples for things like round-trips and orderings.
And, for reviewing, the article’s table of which files each kind of change may move. The first version looked complete to me, and it was about the size I expected, so I ran the usual review over it before opening a PR, eight reviewer agents over the branch. They came back with 21 findings, nothing critical, which collapsed into six causes once deduplicated. A few of them are the reason for this post.
The reviewer was trusting the author
The biggest one was that the reviewer took the kind of change from the commits, and the author writes the commits. So the whole check, the one the table exists for, could be switched off by one line in a commit message saying “fix: handle the empty case”, since a bug fix is allowed to change existing expectations. When the author is an agent that just loosened a test to make it pass, that’s exactly the line it would write (not out of malice, it really does believe it fixed a bug).
The fix was to take the kind of change from the problem statement instead, which is the issue, ticket or plan the change is supposed to serve, and to treat a behaviour change that only the commits claim as a finding by itself:
Take the kind of change, and any change in behaviour, from the brief’s problem statement (the issue, ticket, or plan), not from the change’s own commits. A behaviour change the commits claim and the problem statement does not name is a finding.
This is the same idea the article starts from, applied one level up. The tests need an oracle that doesn’t come from the code, and the check on the tests needs an oracle that doesn’t come from the author, otherwise it’s grading the author’s homework with the author’s answer key.
The table had holes
The rest of the findings were mostly about the table itself, and they’re worth knowing if you build one.
The most obvious one, once someone pointed it out, is that the table only looked at expected values. You can get the same rewrite past it by keeping the expected value and changing the input (or a fixture feeding it) until the buggy code produces the old expectation again. So a fixture that feeds a case’s input now counts as part of that case, not as harness code.
It also had no row for some ordinary, legitimate kinds of change. Deleting a feature deletes its tests, which the “deleted case” flag treated as suspicious. A test that had a wrong expected value all along, fixed on its own, fit no row. And a migration from one test framework to another changes every test file while meaning to change none of them. The final table ended up with six rows:
| Kind of change | Production code | Existing test cases (inputs, assertions, expected values and files) | Test harness |
|---|---|---|---|
| New behaviour or bug fix | changes | change only where the stated behaviour changed; new cases added | may change |
| Behaviour removal | the behaviour is deleted | cases of the removed behaviour deleted; the rest unchanged | may shrink |
| Refactor | changes | unchanged in meaning | unchanged in meaning, unless a renamed API forces it |
| Coverage work | unchanged | unchanged; new cases added | may change |
| Test correction | unchanged | a wrong case corrected, each changed value naming its independent source | may change |
| Test-tooling work, a framework migration included | unchanged | unchanged in meaning | changes |
Strengthening an assertion is fine in every row; loosening or re-baselining one is what gets flagged. That rule came out of another finding, where the reviewer would ask for a stronger assertion and then, on the re-review of a refactor, flag the stronger assertion as a forbidden test edit. It’s also the case mutation testing can’t help with, since a loosened test still fails under a mutant, just under fewer of them, so the table is the only thing looking for it.
Other things a prompt does that you didn’t mean
A few findings had nothing to do with the article and everything to do with writing one prompt for an agent that does two jobs. The “prove each assertion can fail with mutation testing” rule sat in the shared list of what a good test is, and the review questions held every test to that list, so a reviewer could decide to run mutation checks in the author’s working tree, which is slow and also not a reviewer’s business. Review mode is now read-only (no edits, no test runs, no running the change’s code), and an unclear brief defaults to it.
The expected-file rule said the file is one “a person approved once”, but in practice the writing agent records the file and then considers it approved, which is the run-it-and-paste-the-output oracle the same prompt bans two paragraphs earlier. A self-recorded expected file is now only a proposal until someone reviews it. And asking for a property test “for anything with an invariant” pushed the agent to add a property-testing library to repos that didn’t have one, so properties are now only for invariants the requirement names, with the repo’s own tools, and a missing library is something to mention, not install.
Fixing all of that caused three new problems in the re-review, which I should’ve seen coming. Banning working-tree reads made the reviewer blind in the two places that review uncommitted work, fixtures ended up listed under two columns with opposite rules, and the review skill that runs this agent never passed it the problem statement it was now told to classify from (a second review skill writes its own briefs, so it needed the same fix separately). The prompt went from 68 lines to about 100, roughly 1.4k tokens that load on every review, which I think is a fair price for what it checks.
Summary
Most of “Tests as Data” translates into a prompt pretty directly, and the parts about black-box assertions, independent oracles and tables are things any test agent should be told. The file-kinds table is the useful new check, but only if the reviewer learns the kind of change from somewhere the author doesn’t control, the issue or the plan, because a check whose input is the author’s own description of the change can be turned off by that description. It also has to cover changed inputs and fixtures, not only expected values, and it needs rows for removing behaviour, correcting a wrong test and migrating tooling, or it will flag honest work and let the loosened test through.