AI can write a unit test for almost any function in seconds. The catch is that, without guidance, it tends to write only the happy-path tests — the ones that prove nothing. A good prompt forces the AI to think about edge cases, error paths, and the behaviours that actually matter.
Unit tests are one of the highest-leverage things AI can do for you as a developer. The patterns are repetitive, the boilerplate is verbose, and the cognitive cost of writing them is exactly what makes most teams skip them. AI removes that friction — but only if you steer it towards meaningful coverage instead of test-padding.
This tutorial gives you a prompt pattern based on the classic Arrange-Act-Assert structure, with explicit prompts for the edge cases AI tends to forget.
A unit test has three jobs: set up the world, call the thing under test, and verify what happened. That is the Arrange-Act-Assert pattern, and every solid test framework speaks it natively. When you prompt AI for tests, mirroring this structure in your instructions produces tests that read clearly and exercise the right boundaries.
Think of it like restaurant inspection: you set the table the way a customer would, ask the chef to cook, and check whether the dish is what was ordered. A bad inspector only orders the bestseller; a good one orders the spicy dish, the gluten-free request, and the one the chef is most likely to overcook.
Without a structured prompt, AI generates one test that proves the function does something — usually the happy path — and stops there. That is worse than no test, because it lulls you into a false sense of safety.
Weak prompt
write a unit test for this function
function divide(a, b) {
return a / b;
}
You will get one assertion: expect(divide(10, 2)).toBe(5). No test for division by zero. No test for negative numbers. No test for non-numeric input. The function "has a test" but the dangerous behaviour is uncovered.
Strong prompt
Test framework: Jest 29 with TypeScript
File under test: src/utils/divide.ts
```ts
export function divide(a: number, b: number): number {
if (b === 0) throw new Error("Cannot divide by zero");
return a / b;
}
```
Write a complete test suite using Arrange-Act-Assert.
Cover:
1. Happy path: positive integers, two cases including a non-evenly-dividing pair
2. Edge cases: zero numerator, negative numbers, floating-point precision
3. Error path: dividing by zero must throw with the exact error message
4. Type safety: ensure the return type is number
Use describe() per behaviour and it() per case. Each test should be one
clear arrange, act, assert. No setup/teardown unless required.
Output only the test file. No commentary.
You now get a structured describe block with named cases, error-path coverage, edge-case coverage, and no flaky assertions on the side. The output reads like a specification, not just a smoke test.
Tip: After the AI writes the tests, ask a follow-up:
What behaviours of this function are NOT covered by these tests?The answer is often more useful than the tests themselves.
Pick a pure function from a recent project — a formatter, a validator, a calculator. Generate tests using the strong-prompt format. Run them. Were any tests genuinely useful for catching potential regressions? Were any redundant?
Take the same function and intentionally introduce a subtle bug (off-by-one, wrong comparison operator). Re-run the AI-generated tests. If they don't catch the bug, add the missing case to your prompt and regenerate. Repeat until your prompt produces a test suite that catches subtle bugs.
Ask the AI to write tests in two different styles: classical Arrange-Act-Assert and table-driven (a list of input/expected pairs iterated through). Compare which style is easier to extend when a new case appears six months later.
Sign in to join the discussion and post comments.
Sign inPrompt Engineering for Image Generation
Turn words into stunning visuals. Master AI image generation tools like Midjourney, DALL·E 3, and Stable Diffusion with 18 focused tutorials — from first prompt to full brand identity.
Advanced Prompt Engineering Techniques
Master the powerful techniques AI experts use every day. Chain-of-thought, RAG, agents, function calling, prompt evaluation, and much more — 20 deep-dive tutorials.
Prompt Engineering for Content & Copywriting
Write blogs, ads, emails, and social media content ten times faster with AI. 13 practical tutorials on prompt engineering for content creators and copywriters.
Prompt Engineering for Data Science & Analytics
Supercharge your data workflows with AI. 15 practical tutorials on using prompt engineering for data cleaning, EDA, machine learning, SQL, visualisation, and more.
Foundations of Prompt Engineering
The must-know basics of prompt engineering. Learn what prompts are, how AI models read them, and how to write clear instructions that get great results.
Prompt Engineering for Business & Productivity
Use AI to work smarter — automate tasks, make better decisions, and communicate professionally. 12 practical business prompt tutorials for professionals.