AI-POWERED TESTING

Using AI Agents to Explain a Test Failure You Did Not Write

CI is red on your pull request, and the failing test is one you have never seen. You didn't write it, the name doesn't tell you much, and the stack trace ends somewhere inside a shared fixture. The bug might not be hard. What's missing is context: what the test was meant to prove and why it ever passed.

This is a job where AI assistants do well, as long as you use them the right way. A chat assistant can read the test and its fixtures without getting tired and give you a plain-English summary. A coding agent such as Claude Code or Copilot in agent mode can go further: run the test itself, read the git history and check its own guesses. In both cases I treat the AI as a first-pass analyst, not as the one who fixes the test. Its job is to produce a hypothesis. My job is to check it.

Here is the workflow I use: what to give the assistant, the questions that get honest answers, the limits I set when an agent can run commands, and how to turn the explanation into a fix that doesn't quietly weaken the test.

Build an API Automation Framework With Node.js

Learn Node.js, Cucumber, GitHub Copilot, APIs, CI/CD, and modern automation by building a complete framework.

Learn more

Give it the test, the fixtures and the full trace

The most common mistake is pasting only the stack trace. A trace tells you where the code failed. It says almost nothing about what the test was trying to prove, and the model fills that gap with guesses that sound reasonable. A useful prompt has three parts:

  • The full test function, including decorators, parametrize markers and the fixture names it receives.
  • The fixture definitions it depends on, especially any that touch external state: an API client, a database seed or a token.
  • The complete traceback, not just the last line, so the call chain is visible.

Then ask for an explanation and nothing else:

Here is a failing pytest test, its fixtures and the full traceback.
1. In plain English, what behaviour is this test asserting?
2. What does the traceback say went wrong, and where?
3. List the two or three most likely root causes, most likely first.
Do not suggest fixes yet.

[test]
[fixtures]
[traceback]

Keeping fixes out of the first step is deliberate. If you ask for an explanation and a fix together, the model tends to settle on the first fix it thinks of and shape the explanation to justify it. Keeping the steps apart keeps the explanation honest.

Make sure the actual assertion line is in what you paste. When the check is hidden inside a helper or a custom matcher, the model can misread what is being asserted, and every later step will be slightly wrong. When the trace points into logging or request helpers, the habits in debugging failed API tests with better logging make the evidence much clearer for you and the model alike.

Ask whether the test or the code is wrong

Once you have an explanation, the real question is whether the test is wrong or the application is. With an inherited test you don't have the author's intent, so ask the model to argue both sides:

Given your explanation: what would have to be true about the application
for this test to be correct and meaningful? And what would have to be
true for this test to be outdated or badly written? Point to the specific
lines that support each case.

This stops the default answer of "the test is fine, the code broke." In practice the useful answers are things like an intentional status code change in a newer API version, a fixture seeding data in a shape the schema no longer accepts, or a test written against a mock that now runs against a real endpoint.

Watch for three weaknesses in what comes back:

  • Confident claims about intent. When the test name and the assertion disagree, trust the assertion.
  • Invented library behaviour. With internal utilities or less common libraries, the model may describe method signatures that don't exist. Check against the real source.
  • No knowledge of recent changes. A chat assistant doesn't know what changed in the last deploy. Give it the relevant diff yourself.

When the failure only happens in CI, code-level reasoning won't be enough. Combine the explanation with an environment check like the one in debugging a flaky API test that only fails in CI.

When an agent can run the test itself

A coding agent with terminal access changes the workflow. It doesn't have to guess from pasted text. It can run the test, read git log for the files involved, open the fixtures and check its own hypothesis. That's a real improvement, as long as you limit what it is allowed to do.

For an investigation, I tell the agent explicitly that it is read-only:

Investigate why tests/api/test_orders.py::test_refund_is_idempotent fails.
You may: run that single test, run git log / git show / git diff on the
files it touches, and read any file in the repo.
You may not: edit any file, change assertions, add skip or xfail markers,
or run the full suite against a shared environment.
Report: what the test asserts, the evidence you gathered (commands and
output), your best hypothesis, and one way to confirm or rule it out.

Asking for evidence matters most here. A report that shows the commands it ran and what they printed can be checked in a minute. A report that only gives conclusions can't. If the agent says "the fixture changed in the last commit," I want to see the git show output it is relying on.

Put the same limits in your repository instruction file, such as .github/copilot-instructions.md for Copilot or the equivalent file your agent reads, so you don't depend on remembering to type them each time. The rule I care about most is one line: never change an assertion or skip a test to make a failure go away; report the mismatch instead.

Turn the explanation into a fix you can trust

Once I have confirmed the hypothesis myself, I ask for a fix with the constraint stated up front:

The verified cause is: [hypothesis]. Suggest a fix that keeps the
original intent of the test. Do not loosen the assertion, widen an
expected value, or skip the test. If the right fix belongs in the
application code rather than the test, say so and stop.

The last sentence matters because assistants default to fixing whatever you handed them, and you handed them the test. The worst outcome is a "fix" that relaxes an assertion just enough to turn the test green while the real bug ships.

A few habits make this reliable:

  • Run the test in isolation before and after. pytest tests/api/test_orders.py::test_refund_is_idempotent -v confirms it was failing for the reason you think and passes for the right reason afterwards.
  • Prove the test can still fail. Temporarily break the behaviour it protects, or change the expected value, and confirm it goes red. A test that can't fail protects nothing.
  • Check the fixture's other users. If shared fixture state was the cause, other tests may be passing for the wrong reason.
  • Leave the explanation in the PR. A short summary of the cause and the evidence helps the reviewer and the next person who inherits this test.

Used this way, the assistant speeds up rebuilding context, and you still own the conclusion. If you want to practise this with a real API suite, Copilot and the full test loop, my API testing courses are built around it.