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.

๐Ÿ”— Related Prompts

๐Ÿ’ป Coding beginner

Explain Code Like Im a Beginner

Get any code explained in plain English with line-by-line breakdowns, analogies, and learning suggestions.

๐Ÿ’ป Coding beginner

Debug My Code and Explain the Fix

Get your code debugged with clear explanations of what went wrong and why, plus the corrected version.

๐Ÿ’ป Coding intermediate

Write Unit Tests for My Code

Generate thorough unit tests covering edge cases, error handling, and both positive and negative scenarios.

๐Ÿ’ป Coding intermediate

Convert Code Between Languages

Convert code between any programming languages while maintaining idiomatic patterns and best practices.

๐Ÿ’ป Coding intermediate

Write a REST API Endpoint

Generate production-ready REST API endpoints with validation, error handling, and documentation.

๐Ÿ’ป Coding advanced

Create a GitHub Actions CI/CD Workflow for Automated Testing and Deployment

Generate a complete GitHub Actions CI/CD workflow with build, test, deploy, and notification jobs for your project.