Create Pytest Fixtures and Parametrized Test Suites for Python Applications
Generate a complete pytest test suite with reusable fixtures, parametrized test cases, and proper organization for Python projects.
๐ The Prompt
Write a comprehensive pytest test suite for [MODULE_OR_FUNCTION_NAME] in a [APPLICATION_TYPE] Python application. The module handles [BRIEF_FUNCTIONALITY_DESCRIPTION].
Generate the following:
1. **Fixtures** (in `conftest.py`):
- A session-scoped fixture for [EXPENSIVE_RESOURCE] (e.g., database connection, API client) with proper setup and teardown using `yield`
- A function-scoped fixture that provides [TEST_DATA_DESCRIPTION] with realistic sample data
- A fixture that mocks [EXTERNAL_DEPENDENCY] using `unittest.mock.patch` or `pytest-mock`
- A fixture factory using `@pytest.fixture` with parameters for creating [ENTITY_NAME] objects with customizable attributes
2. **Parametrized Tests**:
- Use `@pytest.mark.parametrize` to test [FUNCTION_NAME] with at least 6 cases covering: valid inputs, boundary values, empty/null inputs, type mismatches, and [DOMAIN_SPECIFIC_EDGE_CASE]
- Include indirect parametrization for fixture-based test data
- Add descriptive test IDs using `pytest.param(..., id='descriptive_name')`
3. **Test Organization**:
- Group tests in a class `Test[FeatureName]` with clear docstrings
- Include `@pytest.mark.slow` for integration-style tests
- Add `@pytest.mark.xfail` for known issues with [KNOWN_ISSUE_DESCRIPTION]
4. **Assertions**: Use specific assertions (`assert x == y`, `pytest.raises`, `pytest.approx`) rather than bare `assert`. Include assertion messages for complex checks.
Assume these dependencies are installed: [DEPENDENCY_LIST]. Follow the Arrange-Act-Assert pattern consistently.
๐ก Tips for Better Results
Describe the actual function signatures and return types of what you're testing so the AI generates accurate assertions. List your real external dependencies in [EXTERNAL_DEPENDENCY] to get useful mock configurations. Review generated edge cases and add domain-specific ones the AI might miss.
๐ฏ Use Cases
Python developers writing or expanding test coverage for existing modules who need well-structured, maintainable test suites with proper fixture management.