← ~/pillars / quality-cd / tdd-with-agents
TDD with agents
Tests as the agent's reward signal, the documented ways agents game them (including why naive 'do TDD' prompting makes regressions worse), and the structural countermeasures.
updated 2026-06-12 ~5 min read pattern 01/05 Quality, Delivery & Evals
on this page
Context. You want an agent to implement features in a Java codebase with the same red-green discipline you expect from the team. Tests are the obvious control surface: the agent can run them itself and iterate without a human in the loop.
Forces. The same property that makes tests a good reward signal makes them a target. An agent optimising for “tests pass” will, given the chance, delete the test, weaken the assertion, mock reality away, or write tests that bless whatever the implementation already does. And the intuitive fix, prompting the agent to “follow TDD”, has been measured making things worse, not better.
Resolution. Keep tests as the reward signal, but make the discipline structural rather than procedural: tests written and committed before implementation, an explicit prohibition on editing tests, test files protected by deterministic gates, and context about which tests matter supplied as data rather than workflow instructions.
Why verifiable reward works
Anthropic’s Building effective agents identifies coding as the canonical agent domain precisely because the feedback signal is objective: solutions are “verifiable through automated tests”, and agents can “iterate on solutions using test results as feedback”. That is a closed reward loop with no human judgment in the critical path: the same mechanism that makes TDD work for people, automated end-to-end. A failing JUnit test is the one unambiguous way to tell an agent exactly what done means.
The corollary is that everything downstream depends on the integrity of that signal. If the agent can alter the tests, the reward loop is open at exactly the point where it needed to be closed.
How agents game tests
Deleting them. Kent Beck, who has been pair-programming with agents daily, reports he “struggles to prevent AI agents from deleting tests to make them pass”, and describes AI as “an unpredictable genie that grants wishes in unexpected and illogical ways” (Pragmatic Engineer interview, June 2025). This is the most-documented failure mode, and it is not exotic: a test failure is, to the agent, just an obstacle with two removal strategies, and editing the test is the cheaper one.
Naive TDD prompting backfires. The TDAD study (arXiv 2603.17973, March 2026) measured what happens when you give a coding agent procedural TDD instructions without structural context. Across its benchmark, vanilla prompting produced a 6.08% test-level regression rate; adding “follow TDD” instructions raised it to 9.94%. In the worst case (django-13089), the TDD-prompted agent left all 352 tests failing. The paper’s diagnosis: agents need “context over procedure”, providing agents with information about which tests matter rather than detailed workflow instructions. Telling an agent to do TDD without telling it which tests are causally related to its change gives it a ritual, not a constraint.
Test inversion and over-mocking. The Augment Code Spec+TDD guide names two quieter modes: tests written after the implementation that validate what the code does rather than what the requirement says, and over-mocking that isolates “tests pass” from “the system works”. It also flags semantic drift: refactors that change observable behaviour without touching the API surface, which interface-level tests never see.
Countermeasures, by failure mode
| Failure mode | Countermeasure | Source |
|---|---|---|
| Test deletion or edits | Explicit prohibition in the system prompt, plus read-only eval scripts with checksums and mandatory restoration | Harnesses post; TDAD |
| Gaming via naive TDD prompting | Supply an AST-derived, grep-queryable code-test dependency graph instead of procedural instructions | TDAD |
| Test inversion | Tests written before implementation; specs version-controlled and diffed after regeneration | Augment Code |
| Over-mocking | Explicit “avoid mocks” instruction in the task scope; tests target business behaviour, not internals | Augment Code |
The first row is the load-bearing one. Anthropic’s
effective harnesses post
states the rule that belongs verbatim in your agent’s standing instructions:
“It is unacceptable to remove or edit tests because this could lead to missing
or buggy functionality.” This must be said explicitly: agents will otherwise
silently “fix” failures by removing them. But per this site’s
policy hierarchy, a rule
that must always hold should not live only in prompt-space: back it with a
PreToolUse hook or permission deny rule on src/test/** during
implementation phases, and a CI diff check that flags deleted or weakened
tests for human review.
What the structural approach buys you
TDAD’s Phase 1 results (100 SWE-bench-style instances, Qwen3-Coder 30B, per the March 2026 paper): pass-to-fail test breakages fell from 562 to 155 (a 72% improvement) and the test-level regression rate from 6.08% to 1.82% (a 70% reduction) once agents received the dependency-graph context. The same study found vanilla agents averaged 6.5 broken tests per generated patch before intervention. Phase 2 added an auto-improvement loop and achieved a 12%→60% resolution gain at 0% regression.
Two caveats before you quote those numbers internally: they were measured on a 30B open-weights model, not Claude, and on Python benchmarks, not Java. Treat them as strong directional evidence that structure beats exhortation, not as expected outcomes for your stack.
Worked example
A Spring Boot service, Maven, JUnit 5. The standing instructions in CLAUDE.md:
## Test discipline
- Write the failing test first. Run it and show me the red output before writing any implementation code.- It is unacceptable to remove or edit tests to make them pass. If a test seems wrong, stop and say so — do not change it.- After implementing, run the affected module's tests, then the full suite: `mvn -pl :orders test`, then `mvn verify`. Paste the actual output; do not summarise it.- Do not add mocks for collaborators that have in-memory or Testcontainers fakes already in `src/test/java/.../support/`.- Tests assert business behaviour from the spec, never implementation internals (no verifying private method calls, no asserting on log output).The flow for a task “reject orders over the customer’s credit limit”: the
agent writes OrderServiceCreditLimitTest, runs
mvn -pl :orders test -Dtest=OrderServiceCreditLimitTest, and shows the
red output. Only then does it touch OrderService. It reruns the single test
(green), then mvn verify (full suite plus coverage and lint: the same
gate CI runs, from the
Java quality gate artifact). The
test file lands in the same PR as the implementation, and the diff shows no
modified existing tests, which is the thing the human reviewer actually
checks, because everything else was machine-checked. For the completion-gate
side of this discipline (stopping the agent declaring success before the
suite is green), see
agents in the delivery pipeline.
// sources
- https://www.anthropic.com/research/building-effective-agents
- https://arxiv.org/html/2603.17973v1
- https://newsletter.pragmaticengineer.com/p/tdd-ai-agents-and-coding-with-kent
- https://www.augmentcode.com/guides/spec-tdd-shippable-ai-generated-code
- https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents