On this page
The core idea
AI assistants work best on bounded tasks with a clear signal of success. A failing test is exactly that signal: it describes the behaviour you want in executable form, and the assistant can iterate until it passes. Working in small loops, one behaviour at a time, also keeps each diff reviewable and makes it easy to throw away a bad attempt. As a session grows, quality usually falls, so starting fresh with a short summary often beats pushing on.
A practical approach
Step 1
Pick one behaviour and write, or ask for, a failing test that describes it. Confirm it fails for the right reason before any implementation.
Step 2
Ask the assistant to make that test pass with the smallest reasonable change, without modifying the test.
Step 3
Run the full test suite, review the diff and commit when it is green.
Step 4
Refactor in a separate step, again keeping tests green, so behaviour changes and cleanups do not mix.
Step 5
When the conversation gets long or confused, stop, write a short summary of decisions and remaining work and start a new session with it.
Worked example
A developer asks an assistant to build a discount-code feature in one go. After an hour the code mostly works, but nobody can explain why a percentage discount sometimes applies twice. Restarting with tests first, one for a fixed discount, one for percentage, one for expiry and one for single use per learner, the feature is built in four short loops. The double-discount case becomes a fifth test before it can ever reach production.
A mistake to avoid
Do not let the assistant change a failing test to make it pass unless you have decided the test itself was wrong. Editing tests to match the code reverses the purpose of the loop.
Try it yourself
Choose a small function you need to write, such as validating a phone number. Write three failing tests first, then use an assistant to implement it one test at a time, committing after each passes.
Your implementation checklist
- Each loop targets one behaviour
- A failing test exists before implementation
- Tests are not edited to fit the code without a deliberate decision
- Each green loop ends with review and a commit
- Long sessions are restarted with a written summary
Share or cite this guide
Innovate with Ajay Editorial (2026-10-07). Keep AI Coding Sessions Small with a Test-First Loop. https://innovatewithajay.com/blogs/keep-ai-coding-sessions-small-with-tests
