# Test-first with a coding agent: failing test, then code

Canonical: [Test-first with a coding agent: failing test, then code](https://contextingo.com/blog/test-first-with-coding-agents)

Locale: en

Purpose: Ask the agent to reproduce a bug as a failing test before it changes code. The workflow, the traps, and how to check that the test is a real one.

Content updated: 2026-09-22

Source revision: content.ts 2026-09-22

Maintenance owner: the Contextingo site owner

[Topic map](https://contextingo.com/llm/topics/blog.md) · [Complete scoped map](https://contextingo.com/sitemap.md)

The most reliable way to point an agent at a bug is a failing test. It turns “it seems broken” into a command that exits non-zero, and it gives the agent something to run after every change.

## The workflow

1. Describe the bug and ask for a test that reproduces it. Do not allow changes to the code yet.
2. Run the test and confirm that it fails for the reason you expect.
3. Ask for the smallest change that makes it pass.
4. Run the whole suite, not only the new test.
5. Review the test and the fix as two separate things.

## Traps

- **A test that fails for the wrong reason,** such as a typo or a missing import.
- **A test written to match the new code,** which proves nothing about the bug.
- **A weakened existing test** changed so that it passes.
- **Tests that mock the very thing that was broken.**

## How to check a test

Revert the fix and run the test again. If it still passes, the test does not cover the bug. A reviewer can do the same in a minute.

## Record the outcome

Save the cause of the bug as a finding with its source, and any approach that failed as a failed attempt. The next person who sees a similar symptom will start with the answer. See [failed attempts as documentation](/blog/record-failed-attempts-documentation).

## Questions

### Does this work for every kind of bug?

It fits bugs you can reproduce in code. For timing problems and flaky tests, first make the failure reproducible.

### Should the agent run the tests itself?

Yes, and CI should run them again. Contextingo fetches the CI result for the commit, so a claim that the tests pass is checked.
