How to Write Unit Tests with AI (and Keep Them Useful)
AI can write a hundred tests in seconds. The skill is keeping the ten that would actually fail when the code breaks.
AI test generation has a quality problem that is easy to miss: the tests pass. They pass because they were written against the implementation you already have, not against the behaviour you expect. A suite that cannot fail is documentation, not a safety net.
Here is a workflow that uses AI for the tedious part while keeping the tests meaningful.
Start from the contract, not the code
Give the model the function signature, the expected behaviour and the edge cases — then the implementation. If you only paste the code, it will describe what the code does, including its bugs.
Function: calculateDiscount(price, code)
Contract:
- price must be > 0, otherwise throw
- SAVE10 returns 90% of price
- SAVE20 returns 80% of price
- unknown codes return price unchanged
- round to 2 decimals
Framework: Vitest
Ask for the cases explicitly
A good prompt enumerates the categories you want:
- Happy path
- Boundary values (0, 1, maximum)
- Invalid input
- Floating point and rounding
- Empty and null values
Without this list you get three happy-path tests and a false sense of coverage.
Check that the tests can fail
This is the step most people skip. Break the implementation on purpose — change a boundary, flip a condition — and run the suite. If everything still passes, the tests are decorative.
| Mutation | Expected result |
|---|---|
Change < to <= | At least one test fails |
| Remove rounding | Rounding test fails |
| Return price for SAVE20 | Discount test fails |
If a mutation survives, you found a gap. Ask the model to add a test for exactly that case.
Prefer behaviour over implementation
Tests that mock every internal call break the moment you refactor. Prefer asserting on outputs and observable side effects. Mock only what crosses a process boundary: network, database, time, randomness.
// Brittle: asserts the internal call order
expect(helper.parse).toHaveBeenCalledBefore(helper.validate)
// Better: asserts the observable result
expect(calculateDiscount(100, 'SAVE10')).toBe(90)
A review checklist for AI-written tests
- Does each test have a name that describes the behaviour?
- Does it assert something specific, or just "not undefined"?
- Are edge cases actually exercised, or only mentioned?
- Is the test independent of execution order?
- Would a real bug make it fail?
Coverage measures which lines ran. Mutation testing measures whether anyone would notice if they were wrong.
Where AI genuinely helps
- Generating the first draft for pure functions.
- Enumerating edge cases you forgot.
- Converting tests between frameworks.
- Writing parameterised test tables.
Where it hurts
- Tests for stateful UI flows with timing.
- Anything requiring real infrastructure.
- Snapshots that encode accidental output.
Use the AI Unit Test Generator for the draft, then apply the checklist above. For the data your tests assert on, the JSON Formatter keeps fixtures readable.
Practical write-ups on AI tooling and developer utilities from the team behind DevToolAI. Every guide is tested against the tools we ship.