Coding & Software

Write Unit Tests for a Function

Generates focused unit tests for one function, covering normal cases, boundaries and invalid input without mocking more than needed.

Designed for general-purpose AI assistantsPublished November 2026

Customize

Fill in the fields and the prompt updates. What you type stays in this page and is never saved or sent.

Preview

Write unit tests for the function below using {{test framework}}.

The function should: {{expected behavior}}

{{code}}

Cover, in this order:
- The normal, expected cases.
- Boundary values: empty, zero, one item, maximum, minimum.
- Invalid or unexpected input, and the behavior I described for it.
- Cases where two inputs look similar but should give different results.

Rules:
- One behavior per test, with a name that describes the behavior rather than the function.
- No tests that only repeat the implementation.
- Do not mock anything unless the function touches the network, disk or clock, and say what you mocked and why.
- If my description and the code disagree, point that out before writing tests for either version.

Output only the test file, followed by a short list of behaviors you could not test and why.

3 required fields are empty and will stay as a {{placeholder}} in the copied text.

How to use it

  1. Name your test framework (for example pytest, Jest or JUnit) and describe the intended behavior in a sentence or two.
  2. Paste one function at a time. Tests for a whole module tend to become shallow.
  3. Read the notes about disagreements between your description and the code; they are often real bugs.
  4. Run the tests, then ask the assistant to explain any test that fails before changing the test or the code.

Example output

Illustrative only. Real answers depend on your input and the assistant you use.

Function: parse_duration("1h30m") returns the number of seconds. Framework: pytest.

def test_parses_hours_and_minutes_together():
    assert parse_duration("1h30m") == 5400

def test_accepts_a_single_unit():
    assert parse_duration("45m") == 2700

def test_empty_string_raises_value_error():
    with pytest.raises(ValueError):
        parse_duration("")

def test_rejects_unknown_unit():
    with pytest.raises(ValueError):
        parse_duration("5x")

Not tested: very large values (the description does not say whether there is a maximum) and negative durations (the code accepts "-5m"; the description does not say whether it should).

Tips

Ask for the tests first and the explanation second. That keeps the answer short enough to review in one pass.

  • Coding & Software

    Code Review Checklist Prompt

    A structured review prompt that checks correctness, failure handling, security and tests, and labels every finding by severity.

    Customizable fields
    3 fields
  • Coding & Software

    Debug a Flaky Test

    Finds why a test passes sometimes and fails other times, ranks the likely causes and gives an experiment to prove each one.

    Customizable fields
    8 fields
  • Coding & Software

    Document an API Endpoint

    Writes reference documentation for one API endpoint from its code, covering parameters, responses, errors and a working example.

    Customizable fields
    6 fields