AI prompts
AI Prompts for Writing Unit and Integration Tests
An AI assistant writes useful tests when you give it the function, the language and test framework, and the behaviour you expect, and when you ask it to list edge cases before it writes any code. The tests are only trustworthy after you run them and break the code on purpose to see them fail.
By CodingMarble · Published · 4 min read

Why test prompts need more structure than most
Ask an assistant to 'write tests for this function' and you will usually get a handful of happy-path checks that restate what the code already does. Those tests pass on day one and protect nothing. A test earns its place by failing when behaviour breaks, so the prompt has to describe behaviour, not just paste code.
Every template below follows the same shape: say what language and test framework you use, paste the code under test, state the rules in plain words, and tell the assistant what output format you want. Replace the square-bracket placeholders before sending. Keep the code you paste free of secrets, customer data and anything your employer or client has not allowed you to share with an external tool.
Templates for unit tests
Unit tests exercise one function or class with its collaborators removed. They are fast, they point at the exact line that broke, and they are the easiest kind for an assistant to draft because the input and output fit in one message. The three templates here move from a plain request, to a discovery step, to a compact table format. Use them in that order when a function carries real business rules, such as discounts, tax, dates or permissions, because a mistake there costs more than a mistake in a string helper.
Notice that each prompt asks the assistant to report disagreement instead of silently choosing a side. That single instruction turns a test generator into a reviewer: when your stated rule and the code differ, you hear about it before the suite locks the wrong answer in place.
1. Tests for a given function
You are helping me write unit tests.
Language: [LANGUAGE]. Test framework: [FRAMEWORK, e.g. Jest, Vitest, pytest, JUnit].
Function under test:
[PASTE FUNCTION]
Intended behaviour, in my words:
[RULES, e.g. returns the total after discount; never negative; rejects empty cart]
Write tests that check the intended behaviour above, not the way the code is written.
Use clear test names that read as sentences. Do not modify the function.
If the code and my stated rules disagree, tell me instead of writing a test that matches the code.2. Edge-case discovery before any code
Here is a function and its purpose:
[PASTE FUNCTION]
Purpose: [ONE SENTENCE]
Before writing any test code, list the edge cases and boundary values worth testing.
Group them as: empty or missing input, boundaries, invalid types, large input, time or locale issues.
For each, give the input and the output you expect. Mark any where the correct output is unclear
so I can decide. Do not write test code yet.3. Table-driven tests
Convert the cases below into a table-driven test in [LANGUAGE] using [FRAMEWORK].
One test body, one array or table of rows with fields: name, input, expected.
Cases:
[LIST OF INPUT -> EXPECTED PAIRS]
Add rows for boundaries I may have missed, and label each added row with a comment
saying why it exists. Keep every row independent of the others.Templates for mocks, bug reports and properties
The next four templates cover work that goes beyond a single pure function. Mocking limits a test to your own logic by replacing a slow or unpredictable dependency, such as a payment client or a clock. A regression test captures a reported bug so it cannot return unnoticed. Property-style tests check rules that must hold for every input, which finds cases you would never think to write by hand. The integration prompt checks that real parts work together, which mocks cannot show.
Write tests for [FUNCTION NAME] in [LANGUAGE] with [FRAMEWORK].
It depends on [DEPENDENCY, e.g. a payment client, a database module, fetch].
Replace only that dependency with a test double. Do not mock the function under test
or anything in the same module. Cover: success, the dependency throwing,
the dependency returning an empty or malformed result, and a timeout.
Assert on what the function returns or throws, and on the arguments it passed
to the dependency. Show the mock setup as a short helper at the top.A user reported this bug:
[PASTE BUG REPORT: steps, input, actual result, expected result]
Relevant code:
[PASTE CODE]
Write one regression test that reproduces the bug and fails on the current code.
Use the smallest input that triggers it. Name the test after the symptom.
Then, separately, explain in two sentences what you think the cause is.
Do not change the code in this reply.Function: [PASTE FUNCTION]
List properties that should hold for every valid input, for example: output length,
ordering, idempotence (running twice equals running once), round trips (decode(encode(x)) equals x),
or results never leaving a range.
Then write tests for those properties using [LIBRARY, e.g. fast-check, Hypothesis]
or, if I do not use one, a loop over generated inputs with a fixed seed so failures can be repeated.Write an integration test in [LANGUAGE] with [FRAMEWORK] for this flow:
[DESCRIBE FLOW, e.g. POST /orders creates a row and returns 201]
Use a real [DATABASE OR ROUTE HANDLER] against a test database or in-memory instance,
and seed only the rows the test needs. Reset state between tests.
Assert on the response and on what was stored. Do not mock the layer I am trying to integrate.How to use the output without trusting it blindly
Treat the reply as a first draft from a quick colleague who has never run your project. Paste the tests into a file, run the suite, and read every failure. A failing test means one of three things: the test is wrong, the code is wrong, or your stated rule was unclear. Deciding which is the real work, and the assistant cannot do it for you because it does not know what your business actually wants.
Prompt 2 is worth using before Prompt 1 on anything with real rules, such as money, dates or permissions. Reading a list of cases takes a minute, and spotting a case where the expected answer is genuinely ambiguous is often the most valuable result. Settle those questions with a person who owns the rule, then feed the agreed answers back into Prompt 1.
Practical tip: paste your existing test file for the same project and say 'match this style, naming and helper usage'. Generated tests that follow your conventions are far easier to review and merge than tests in a different style.
What to verify before you commit the tests
Running the tests and seeing green is the weakest check. The stronger check is mutation by hand: change the code under test in a way that should break a rule, and confirm a test fails. Flip a comparison from greater-than to greater-or-equal, remove a null check, return a constant. If the suite stays green after each change, the tests are not guarding that behaviour.
- Run the new tests on their own and in the full suite, in a random order if your runner supports it, to catch hidden dependence between tests.
- Introduce two or three deliberate bugs, one at a time, and confirm at least one test fails for each. Revert after every check.
- Read each assertion and ask what it would miss. An assertion that only checks a value is not null or a list is not empty is usually too loose.
- Check that mocks return realistic shapes. A fake that returns a tidier object than the real service hides integration bugs.
- Search for hard-coded dates, random values and network calls, which make tests flaky.
- Confirm the test fails before the fix when it is a regression test. A regression test that never failed proves nothing.
Failure modes to watch for
The most common problem is the mirror test: the assistant reads the implementation and writes assertions that repeat its logic. If the function has a bug, the test encodes the bug and passes. You prevent this by stating the intended behaviour yourself and by asking the assistant to flag disagreements, as Prompt 1 does.
Other patterns to catch: tests that assert on private details so any refactor breaks them; over-mocking, where the test mostly checks that mocks were called; invented library functions or wrong import paths that fail on the first run; snapshots accepted without being read; and a long pile of near-identical cases that adds noise but no coverage. A smaller suite that you understand beats a large one nobody reads.
AI-written tests also tend to skip the unglamorous cases: time zones, concurrent calls, partial failures, very large input. Ask for those explicitly, and add the cases you know from real support tickets. Your own project history is information the assistant cannot see.
Keep reading
- AI promptsAI Prompts for Debugging Code: 7 Templates That WorkA debugging prompt works when it gives the assistant the error, the code involved, what you expected and what you tried, and then asks for a specific kind of help. Use the templates below as starting points, treat every answer as a hypothesis to test, and never paste secrets into a prompt.
- AI promptsAI Prompts for Code Review: 7 Templates With ChecksAI-assisted code review works best as a second pair of eyes that checks one concern at a time and cites where it found each issue. It supplements human review and never replaces it, so verify every finding against the code before acting on it.
- Interview questions and cheat sheetsJavaScript Interview Questions and AnswersThis page gives you eighteen JavaScript interview questions, grouped from fundamentals to trick questions, each with a precise answer you can say aloud in under a minute. Code examples are short enough to rewrite on a whiteboard.
- AI and developer toolsAI Coding Assistants for Beginners: Use Them to LearnA beginner should treat an AI coding assistant as a patient tutor and a fast first-draft writer, never as an authority. Ask narrow questions, read and run everything it gives you, and check it with tests, because the tool can sound certain while being wrong.
Need this built properly for your business?
CodingMarble is a remote studio in India that builds websites, apps and games on fixed packages with the scope written down before payment. Try the free tools we run, or start a project when you are ready.