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.

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