The Prompt Library
CodingPython

Write Unit Tests for Python Function

Generate Pytest-style unit tests for a given Python function.

Select Model:

Prompt:

I need help writing comprehensive unit tests for a Python function using pytest. Please generate well-structured tests that thoroughly verify the function's behavior. Here is the function to test: ```python [PASTE FUNCTION CODE HERE] ``` Please create a complete set of pytest unit tests that: 1. TEST COVERAGE * Tests all main functionality of the function * Covers expected use cases and edge cases * Includes both valid and invalid inputs * Tests error conditions and exceptions * Achieves high code coverage 2. TEST ORGANIZATION * Uses clear test function names that describe what's being tested * Groups related tests logically * Follows pytest best practices and conventions * Uses appropriate fixtures when needed * Includes setup and teardown code if necessary 3. ASSERTIONS & VALIDATION * Uses appropriate pytest assertions * Includes clear failure messages * Validates all relevant aspects of the function's output * Checks both return values and side effects if applicable * Verifies exceptions with proper context 4. MOCKING & ISOLATION * Isolates the function from external dependencies when appropriate * Uses mocks, patches, or stubs as needed * Demonstrates how to test the function independently * Shows how to simulate different scenarios 5. TEST DOCUMENTATION * Includes docstrings explaining test purpose * Comments explaining complex test logic * Demonstrates usage patterns through tests * Provides context for why specific test cases are important Please provide the complete test file that I can directly use with pytest, including all necessary imports and any helper functions or fixtures needed to make the tests run successfully.

Related Prompts

Other prompts you might find useful

CodingPython

Debug Python Error Message

Help diagnose and fix a Python error or traceback.

6 models availableView details
CodingPython

Python Script Generator

Generate a basic Python script for a task, function, or automation.

6 models availableView details
CodingPython

Explain a Python Code Snippet

Break down a piece of Python code and explain what it does in plain English.

6 models availableView details
CodingPython

Refactor Python Code for Clarity

Improve readability and structure of messy or inefficient Python code.

6 models availableView details