Generating Tests by Prompt
Get AI to write meaningful tests.
Generating Tests by Prompt is a free Vibe Coding lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vibe Coding learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Tests Are Your Safety Net
When AI writes most of your code, tests become the contract that keeps it honest. Each assertion is a promise the assistant cannot silently break.
The goal is not 100 percent coverage for its own sake. It is to pin the behaviors you care about so that future generations cannot regress them.
This lesson is about prompting your way to a test suite worth trusting.
Describe Behavior, Not Implementation
Weak prompts ask the model to "write tests for this function." The result mirrors the implementation and passes trivially, even when the code is wrong.
Strong prompts describe the expected behavior in your own words. The test then encodes intent, not the model's reading of the existing code.
Specify inputs, outputs, and invariants the way a product spec would.
Write tests for the checkout function based on these rules, not on the current implementation: total must equal sum of line items minus discount; discount can never exceed subtotal; an empty cart must throw. Cover the boundary at exactly zero and at the discount cap.Start With the Failing Case
Tests that only confirm the happy path give false comfort. Ask for the cases most likely to break first: nulls, empties, limits, and malformed input.
A suite that exercises failure modes is the one that catches real regressions. The boring success case rarely surprises anyone.
Front-load the edges and you front-load the bugs.
Generate tests that focus on failure and boundary conditions for this parser: empty string, whitespace only, max length plus one, invalid unicode, and deeply nested input. For each, assert the exact error type and message the code should produce.Beware Tests That Pin Bugs
If you ask a model to write tests against existing code, it may assert the current buggy behavior as if it were correct. The suite goes green while the bug stays.
This is the single biggest trap in AI test generation. The test documents the defect instead of catching it.
Always review whether each assertion reflects what you want, not merely what the code currently does.
For each test you generated, tell me explicitly whether the expected value is derived from my stated requirements or from observing the current code's output. Flag any assertion that simply mirrors existing behavior so I can verify it is actually correct.The Arrange-Act-Assert Spine
Readable tests follow a clear shape: set up state, perform one action, assert one outcome. Models drift into multi-assertion mega-tests that are hard to debug when they fail.
Ask for focused tests with descriptive names that read like specifications.
A failing test should tell you exactly which behavior broke, in its name alone.
Refactor these tests so each one follows arrange-act-assert, tests a single behavior, and has a descriptive name that reads like a sentence describing the expected outcome. Split any test that contains unrelated assertions.Mocking the Right Boundaries
AI often mocks too much, stubbing out the very logic under test until the test asserts nothing real. Or it mocks too little and hits a live database.
Mock at the system boundary: network, clock, filesystem, payment gateway. Keep your own business logic real.
State the boundary explicitly so the model does not guess.
Write these tests so that only external boundaries are mocked: the HTTP client, the system clock, and the payment provider. Do NOT mock our own pricing or validation logic; that logic must run for real so the test exercises it.Property-Based Thinking
Example tests check specific inputs; property tests check invariants across many generated inputs. "Reversing a list twice returns the original" holds for every list.
Asking the model for property-based tests surfaces bugs that hand-picked examples miss, because the framework hunts for counterexamples.
Use them where invariants are clear: encoders, sorters, serializers, math.
Write property-based tests for the serializer using a generative testing library. Assert the round-trip invariant that decode(encode(x)) equals x for any valid input, and let the framework search for the smallest failing case.Coverage Without Theater
Coverage percentage is a map, not the territory. You can hit every line while asserting nothing meaningful. Models love padding suites to chase a number.
Use coverage to find untested branches, then judge each by hand: is this path worth pinning?
Aim for tests that would fail if the behavior were wrong, not tests that merely execute the code.
Run coverage and list the uncovered branches in this module. For each one, tell me whether it represents a meaningful behavior worth testing or trivial code I can ignore, and propose a focused test only for the meaningful ones.Mutation: Testing Your Tests
A green suite that survives any code change is worthless. Mutation testing introduces small bugs and checks whether your tests notice.
If a mutant survives, your tests do not actually constrain that behavior. The model can propose mutations and you verify your suite kills them.
This is how you measure test quality, not just quantity.
Propose five small mutations to this function, such as flipping a comparison or removing a guard, that would represent real bugs. For each mutation, tell me whether the current test suite would catch it, and write a new test for any mutation that survives.Make Tests Deterministic
Flaky tests erode trust until people ignore failures entirely. The usual culprits are real time, random seeds, network calls, and test ordering.
Demand deterministic tests: inject the clock, fix the seed, isolate state between runs. A suite you cannot trust is worse than no suite.
Reliability is a feature of the tests themselves.
Make this test suite fully deterministic: inject a fixed clock instead of reading current time, seed all randomness, remove any reliance on test execution order, and ensure each test resets shared state in setup and teardown.Tests As Living Spec
A good suite doubles as documentation. New contributors, human or AI, learn the system's rules by reading the assertions.
Keep test names and structure clear enough that the suite explains intended behavior on its own. The model reads them too, and writes better code as a result.
Your tests become the durable memory the model lacks.
Quick Check
Test your grasp of prompt-driven test generation.
Recap
Prompt tests from behavior and requirements, not from the implementation, so they catch bugs instead of pinning them. Front-load failure and boundary cases, mock only at boundaries, and use property and mutation testing to measure real strength.
Keep the suite deterministic and readable so it serves as a living spec. Next, you will hunt for security gaps the model left behind.
Frequently asked questions
Is the “Generating Tests by Prompt” lesson free?
Yes — the full text of “Generating Tests by Prompt” is free to read here on the web, and the Vibe Coding course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vibe Coding course, upgrade to CoddyKit PRO.
What will I learn in “Generating Tests by Prompt”?
Get AI to write meaningful tests. You practise Vibe Coding with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vibe Coding?
No prior experience is required. Vibe Coding on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Generating Tests by Prompt” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vibe Coding lesson?
Yes. Every Vibe Coding lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why AI Code Needs Review
- Generating Tests by Prompt
- Finding Security Gaps
- Hardening for Production