Writing Tests With an AI Coding Assistant: A Practical Workflow
AI assistants can write tests quickly, but fast tests are not the same as useful ones. This workflow gets you tests that actually catch bugs.
Ask an AI coding assistant to "write tests for this file" and it will happily produce a wall of green checkmarks. The problem: many of those tests only prove the code does what the code does. They pass today and they will keep passing when you introduce a bug.
Here is a workflow that produces tests worth keeping.
Start with behaviour, not code
Before the assistant sees the implementation, describe what the code is supposed to do. For example:
"This function calculates shipping cost. Orders over $100 ship free. International orders add a flat $15. Weight over 20 kg adds $2 per extra kilo."
Then ask it to list test cases from that description. Tests written from intended behaviour catch bugs. Tests written by reading the implementation tend to copy its mistakes.
Ask for the edge cases explicitly
Assistants default to the happy path. Push them further:
- Boundary values: exactly $100, exactly 20 kg
- Empty, zero and negative inputs
- Invalid types or missing fields
- Combinations: an international order that also qualifies for free shipping
A prompt like "List the edge cases a careful tester would worry about" reliably surfaces cases you would have skipped.
The value of a test is the bug it catches, not the line it covers.
Make one test fail on purpose
Here is a quick sanity check for any generated test: break the code deliberately (flip a > to >=, change a constant) and run the tests. If nothing fails, the tests aren't testing much. Ask the assistant to strengthen them, and tell it which change went unnoticed.
Keep tests readable
Ask for tests that a teammate could understand in ten seconds:
- One behaviour per test
- Descriptive names such as
free_shipping_applies_at_exactly_100 - Clear setup, action and check, in that order
- No clever helpers unless they remove real repetition
Readable tests double as documentation. Six months from now, they explain what the code was meant to do.
Review the mocks
Generated tests often mock too much. If every dependency is mocked, the test only checks that functions call each other. Ask: "Which of these mocks are necessary? Could any test use the real implementation?" Fewer, more honest mocks mean tests that fail when integration breaks.
Run the tests yourself
Never accept tests you haven't run. Check that:
- They pass on the current code (if the code is correct).
- They fail when you break the behaviour they claim to cover.
- They run quickly enough that you will actually keep running them.
A simple loop
Put together, the workflow is: describe behaviour, generate cases, add edge cases, generate tests, break the code to check them, then clean up. It takes a little longer than "write tests for this file", but the result is a test suite that protects you instead of flattering you.
The takeaway
AI makes writing tests cheap, which is wonderful, but cheap tests can give false confidence. Steer the assistant toward intended behaviour and edge cases, prove each test can fail, and you get the speed without the illusion.
Enjoyed this piece?
Get the next one in your inbox, every other Friday.