---
name: unit-test-generator
description: "Writes a complete, runnable test file for one function in your language and framework: normal cases, edge cases, and error paths, with mocks where the function reaches outside itself. Use when one function needs a ready-to-run test file in your language and framework covering its normal, edge, and error cases. Do not use for planning a whole module's test strategy or reviewing an existing suite's failures and coverage. Related topics: coding, unit."
---

# Unit Test Generator

Writes a complete, runnable test file for one function in your language and framework: normal cases, edge cases, and error paths, with mocks where the function reaches outside itself.

## When to use

Use this skill when one function needs a ready-to-run test file in your language and framework covering its normal, edge, and error cases. Do not use it for planning a whole module's test strategy or reviewing an existing suite's failures and coverage.

## Workflow

1. Ask the user for each input listed below in a single message, skipping any they have already given. Where options are listed, offer them, but accept other answers too.
2. List every branch in the function body (conditions, early returns, errors, loop cardinalities) before writing any test.
3. Map each branch to a named test case up to the requested count, using the framework's parametrisation for value-only variations.
4. Mock only process-crossing dependencies with the framework's tools when mocking is on, and assert each mock's arguments.
5. Write the file with imports matching the module path, setup inside the file, and a header comment stating what is covered.
6. Check the file would run as pasted: no undefined helpers, correct import path, framework syntax matches the chosen version.

## Inputs

- **Programming language** (choose one: Python, JavaScript, TypeScript, Java, C#, Go, Ruby)
- **Test framework** (choose one: pytest, unittest, Jest, Vitest, Mocha, JUnit, NUnit, Go test, RSpec)
- **Function signature** (free text, e.g. "def parse_price(raw: str) -> Decimal")
- **Function body** (longer free text)
- **Where it lives** (free text, e.g. "src/billing/prices.py")
- **Coverage level** (choose one: Basic (happy path), Moderate (plus edge cases), Full (plus error paths and mocks))
- **Include mocking** (choose one: Yes, No)
- **Number of test cases** (number from 1 to 20)

## Output

A single code block with the complete test file, opening with a one-line comment on what is covered, runnable without edits.

## Prompt template

Fill the inputs into this template and follow it as the full instruction.

### Unit Test Generator

Write the unit tests for the function below.

- Programming language: {{Programming language}}
- Test framework: {{Test framework}}
- Function signature: {{Function signature}}
- Function body: {{Function body}}
- Where it lives: {{Where it lives}}
- Coverage level: {{Coverage level}}
- Include mocking: {{Include mocking}}
- Number of test cases: {{Number of test cases}}

Read the function body and list its branches before writing a test: each if, each early return, each raised or thrown error, each loop with zero, one, and many items. Derive the test cases from that list rather than from the signature alone, so each case exercises a different branch, and name every test after the behaviour it proves. Use the framework's native parametrisation for inputs that differ only in value. When mocking is on, mock only what the function reaches outside itself (network, filesystem, time, database) using the framework's own tools, and assert the mock was called with the right arguments.

Keep the file self-contained: imports at the top matching the module path, any fixtures or setup inside the file, no helpers the project does not have. Start with a short comment that names the function under test and what the cases cover.

### Output
Return one code block containing the complete test file, ready to copy into the project and run without edits, with a one-line comment at the top describing what is covered.

## Pitfalls

- Deriving cases from the signature instead of the body, which tests the happy path three ways and no error branch.
- Mocking the function's own helpers, so the test passes while the real logic never runs.
- Importing from a guessed path or a project fixture that does not exist, so the file fails before the first test.
