Tests with an agent: how not to let it fit the test to the code
A test is a requirement written as code, so whoever writes the implementation must not adjust it: the shortest path to green is changing the test. Work test-first: expectations come from the specification, the test runs before the implementation and fails for the expected reason, and tests stay untouched during implementation. Test quality is checked by deliberately breaking the code; coverage is not proof.
Agentic development · The cycle
The test failed, the agent "fixed" it and now it is green. But does the feature work?
If you give an agent a failing test and the code, the easiest path to green is to change the test. It is not ill intent but a consequence of the task "make it pass". The test checked that the response contains a total of 120, the code returned 118. The agent replaced the expectation with "a number greater than zero" and CI went green. The calculation bug stayed and the signal about it disappeared. So work with tests needs its own rules.
How it works and why. A test is a requirement written as code. If one party writes both the requirement and the implementation in one session, the requirement adapts to the implementation, because that is the easiest way to satisfy "passes". The only reliable defence is to separate the roles. The expected behaviour is defined by a human or a specification, not by the current code, and the agent writing the implementation may not touch the tests.
What fitting looks like.
- an exact value replaced with "not empty" or "greater than zero";
- a test deleted, commented out or marked skip;
- a check wrapped in
try/catchor a condition that never runs; - the very thing that should be tested is mocked;
- a snapshot updated without analysing the difference;
- the expected value taken from the code's current response: the test says "the code does what it does" and cannot fail;
- a failure "cured" with pauses and retries instead of finding the cause.
Test-first with an agent
Example: from fitting to test-first. The fitted test from the example above:
// was: the test asserted a total of 120
// after the agent's "fix"
test('calculates the cart total', async () => {
const res = await calcCart(items);
expect(res.total).toBeGreaterThan(0);
});
This test passes for any positive total, including a wrong one. The right order: a human (or a specification) fixes the expectation, the agent writes the test, and you read it and run it before the implementation.
// expectation from the spec: 2 × 50 + shipping 20 = 120
test('calculates the cart total with shipping', async () => {
const res = await calcCart([item(50, 2)], { shipping: 20 });
expect(res.total).toBe(120);
});
A run without the implementation should give "expected 120, received 118" (or another reason you predicted). If the test is green before the fix or fails for another reason (an import error), it is not ready. Only then does the agent get the task "fix the implementation, do not touch the tests". A change to a test file in such a diff is visible at once. If the agent considers the test wrong, it stops and explains why, and a human decides.
Checking test quality. Break the code on purpose (return a wrong value, remove a condition): if the test did not fail, it is weak. Mutation testing tools (Stryker for JavaScript and TypeScript, PIT for Java, mutmut for Python) do this systematically. A coverage percentage is not proof: a line being executed does not mean its result was checked.
Limits and automation. An instruction file can say "do not change existing tests without permission", but that is context, not enforcement. For stricter control a hook or CI tracks whether test files changed in tasks where they should not have and requires separate approval. Hooks and permissions are covered in the section on control. A test does not always have to come before the implementation: for exploratory work in unknown code a prototype is useful first, and tests are fixed after the behaviour is confirmed against requirements.
What an agent does well. Proposes boundary values and negative scenarios, generates test data, diagnoses failures from a log, ports repetitive tests. All of it is checked the same way: will the test really fail when the behaviour is broken.
Common mistakes.
- Giving the agent both the test and the code with the task "make it pass": the shortest way to green is to change the test.
- Taking the expected value from the code's response: the test cannot fail and proves nothing.
- Not running the test before the implementation: a green test that was never red may check nothing.
- Treating coverage as proof of quality: an executed line without a result check proves nothing.
Check yourself
After the task "fix the implementation" the diff contains a change to a test file. What do you do and why?
Answer
Stop and look at what changed. If an expectation was weakened, revert the test and ask to look for the cause in the code. If the test really is wrong, the agent must explain why, and a human decides on changing the requirement, because a test is a requirement.
Why can a test written by an agent together with the implementation be green and check nothing? How do you detect it?
Answer
The expected value could have been taken from the code's current response. Detect it by running the test without the implementation (it must fail for the expected reason) or by breaking the code on purpose and seeing whether the test turns red.