Unit Test Creator
Use Case: Generate comprehensive PyTest unit tests from Python function signatures and docstrings
System Instructions
You are a senior Python test engineer. Write thorough PyTest test suites covering happy paths, edge cases, and error conditions. User Prompt Template
Write PyTest unit tests for the following Python function:
```python
{FUNCTION_CODE}
```
Test requirements: {TEST_REQUIREMENTS} Run This Prompt โ SDK Snippets
Implementation Guidelines
What This Prompt Does
This prompt generates a full PyTest test suite from a Python function signature, including the implementation body and any docstrings. It produces tests for happy paths, boundary conditions, type errors, and expected exceptions โ not just a trivial assert result == expected stub. By instructing the model to reason about edge cases before writing tests, it surfaces test scenarios the developer may not have considered.
System Prompt
You are a senior Python test engineer specializing in PyTest, Test-Driven Development (TDD),
and mutation testing.
When writing unit tests:
1. Analyze the function signature, docstring, and body to enumerate ALL testable behaviors.
2. Structure tests using the Arrange-Act-Assert (AAA) pattern with clear comments.
3. Cover at minimum:
- Happy path: typical valid inputs with expected outputs
- Boundary conditions: empty inputs, zero, None, empty string, empty list
- Type errors: wrong input types (use pytest.raises)
- Value errors: out-of-range or invalid values (use pytest.raises)
- Edge cases specific to the function's domain
4. Use parametrize (@pytest.mark.parametrize) for inputs that vary across a shared assertion pattern.
5. Use descriptive test function names: test_<function_name>_<scenario>.
6. Add a one-line docstring to each test explaining what behavior it verifies.
7. Mock external dependencies (I/O, network, database) using unittest.mock or pytest-mock.
8. Output a complete, runnable .py file with all necessary imports.
User Prompt Template
Write a complete PyTest test suite for the following Python function.
Function to test:
```python
{FUNCTION_CODE}
Test requirements and context:
- {TEST_REQUIREMENTS}
- External dependencies to mock: {DEPENDENCIES_TO_MOCK}
- Minimum test coverage goal: {COVERAGE_GOAL}%
Output a complete test_
## Example Output
**Input `{FUNCTION_CODE}`:**
```python
def calculate_discount(price: float, discount_pct: float) -> float:
"""Apply a percentage discount to a price. Raises ValueError if inputs are invalid."""
if price < 0:
raise ValueError("Price cannot be negative")
if not (0 <= discount_pct <= 100):
raise ValueError("Discount must be between 0 and 100")
return round(price * (1 - discount_pct / 100), 2)
Model output:
import pytest
from mymodule import calculate_discount
class TestCalculateDiscount:
"""Tests for calculate_discount(price, discount_pct)."""
# --- Happy Path ---
def test_standard_discount(self):
"""Applies a 20% discount to a standard price correctly."""
# Arrange
price, discount = 100.0, 20.0
# Act
result = calculate_discount(price, discount)
# Assert
assert result == 80.0
@pytest.mark.parametrize("price,discount,expected", [
(50.0, 10.0, 45.0),
(99.99, 50.0, 50.00),
(0.0, 100.0, 0.0),
(1000.0, 0.0, 1000.0),
])
def test_parametrized_discounts(self, price, discount, expected):
"""Verifies discount calculation across multiple price/discount combinations."""
assert calculate_discount(price, discount) == expected
def test_result_rounds_to_two_decimals(self):
"""Ensures the result is rounded to 2 decimal places."""
result = calculate_discount(10.0, 33.333)
assert result == 6.67
# --- Boundary Conditions ---
def test_zero_discount(self):
"""A 0% discount returns the original price."""
assert calculate_discount(200.0, 0) == 200.0
def test_full_discount(self):
"""A 100% discount returns 0.0."""
assert calculate_discount(200.0, 100) == 0.0
def test_zero_price(self):
"""A zero price always returns 0.0 regardless of discount."""
assert calculate_discount(0.0, 50.0) == 0.0
# --- Error Conditions ---
def test_negative_price_raises(self):
"""Raises ValueError when price is negative."""
with pytest.raises(ValueError, match="Price cannot be negative"):
calculate_discount(-1.0, 10.0)
def test_discount_above_100_raises(self):
"""Raises ValueError when discount exceeds 100."""
with pytest.raises(ValueError, match="Discount must be between 0 and 100"):
calculate_discount(100.0, 101.0)
def test_negative_discount_raises(self):
"""Raises ValueError when discount is negative."""
with pytest.raises(ValueError):
calculate_discount(100.0, -5.0)
Tips & Variations
- Mutation testing integration: Run
mutmutagainst the generated tests to verify they actually catch bugs. If mutants survive (e.g., changing<to<=in the validation), add targeted tests for those boundary values. - Property-based testing: Add
"Also write 2 Hypothesis-based property tests"to{TEST_REQUIREMENTS}. The model will importhypothesisand write@givenstrategies that cover vast input spaces automatically. - Async functions: For
async deffunctions, add"Use pytest-asyncio with @pytest.mark.asyncio"to the requirements. The model will wrap test functions appropriately. - Coverage enforcement: Add a
conftest.pysnippet with--cov-fail-under={COVERAGE_GOAL}to your pytest config so CI enforces the coverage threshold the model was targeting.