Skip to main content
coding intermediate

Create Comprehensive Unit Tests for Any Function

Generate comprehensive unit tests for any function with edge cases, mocking, and best practices. Perfect for developers wanting thorough test coverage.

Works with: chatgptclaudegemini

Prompt Template

You are an expert software testing engineer. I need you to create comprehensive unit tests for a function. Function to test: ```[PROGRAMMING_LANGUAGE] [FUNCTION_CODE] ``` Please create unit tests that include: 1. **Happy path tests** - Test normal expected inputs and outputs 2. **Edge cases** - Test boundary conditions, empty inputs, null values, etc. 3. **Error handling** - Test invalid inputs and expected exceptions 4. **Mock dependencies** - If the function has external dependencies, show how to mock them 5. **Performance considerations** - If relevant, include tests for performance requirements For each test case, provide: - A descriptive test name that explains what's being tested - Clear arrange, act, assert structure - Comments explaining the test's purpose - Any necessary setup or teardown code Use the testing framework: [TESTING_FRAMEWORK] Additional requirements: [ADDITIONAL_REQUIREMENTS] Please organize the tests logically, include imports/setup code, and follow best practices for the specified programming language and testing framework. Aim for high code coverage while keeping tests maintainable and readable.

Variables to Customize

[PROGRAMMING_LANGUAGE]

The programming language of the function

Example: Python

[FUNCTION_CODE]

The complete function code to create tests for

Example: def calculate_discount(price, discount_percent, customer_type='regular'): if price <= 0: raise ValueError('Price must be positive') if discount_percent < 0 or discount_percent > 100: raise ValueError('Discount must be between 0 and 100') base_discount = price * (discount_percent / 100) if customer_type == 'premium': base_discount *= 1.1 return round(price - base_discount, 2)

[TESTING_FRAMEWORK]

The testing framework to use

Example: pytest

[ADDITIONAL_REQUIREMENTS]

Any specific testing requirements or constraints

Example: Include parametrized tests and ensure all branches are covered

Example Output

```python import pytest from your_module import calculate_discount class TestCalculateDiscount: def test_calculate_discount_regular_customer_happy_path(self): """Test normal discount calculation for regular customer""" # Arrange price = 100.0 discount_percent = 10.0 # Act result = calculate_discount(price, discount_percent) # Assert assert result == 90.0 def test_calculate_discount_premium_customer(self): """Test discount calculation with premium customer bonus""" # Arrange price = 100.0 discount_percent = 10.0 customer_type = 'premium' # Act result = calculate_discount(price, discount_percent, customer_type) # Assert assert result == 89.0 # 100 - (10 * 1.1) = 89 @pytest.mark.parametrize("price,discount,expected", [ (50.0, 20.0, 40.0), (99.99, 15.0, 84.99), (10.0, 0.0, 10.0), (100.0, 100.0, 0.0) ]) def test_calculate_discount_parametrized_cases(self, price, discount, expected): """Test various price and discount combinations""" result = calculate_discount(price, discount) assert result == expected def test_calculate_discount_invalid_price_raises_error(self): """Test that negative or zero price raises ValueError""" with pytest.raises(ValueError, match="Price must be positive"): calculate_discount(0, 10) with pytest.raises(ValueError, match="Price must be positive"): calculate_discount(-5, 10) def test_calculate_discount_invalid_discount_raises_error(self): """Test that invalid discount percentage raises ValueError""" with pytest.raises(ValueError, match="Discount must be between 0 and 100"): calculate_discount(100, -1) with pytest.raises(ValueError, match="Discount must be between 0 and 100"): calculate_discount(100, 101) ```

Pro Tips for Best Results

  • Always include the function's imports and dependencies in your prompt for accurate test generation
  • Specify the exact testing framework and version if you have specific syntax requirements
  • Ask for parametrized tests when you need to test multiple input combinations efficiently
  • Include any existing test patterns or naming conventions used in your codebase
  • Request coverage reports or mention specific coverage targets if code coverage is important

Tags

Want 500+ Expert Prompts?

Get the Premium Prompt Pack — organized, tested, and ready to use.

Get it for $29

Related Prompts You Might Like