AIExplore
Explain a Failing Test With ChatGPT
Share the test code, the failure output, and the code under test so ChatGPT can explain why expected and actual values do not match.
A failing test tells you something is wrong, but it does not always tell you why. The red line in your terminal says expected 4 but received 3, and you stare at the function wondering which branch went sideways. You could read the code again, or you could hand the puzzle to ChatGPT with enough context to solve it.
The key is giving ChatGPT three things at once: the test itself, the failure output, and the code the test calls. When all three land in one message, ChatGPT can trace the path from input to output and explain where the logic breaks. Miss any of the three and the answer will be a guess.
What explaining a failing test means
You are not asking ChatGPT to fix the code. You are asking it to read the test, read the function, and tell you a story about why the result differs from what the test expected. That story is the diagnosis. Once you understand the story, writing the fix is usually the easy part.
When this approach is useful
- The test passed yesterday and you cannot see what changed
- The assertion message is confusing or too terse to act on
- The function under test has several branches and you are not sure which one fires
- You inherited the test suite and do not yet know the codebase well
- You want a second reader before you start rewriting logic
Prompt for a Jest test with a wrong return value
Test file (sum.test.js):
test('adds tax to subtotal', () => {
expect(addTax(100, 0.08)).toBe(108);
});
Failure output:
Expected: 108
Received: 100.08
Code under test (sum.js):
function addTax(subtotal, rate) {
return subtotal + rate;
}
Task: explain why the test fails and what the function is doing wrong. Do not fix it yet.Why this prompt works
It includes all three pieces. ChatGPT can see that the function adds the rate directly instead of multiplying it by the subtotal. Without the code under test, ChatGPT would have to guess what addTax does, and the guess might match a different bug entirely.
Prompt for a Python assertion error with fixtures
Test file (test_discount.py):
def test_bulk_discount(sample_cart):
result = apply_discount(sample_cart, threshold=5)
assert result.total == 45.00
Fixture (conftest.py):
@pytest.fixture
def sample_cart():
return Cart(items=[Item("Widget", 10.00)] * 5)
Failure output:
AssertionError: assert 50.0 == 45.0
Code under test (pricing.py):
def apply_discount(cart, threshold=10):
if len(cart.items) >= threshold:
cart.total = sum(i.price for i in cart.items) * 0.9
else:
cart.total = sum(i.price for i in cart.items)
return cart
Task: explain why the discount is not applied. Walk through the values step by step.Prompt for an integration test that times out
Test (test_api.js):
it('returns user profile', async () => {
const res = await request(app).get('/api/profile').set('Authorization', token);
expect(res.status).toBe(200);
}, 5000);
Failure output:
Timeout - Async callback was not invoked within the 5000 ms timeout.
Route handler (routes/profile.js):
router.get('/api/profile', auth, async (req, res) => {
const user = await User.findById(req.userId);
if (!user) return;
res.json(user);
});
Task: explain what could cause the timeout. Focus on the route handler logic.What to give ChatGPT
- The full test including the assertion line
- The exact failure message from the terminal
- The function or module the test calls
- Any setup such as fixtures, mocks, or environment values that affect the result
- A note asking for explanation before any fix
How to refine the explanation
If the first answer is too broad, point at the specific assertion. Say something like the total is 50 instead of 45, walk me through how threshold is compared to the item count. A narrow follow up almost always produces a sharper trace than starting over.
Common mistakes
- Pasting only the test without the code it calls
- Sending only the error message without the test or the source
- Asking ChatGPT to fix the code before it explains the failure
- Leaving out fixtures or mocks that change the inputs
- Forgetting to include the exact assertion line that failed
How to check the explanation
Read the story ChatGPT tells and follow it with your own eyes in the code. If the story says the threshold check uses 10 but you passed 5, open the source and confirm the default value. The explanation should match every number and branch in the actual code. If any step feels off, ask for that single step again.
Takeaway
A failing test is a conversation starter, not a dead end. Give ChatGPT the test, the output, and the source, and it will walk you through the mismatch so you can decide what to change.

explore