concord ai

Can you trust tests written by AI

Learn why tests written by the same agent that wrote the code can pass while proving nothing, and how to write tests that actually guard against bugs.

Alex Choi, AI Engineer5 min read

the short answer

No. A test written by the same agent that wrote the code can pass while proving nothing because it encodes the agent's reading of the task, including any misreading.

Abstract technical illustration for the article Can you trust tests written by AI

You asked an agent to write a function and its tests. The tests pass. Do you trust them

No, not if the same agent wrote both the code and the tests. The test encodes the agent’s reading of the task, including any misreading. If the agent misunderstood the requirement, its test will verify the wrong behaviour, and both will pass.

Where AI-written tests go wrong

Three patterns show up repeatedly in tests written by the agent that wrote the code. Learn to recognise them so you can spot the false positives before they reach review.

Assertions that mirror the implementation

The most common pattern is an assertion that checks the exact output the code computes rather than the observable behaviour the code is supposed to provide.

For example, an agent writes a function formatDate and its test. The test asserts that the function returns the string "2025-06-04" for the input 2025-06-04T12:00:00Z. That assertion passes whether the function implements the intended ISO-8601 formatting or simply echoes the input. The test guards nothing.

The correct test asserts that the output string parses as an ISO-8601 date and represents the same instant as the input. That assertion fails if the function hardcodes the wrong format or drops the timezone.

Mocks that replace the behaviour under test

Agents often replace the behaviour they are supposed to test with a mock that returns the expected result. The mock makes the test pass, but it does not exercise the real code path.

In a recent change, an agent wrote a function that fetches a user’s permissions from a database and its test. The test mocked the database client to return {"admin": true} for every call. The test passed even though the real implementation had a typo in the SQL query that would have returned an empty result set in production.

A mock is appropriate for isolating the unit under test, not for replacing the unit itself. If the mock’s return value is hardcoded to the desired outcome, the test verifies nothing about the real behaviour.

Tests that only cover the happy path

Agents optimise for green tests, so they tend to generate cases that exercise the code when everything works and skip the cases that expose bugs.

A function that validates a user’s email address might get a passing test for "alice@example.com" and nothing else. The test passes, but the implementation crashes on "alice@.com" or returns true for "alice@example".

Edge cases and error paths are where most production bugs hide. A test suite that omits them gives you no protection when the code meets those cases in the wild.

Write the expected behaviour first

The only reliable way to prevent the agent’s misreading from slipping into the test is to write the expected behaviour before you write the code. State the requirement in a form you can test, then implement the code against that requirement.

Start with a failing test that encodes the observable behaviour you want. If the test passes immediately, you know the test is wrong, not the code. Only when the test fails with the correct implementation should you accept it.

For example, define the requirement as "the function must accept a UTC ISO-8601 string and return a string that parses back to the same instant". Write a test that asserts this property for a range of inputs. The test will fail until the implementation correctly handles parsing and formatting.

This approach turns the agent’s potential misreading into a visible failure. You catch the misunderstanding before it reaches review, not after.

What to do with AI-written tests

AI can still help write tests, but treat its output as a first draft, not a final artefact. You need to validate the test against the intended behaviour, not the implementation it happened to generate.

Use the agent to generate candidate tests quickly, then review each one. Ask yourself whether the test asserts the observable behaviour the code is supposed to provide. If it asserts implementation details or uses mocks that bypass the real behaviour, delete it and write the test you actually need.

When you write the expected behaviour first, you create a clear standard for the agent’s tests to meet. Any test that does not match that standard is obviously wrong, and you can reject it without reading the code.

This discipline costs time up front, but it saves more time later. A failing test that exposes a real bug is worth ten passing tests that guard nothing.

What about code review tools

Some AI code review tools run additional tests on your changes. These tools can catch issues the original tests missed, but they do not solve the problem of tests that encode the agent’s misreading.

If the tests themselves are wrong, no downstream tool can fix them. The root problem is in the test’s assertions, not in the reviewer’s tools. Write tests that guard the right behaviour, and the review tools will have a solid foundation to build on.

For a deeper look at what review tools can and cannot catch, see What AI code review tools catch and what they miss.

Handing off work to another agent

When you hand a task from one agent to another, the second agent inherits the first agent’s context, including any misreadings baked into the tests. The new agent may update the code but keep the flawed tests, so the same false positives persist.

To prevent this, use a shared workspace that records every agent’s assumptions and decisions. You can inspect the task state and the test intentions before accepting the handoff. If the tests encode a misreading, you can reject the handoff or correct the tests before they reach review.

Overlap warnings arrive when an agent claims work, which the shared workspace records for every agent.

For more on handing off work without losing context, see AI coding mistakes to avoid when handing off tasks.

can you trust tests written by aiai written tests reliabilitytests written by ai agentsai test generation pitfallscan ai write good tests

Common questions

Why do tests written by the same AI agent that wrote the code fail to catch issues
The agent encodes its own reading of the task into the test, so it can end up verifying the implementation rather than the intended behaviour. Mocks may replace the behaviour under test, and assertions often mirror the implementation details.
What are the common patterns in unreliable AI-generated tests
Assertions that mirror the implementation, mocks that replace the behaviour under test, and cases that only cover the happy path. These patterns mean the test passes for the wrong reasons.
How can I write tests that actually catch bugs
Write the expected behaviour first and check that a test fails when the code is broken. This forces the test to guard against deviations from the specification rather than the implementation.
Should I avoid using AI to write tests
Not necessarily. Use AI to generate candidate tests, but validate them by hand against the specification. The agent’s output is a starting point, not a replacement for human review.
What role should AI play in writing tests
AI can draft tests quickly, but you must ensure they test the right behaviour. A test that passes because it mirrors the code does not protect against bugs introduced later.

written by

Alex Choi, AI Engineer

Builds tooling for teams whose code is mostly written by coding agents.

Give your agents one shared work-state.