Using AI to Spot Redundant Tests Before Agents Bloat Your Suite
Test suites grow slowly at first and then all at once. Tests get added faster than anyone reviews them, nobody has time to audit hundreds of files by hand, and eventually CI is slow and nobody can say which tests are pulling their weight. Redundancy is usually the cause: the same behaviour checked three times under three names.
Coding agents make this worse and better at once. GitHub Copilot, Claude Code and similar tools can produce a dozen tests in the time it used to take to write one, and they rarely check what already exists. The same tools are also good at the boring pattern-matching it takes to find duplicates: reading a module and telling you that three tests assert the same thing with different variable names.
In this article I'll cover how I prompt for redundancy, why I back the AI's opinion with coverage data before deleting anything, and how to stop agent-written tests from adding duplicates in the first place.
Learn Python, Behave, GitHub Copilot, APIs, and CI/CD by building a real framework you can finish in a weekend.
What AI sees that a coverage report doesn't
An assistant reading your tests isn't running them. It's comparing what the test code says it checks. That catches a different kind of redundancy from a coverage tool. Coverage tells you a line was executed. A model can tell you two tests execute that line for the same reason. Take these two:
def test_create_user_returns_201(api_client):
response = api_client.post("/users", json={"name": "Alice", "email": "alice@example.com"})
assert response.status_code == 201
def test_post_to_users_endpoint_succeeds(api_client):
payload = {"name": "Bob", "email": "bob@example.com"}
response = api_client.post("/users", json=payload)
assert response.status_code == 201
Both check that a valid POST to /users returns 201. The different names and payloads add noise, not coverage. Ask an assistant "Which of these tests assert the same behaviour? Explain what, if anything, differs" and it will find this pair right away.
The prompt that scales better groups tests by intent before comparing them:
These are API tests for a user management service.
For each test, state in one sentence which API behaviour it verifies
(status, body shape, error message, side effect).
Then group tests that verify the same behaviour, and for each group
say whether any test in it would catch a bug the others would miss.
Asking for intent first catches pairs that look nothing alike but both check that a missing required field returns 400. That kind of duplicate builds up when different people write tests in different sprints, and it's common in suites that test missing-field handling.
Don't delete on the model's word alone
The model will sometimes call two tests redundant when they check slightly different edge cases. Before deleting, I ask: "What would have to be true about the implementation for these two tests to catch different bugs?" That makes it justify the claim, and the answer often shows that one test is worth keeping.
Then I check with actual execution data. coverage.py can record which test executed each line, using dynamic contexts. With pytest-cov:
pytest tests/api --cov=app --cov-context=test
coverage html --show-contexts
The HTML report then shows, line by line, which tests reached it. If the AI says two tests are duplicates and both reach exactly the same lines with the same inputs, that's strong evidence. If one reaches a branch the other doesn't, it isn't redundant, whatever the names suggest. For API tests running against a separate server, line contexts only work if you measure the server process, so in that setup I rely more on the intent grouping and on comparing assertions.
Often the right answer is neither "keep both" nor "delete one" but merging them into a parametrized test. The data rows stay explicit and the repeated code goes:
import pytest
@pytest.mark.parametrize("payload", [
pytest.param({"email": "a@example.com"}, id="missing-name"),
pytest.param({"name": "A"}, id="missing-email"),
pytest.param({}, id="empty-body"),
])
def test_create_user_rejects_missing_required_fields(api_client, payload):
response = api_client.post("/users", json=payload)
assert response.status_code == 400
Ask the assistant "If I merged these into one parametrized test, would any case lose coverage?" and then read the merged version yourself. The patterns in structuring pytest parametrize for API edge cases help keep those tables readable.
Stopping agents from adding duplicates
Cleaning up once feels good, but if agents keep adding tests without looking, the duplicates come back. The cheaper fix is at the source. When an agent writes tests, it should check what already covers the behaviour before adding anything. I put this in the repository instruction file (.github/copilot-instructions.md for Copilot, or whatever file your agent reads):
## Adding tests
- Before writing a new test, search tests/ for existing tests of the same
endpoint and behaviour. Extend an existing parametrize table instead of
adding a near-copy.
- In your summary, list each new test and the existing test closest to it,
and say what the new one catches that the existing one does not.
The second rule is the useful one. It turns a vague "added 12 tests" into a list a reviewer can check quickly. A test whose justification is "covers the same case with different data" either becomes a parametrize row or gets dropped before merge.
For human-written tests I use the same check as a pre-PR prompt. Paste the new tests next to the existing module and ask which new ones duplicate existing coverage and what would make them add something. It takes a couple of minutes and catches the most common source of bloat.
Make the habit stick
Three small things keep this going without a quarterly cleanup:
- Keep the prompts in the repo. A
prompts/folder with the intent-grouping and pre-PR prompts means everyone runs the same check. Review changes to them like code. - Add a line to the PR template. "New tests checked against existing coverage" makes the practice visible, whether a person or an agent did the check.
- Look at near-duplicates, not only exact ones. Pairs that differ by a null, an empty string or a boundary value are usually best merged into one parametrized test, not deleted.
The goal isn't fewer tests. It's a suite where every test catches something the others don't. Deciding what to cut when a test is wrong rather than redundant is a related judgment call, covered in when to delete tests instead of fixing them. If you want to build a suite like this from the start, with Copilot in the loop, that's what my API testing courses teach.