Write Pytest Fixtures and Parametrized Tests for Comprehensive Code Coverage
Generate a full Pytest test suite with reusable fixtures, parametrized edge cases, and coverage targets for any Python module.
๐ 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 purpose is [MODULE_PURPOSE].
**Step 1 โ Fixtures**:
Create reusable fixtures for the following test dependencies:
- A [DATABASE_OR_SERVICE] connection mock using `pytest-mock` or `unittest.mock`
- Sample valid input data representing [VALID_DATA_DESCRIPTION]
- Sample invalid input data representing [INVALID_DATA_DESCRIPTION]
- A temporary file/directory fixture if file I/O is involved
- Use `conftest.py` for shared fixtures and scope them appropriately (function, class, module, session). Explain why you chose each scope.
**Step 2 โ Parametrized Tests**:
Use `@pytest.mark.parametrize` to test:
- At least [NUMBER] happy-path scenarios with varying valid inputs
- Edge cases including: empty inputs, boundary values for [BOUNDARY_FIELDS], None values, and type mismatches
- Expected exceptions using `pytest.raises` with match patterns
**Step 3 โ Organization**:
- Group tests into classes by feature area
- Use descriptive test names following the pattern `test_<function>_<scenario>_<expected_result>`
- Add `pytest.mark` custom markers for `slow`, `integration`, and `unit` categories
**Step 4 โ Coverage Target**:
Aim for [COVERAGE_PERCENT]% branch coverage. Identify any lines that are difficult to cover and suggest refactoring strategies.
Include a `pytest.ini` or `pyproject.toml` configuration snippet with recommended settings for the test run.
๐ก Tips for Better Results
1. Provide the actual function signatures or paste the source code so the AI can generate assertions that match real return values. 2. Start with fixture scoping at 'function' level and only broaden to 'session' for genuinely expensive setups like database connections. 3. Review parametrize IDs โ add the `id` parameter for readability in test reports.
๐ฏ Use Cases
Python developers and QA engineers use this when building or expanding test suites to ensure thorough coverage and maintainable test infrastructure.