Topic 5 of 5 · 5 min read
Turning generated tests into real tests
Goal: Rewrite a weak AI-generated test so it checks behaviour, edges and errors, and show it fails against a planted bug.
Use the AI to draft tests, then turn each draft into a real one by specifying behaviour first and proving the test fails against a planted bug. Generated tests are a good start: they set up the files, imports and fixtures quickly. They are a poor finish.
Check yourself · 1 of 3
You want an assistant to write tests that can catch a wrong implementation. What should you give it?
Why it matters
If you ask "write tests for this function" and paste the code, the model reads the implementation and describes it. Ask from the specification instead, and the tests can disagree with the code, which is what a test is for.
A workflow that works
- Write the behaviour in plain sentences before prompting: "returns the 10 most recent orders, newest first; empty list for a new user; throws
UnknownUserfor a missing id." - Prompt from the spec. Give the model the behaviour list and the function signature, not the body. Ask for one test per sentence, plus boundaries, invalid input and failure paths.
- Read each test against the list. Is every sentence covered? Are values hard-coded and justified?
- Plant a bug. Break the code in one small way, run the tests, and confirm one fails. Do it for a boundary, a deleted line and a wrong return.
- Restore the code and keep the test. Optionally run Stryker, PIT or mutmut on the module.
Before and after
-test("recentOrders works", async () => {
- const result = await recentOrders("u1");
- expect(result).toBeDefined();
-});
+test("returns the 10 newest orders, newest first", async () => {
+ await seedOrders("u1", 12);
+ const result = await recentOrders("u1");
+ expect(result).toHaveLength(10);
+ expect(result[0].createdAt >= result[9].createdAt).toBe(true);
+});
+
+test("returns an empty list for a user with no orders", async () => {
+ expect(await recentOrders("u-new")).toEqual([]);
+});
+
+test("throws UnknownUser for a missing user", async () => {
+ await expect(recentOrders("nope")).rejects.toThrow("UnknownUser");
+});
Planting take: 11 now fails the first test. The old test would have passed.
What goes wrong
- Prompting with the implementation only. The model mirrors it, bugs included.
- Accepting a long list of near-identical tests. Ten tests for the happy path and none for errors is still thin.
- Skipping the planted bug. Without it you have not seen the test fail.
- Over-specifying internals. Tests that check private calls break on every refactor. Check behaviour you could explain to a user.
Examples
A prompt that asks for the right thing
Write vitest tests for recentOrders(userId) from this behaviour list only:
- returns at most 10 orders, newest first
- empty array for a user with no orders
- throws UnknownUser for an unknown id
Use hard-coded expected values. Do not read the implementation.
The model cannot copy the code, so the tests reflect the spec.
The planted-bug check
After the tests are written, change orderBy: { createdAt: "desc" } to "asc" and run them. If none fails, the ordering test is missing or too loose. Fix the test, restore the line, and commit both.
Key points
- Write the expected behaviour in plain sentences before asking an assistant for tests.
- Prompt from the specification and signature rather than the implementation, so tests can disagree with the code.
- Ask explicitly for boundaries, invalid input and failure paths, not only the happy path.
- Plant a small bug and see a test fail before you trust it.
- Check behaviour a user could notice rather than private calls that break on every refactor.
Common mistakes
- Pasting the implementation and asking for tests, so the model mirrors it.
- Keeping every generated test without checking which behaviour each one covers.
- Never breaking the code on purpose, so you never see a test fail.
- Asserting private calls or internal state that change with every refactor.
Check yourself · 2 of 3
After a generated test passes, what is a quick way to see whether it protects anything?
Check yourself · 3 of 3
Which prompt is most likely to produce tests that cover failure paths?
Practice items, not counted towards your mastery.
Start here
- The Practical Test Pyramid (martinfowler.com) (opens in a new tab)ArticleFree
Where unit, integration and end-to-end tests belong, and why test behaviour rather than implementation.
- Mocks Aren't Stubs (martinfowler.com) (opens in a new tab)ArticleFree
Defines mocks, stubs, fakes and spies, and the difference between state and behaviour verification.
- Stryker mutation testing (opens in a new tab)ToolFree
Mutation testing for JavaScript, TypeScript, C# and Scala: the tool that checks whether tests notice changed code.
More for this topic
- mutmut: Python mutation testing (opens in a new tab)DocsFree
How to run mutation testing on a Python project and read the surviving mutants.