Write Pytest Fixtures and Parametrized Tests for Python Applications

Generate a complete pytest test suite with reusable fixtures, parametrized test cases, mocking strategies, and organized test structure.

๐Ÿ“ The Prompt

Write a comprehensive pytest test suite for a Python [MODULE_TYPE (e.g., service class, utility module, API client)] called [MODULE_NAME] that handles [FUNCTIONALITY_DESCRIPTION (e.g., user authentication, payment processing, data transformation)]. Module Under Test: - Key methods/functions to test: [LIST_METHODS (e.g., create_user, validate_token, calculate_total)] - External dependencies to mock: [DEPENDENCIES (e.g., database session, HTTP client, Redis cache)] - Expected exceptions: [EXCEPTION_TYPES (e.g., ValueError, NotFoundError, AuthenticationError)] Please generate the following: 1. **Fixtures (conftest.py)**: - A session-scoped fixture for [SHARED_RESOURCE (e.g., database connection, test configuration)]. - A function-scoped fixture that provides a fresh instance of [MODULE_NAME] with mocked dependencies. - A fixture that yields sample test data for [RESOURCE_NAME] and handles teardown/cleanup. - Use @pytest.fixture with appropriate scopes and autouse where justified. 2. **Parametrized Tests**: - Use @pytest.mark.parametrize to test [METHOD_NAME] with at least 5 variations: valid input, boundary values, invalid types, empty input, and edge cases. - Include IDs for each parameter set for readable test output. - Add a parametrized test that verifies correct exceptions are raised for invalid scenarios. 3. **Test Organization**: - Group tests in a class named Test[MODULE_NAME]. - Include setup/teardown methods where appropriate. - Add markers for slow tests and integration tests. - Use unittest.mock.patch or pytest-mock for dependency isolation. 4. **Assertions**: - Use specific assertions (assert x == y, not just assert x). - Verify mock call counts and arguments with assert_called_once_with. Provide the complete conftest.py and test file with comments explaining fixture scope choices.

๐Ÿ’ก Tips for Better Results

List all external dependencies in [DEPENDENCIES] so the prompt generates accurate mocks. Use descriptive IDs in parametrize decorators โ€” they appear in CI logs and make debugging failures much faster. Start with function-scoped fixtures and only escalate scope when you measure a performance need.

๐ŸŽฏ Use Cases

Python developers use this when setting up test infrastructure for new modules or refactoring existing tests to be more maintainable and data-driven.

๐Ÿ”— 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.