Create Pytest Fixtures and Parametrized Tests for Comprehensive Coverage

Generate a comprehensive pytest suite with reusable fixtures, parametrized test cases, and organized test classes for thorough coverage.

πŸ“ The Prompt

Write a complete pytest test suite for a [PYTHON_MODULE_NAME] module that contains the following functions/classes: [FUNCTION_OR_CLASS_LIST]. The module's primary responsibility is [MODULE_PURPOSE]. Please generate the following: 1. **Fixtures** (in a conftest.py file): - A session-scoped fixture that initializes [SHARED_RESOURCE] (e.g., database connection, API client, test configuration) - A function-scoped fixture that provides [TEST_DATA_DESCRIPTION] and properly tears down after each test - A fixture that mocks [EXTERNAL_DEPENDENCY] using unittest.mock or pytest-mock, returning realistic fake responses - Use `yield` for fixtures requiring cleanup and document why each scope was chosen 2. **Parametrized Tests** using @pytest.mark.parametrize: - Test [FUNCTION_NAME] with at least 6 parameter sets covering: valid input, boundary values, empty/null input, type mismatches, large input, and [DOMAIN_SPECIFIC_EDGE_CASE] - Use descriptive test IDs via the `ids` parameter for readable test output - Include indirect parametrize for fixtures where appropriate 3. **Test Organization**: - Group related tests in classes (e.g., TestValidInput, TestEdgeCases, TestErrorHandling) - Add markers for slow tests (@pytest.mark.slow) and integration tests - Include at least one test using pytest.raises for expected exceptions with match patterns 4. **Assertions**: Use specific assertions (assert x == y, not just assert x) and include custom failure messages for complex checks. Provide the conftest.py and test file separately with comments explaining the testing strategy.

πŸ’‘ Tips for Better Results

Provide the actual function signatures and expected behaviors so the AI can generate meaningful assertions rather than generic ones. Use indirect parametrize when your test parameters need transformation through a fixture before reaching the test. Always review generated edge cases against your domain logicβ€”AI may miss business-rule-specific boundaries.

🎯 Use Cases

Python developers use this when building a robust test suite for existing or new modules, especially when they need to quickly scaffold well-organized tests with comprehensive edge case coverage.

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