Skip to content
PromptIntermediateCoding

Python Unit Test Generator

Generate pytest cases for a function, including boundaries, invalid input and a regression test for a known bug.

Reviewed May 4, 2026 · ChatGPT · Claude · Cursor · GitHub Copilot

Prompt workbench

Your variables

Remove secrets and proprietary data you cannot share.

Works with: ChatGPT, Claude, Cursor, GitHub Copilot

Generated prompt
Write pytest tests for this Python function.

Function:
```python
[Function code]
```

Intended behaviour: [Intended behaviour]
Known constraints: [Constraints]
Known bug or regression: [Known bug / regression]

Requirements:
- Use pytest and the standard library unless a dependency is clearly justified.
- Cover the happy path, boundaries, empty input, invalid types and the known regression.
- Do not test implementation details; assert observable behaviour.
- Use parametrisation where it improves clarity.
- Add a short comment explaining any non-obvious case.
- Do not invent behaviour that is not in the specification. If the correct behaviour is ambiguous, write a test marked xfail with a comment and list the question.

Return the test file and a list of behaviours that still need a product decision.

What this prompt is for

Increase confidence in small Python utilities without generating meaningless coverage.

How to get a better result

  • Paste the real function and its docstring.
  • Run the tests immediately; generated tests often encode wrong assumptions.
  • Keep a failing regression test for every fixed bug.

Example input

A function that parses a date string in YYYY-MM-DD and returns a date object, raising ValueError for invalid input.

Example output

Parametrised valid dates, empty string, wrong separator, impossible date (2026-02-30), whitespace, and a regression test for a previously accepted single-digit month. Plus a note that timezone handling is unspecified.

Example outputs are illustrative. Model behaviour varies; check the result before using it.

Limitations

  • • The model cannot know unstated product requirements.
  • • Generated tests can mirror the implementation's bugs.
  • • Coverage percentage is not the same as test quality.

Related resources