Create Pytest Fixtures and Parametrized Tests for Thorough Code Coverage
Generate a full Pytest suite with reusable fixtures, parametrized edge-case tests, custom markers, and high branch coverage targets.
๐ The Prompt
Write a comprehensive Pytest test suite for a [PYTHON_MODULE_NAME] module that contains the following functions/classes: [FUNCTION_OR_CLASS_LIST]. The module's primary responsibility is [MODULE_PURPOSE].
Structure the test suite as follows:
1. **Fixtures** (place in `conftest.py`):
- Create a `[PRIMARY_FIXTURE_NAME]` fixture that sets up [SETUP_DESCRIPTION] with scope=[SCOPE] (function/class/module/session)
- Create a database/mock fixture using [MOCK_LIBRARY] (e.g., unittest.mock, pytest-mock, factory_boy) to simulate [DEPENDENCY_NAME]
- Add a teardown/cleanup step that [TEARDOWN_ACTION]
- Create a fixture that yields a temporary [RESOURCE_TYPE] for integration-style tests
2. **Parametrized Tests**:
- Use `@pytest.mark.parametrize` to test [FUNCTION_NAME] with at least 6 input variations covering:
a. Happy path with typical input
b. Edge cases (empty strings, zero values, None, boundary values)
c. Invalid input types that should raise [EXCEPTION_TYPE]
d. Large/stress input of [LARGE_INPUT_DESCRIPTION]
- Use indirect parametrize for fixture-based parameterization where appropriate
3. **Assertions & Markers**:
- Use `pytest.raises` for exception testing
- Add custom markers: `@pytest.mark.slow` for tests exceeding [THRESHOLD]ms
- Group tests into classes by feature area
4. **Coverage Target**: Aim for [COVERAGE_PERCENT]% branch coverage.
Include clear docstrings for each test explaining the scenario being validated. Output the full `conftest.py` and `test_[MODULE_NAME].py` files.
๐ก Tips for Better Results
List your actual function signatures and expected return types so the AI can generate realistic assertions. Specify the exact exceptions your code raises to get accurate pytest.raises blocks. Mention any async functions so the AI includes pytest-asyncio setup.
๐ฏ Use Cases
Python developers use this when they need to rapidly scaffold a thorough test suite for existing modules, especially before refactoring or when onboarding a legacy codebase to CI.