Yeda AI Tips · #098

Español

Never Trust AI-Written Tests To Validate AI-Written Code

The tests pass. The code is still wrong. Here is the trap: your AI assistant wrote the code with a wrong assumption — then wrote the tests with the same wrong assumption. They agree with each other, and they are both wrong. A green suite proves consistency, not correctness.

Why one mind can't grade its own homework

Unit tests work as a safety net because they encode a second, independent opinion about what the code should do. When a human writes tests for their own code, that independence is already weakened — which is why code review and QA exist. When a single LLM session writes both sides, independence collapses entirely: the misreading of the spec that produced the bug is sitting right there in context, shaping every assertion the model writes next.

This isn't hypothetical. An empirical study of LLM test-generation workflows found that tests generated after faulty code detect only 14% of faults, versus 25% for tests generated independently of the implementation — the errors propagate, and "incorrect implementations and tests are mutually consistent, masking defects rather than revealing them." A second study measuring the influence of incorrect code on test generation found tests generated for incorrect code suffer a 47% worse bug-detection rate on repository-level examples.

The failure modes to watch for

Rules of thumb

SituationWhat to do
AI wrote code + tests togetherCheck the code against each stated invariant from the spec, not against its own tests
AI wrote the testsRead every assert line; verify expected values by hand or from the spec, never from program output
You need tests for AI codeGenerate tests from the requirements in a fresh session, without the implementation in context
Suspicious green suiteReproduce the behavior yourself with one manual run of the real thing
High-stakes logicAdd mutation testing or property-based tests as an independent referee

The unifying rule: the oracle — the source of "what's correct" — must come from outside the model run that produced the code. That's the spec, your own reproduction, or a mechanically independent check.

Power-user moves: mechanical independence

The bottom line

Never let AI tests be the only judge of AI code. Passing tests written by the same mind that wrote the bug is agreement, not evidence. Get your oracle from outside: the spec, your own hands, or a mutation-testing run that tries to kill the suite.

Resources

Building with AI coding assistants? Yeda AI designs, audits, and ships production LLM systems.

Talk to us · Read the blog