Generate unit tests with AI that test the spec, not the bug
A model writes assertions matching what the code does. If the code is wrong, so is the test.
In short
When you generate unit tests with AI, tell it the intended behavior, or it will encode the current behavior including any bug.
- Always run generated tests. An unexecuted test proves nothing and often does not compile.
- Point it at the risky logic: money, permissions, validation, edge cases.
- Coverage is not the goal. A test that cannot fail adds a number and no safety.
The trap: tests that certify the bug
There is a specific failure when you generate unit tests with AI, and it is not laziness. A model reads your function, infers what it is meant to do from what it does, and writes assertions that match. If the function has an off-by-one or an inverted condition, the generated test asserts the broken behavior and goes green forever, and you now have coverage certifying a defect. The fix is to supply the specification separately: say what the function should do, in words, and ask for tests against that. Then run them, because an unexecuted generated test frequently fails to compile or asserts something impossible. Aim them at logic where a mistake costs something, like money, permissions, and validation boundaries, instead of chasing a coverage percentage. Confirm the cause of any failure using debugging with AI, and gate the results with AI code review. It is part of AI for engineering.
How to generate unit tests with AI
State the intent, generate, run, then check they can fail.
- 1
State the intended behavior in words
'This should reject negative amounts and round half up.' Without the spec it tests whatever the code currently does.
- 2
Name the framework and conventions
Your test runner, assertion style, and how you handle fixtures. Otherwise you get a style nobody else uses.
- 3
Point it at the risky paths
Money, permissions, validation, boundaries, and error handling. That is where a missing test actually costs something.
- 4
Ask for edge cases explicitly
'Include empty, null, zero, negative, maximum, and malformed input.' Happy-path tests catch nothing.
- 5
Run every generated test
Unexecuted tests routinely fail to compile or assert the impossible. Running them is the quality gate.
- 6
Break the code and check they fail
Change a condition deliberately. A test that still passes was never testing anything.
Watch-outs
- A model infers intent from the code, so a bug becomes the expected result. Supply the spec yourself.
- Coverage percentage is easy to raise and easy to fake. Test the logic that matters instead.
- Generated mocks can assert on implementation detail, which makes every refactor fail loudly.
Common questions
How do I generate unit tests with AI?
State the intended behavior in words first, name your framework and conventions, and point the model at the risky logic rather than the whole file. Then run every generated test, and deliberately break the code to confirm the tests actually fail when they should.
Why do AI-generated tests miss bugs?
Because the model infers the specification from the implementation. It reads what the function does and asserts that, so an existing defect is written into the test as correct behavior and the suite goes green over a real bug.
Do I need to run the generated tests?
Always. Generated tests frequently fail to compile, import something that does not exist, or assert a condition that cannot hold. Execution is the check that separates a usable test from plausible-looking text.
Should I aim for high coverage with AI?
No. Coverage is easy to inflate with tests that cannot fail, which buys a number and no safety. Target the paths where an error is expensive, such as money handling, permissions, and validation boundaries.
See how you actually work with AI
The AI Skills Quiz scores your real habits, including whether you check AI output or trust it. Free, and it takes a couple of minutes.
Professionals from these companies build their AI skills with Candova
The future belongs to AI‑native companies.
Make sure yours is one.
We make your people AI-fluent so your company becomes AI-native, with Cando, a personal AI trainer for every employee. Start with Candova AI today.
Free to start · Productivity gain guarantee · Built for your real job