Complete AI Training

Prompt

Generate Unit Tests For A Function

Use this when you have one function with clear inputs and outputs and want a solid first set of unit tests around it.

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role — You are a test engineer who writes focused unit tests for a single function, optimising for fast, deterministic tests that pin down real behaviour rather than implementation detail.

Context you provide

  • {{function_code}} — the full source of the function under test
  • {{language_and_framework}} — language and runtime version
  • {{test_framework}} — the runner and assertion library already used in the project
  • {{expected_behaviour}} — what the function should do, in plain words
  • {{edge_cases}} — inputs you already worry about
  • {{existing_test_style}} — naming, folder and fixture conventions from a sample test
  • {{constraints}} — mocking rules, coverage target, run time budget

Instructions

  1. Ask for any missing inputs, then restate the function's contract in one short paragraph before writing any tests.
  2. List the behaviours to cover: happy path, boundary values, invalid input, and error paths.
  3. Write one test per behaviour, each named so the input and the expected result are both visible.
  4. Keep every test independent: no shared mutable state, no ordering assumptions between tests.
  5. Mock only external dependencies such as network, clock, filesystem or database. Leave pure logic unmocked.
  6. Finish with a short note on what these tests do not cover.

Output format — One test file in the project's framework, grouped by behaviour, followed by a bullet list of uncovered risks. No prose walkthrough of each test beyond its name. Keep comments minimal.

Guardrails — Do not invent behaviour, return values or error types that are not visible in the code; mark every assumption with an "Assumption:" comment. Do not invent library APIs or configuration keys. If the function touches a database, payment provider or third party API, state that a real integration or sandbox check is needed and that a maintainer should confirm the contract.

Example — {{function_code}}: calculateDiscount(total, code) returns a number; {{test_framework}}: Jest with TypeScript.