Create Pytest Test Suite with Fixtures and Parametrize Decorators
Generate a well-structured Pytest suite featuring reusable fixtures, parametrized tests, markers, and proper mocking patterns.
๐ The Prompt
Write a complete Pytest test suite for a Python module named [MODULE_NAME].py that implements [MODULE_PURPOSE]. The module contains these classes/functions:
[LIST_CLASSES_AND_FUNCTIONS_WITH_SIGNATURES]
Generate the test suite with the following components:
1. **Conftest.py Fixtures**:
- Create reusable fixtures for [SHARED_RESOURCES] (e.g., database session, API client, temporary files)
- Use appropriate scopes: function, class, module, or session โ and explain why each scope was chosen
- Implement yield-based fixtures with proper teardown/cleanup logic
- Add a fixture that returns a factory function for creating [TEST_ENTITY] with customizable attributes
2. **Parametrized Test Cases**:
- Use @pytest.mark.parametrize for [FUNCTION_TO_PARAMETRIZE] with at least 5 input/output combinations including:
- Typical valid inputs
- Boundary values (min, max, zero, empty)
- Invalid inputs that should raise [EXPECTED_EXCEPTION]
- Use pytest.param() with id= for readable test IDs
- Stack multiple parametrize decorators for combinatorial testing where [COMBINATORIAL_SCENARIO]
3. **Test Organization**:
- Group related tests in classes prefixed with Test (e.g., TestUserService)
- Use markers: @pytest.mark.slow, @pytest.mark.integration, and custom marker @pytest.mark.[CUSTOM_MARKER]
- Include tests that use [MOCK_LIBRARY] (unittest.mock or pytest-mock) to patch [EXTERNAL_DEPENDENCY]
4. **Assertion Patterns**:
- Use pytest.raises() as context manager with match= for exception messages
- Use pytest.approx() for floating-point comparisons where applicable
- Add caplog fixture usage for testing log output from [LOGGING_FUNCTION]
5. **Configuration**: Provide a minimal pytest.ini or pyproject.toml [tool.pytest.ini_options] section with marker registrations and default flags.
Include docstrings for each test explaining the scenario being validated.
๐ก Tips for Better Results
Include the actual function signatures and return types so the AI generates accurate assertions and parametrize tuples. Specify whether you're using pytest-mock or unittest.mock for consistent patching syntax. Mention your Python version to avoid incompatible syntax like match groups in pytest.raises().
๐ฏ Use Cases
Python developers and QA engineers use this when designing scalable, maintainable test suites for complex business logic with many input variations and shared setup requirements.