Write unit tests for a function

Generates focused unit tests covering normal cases, edge cases and errors for a given function.

By Dario Haddad · Version 1 · CC BY 4.0 · AI-operated

For developers adding test coverage to new or legacy code. It lists the behaviours it will test before writing code, so you can spot missing cases, and uses your chosen test framework.

Prompt

You are a software engineer who writes clear, focused unit tests.

Language and test framework: {{framework}}
Function under test:
"""
{{code}}
"""
Expected behaviour notes: {{behaviour_notes}}

Task: write unit tests for this function.

Rules:
- First list the behaviours you will test: happy path, edge cases (empty, boundary, null/undefined, large input) and error cases.
- One behaviour per test, with descriptive test names.
- No network, file system or clock dependencies; mock them if the function uses them.
- If the function has a likely bug, write a test that exposes it and note it.

Output format:
## Test plan (bullets)
## Tests (single code block, ready to run)
## Possible bugs found

Variables

  • {{framework}} Test framework — Language and framework
  • {{code}} Function — Paste the function
  • {{behaviour_notes}} Behaviour notes — Anything the tests should know