Asking AI to Write Test Cases
AI can generate test cases for functions quickly. Test case prompts should specify the testing framework, edge cases, and the types of tests you need.
10 min•By Priygop Team•Updated 2026
Test Case Generation Prompt Example
Test Case Generation Prompt Example
# Test case generation prompt template
test_cases_prompt = """
Write unit tests for this Python function using pytest.
Function to test:
def calculate_discount(price, discount_percent):
if discount_percent < 0 or discount_percent > 100:
raise ValueError("Discount must be between 0 and 100")
return price * (1 - discount_percent / 100)
Write tests that cover:
1. A normal case with a valid price and discount
2. A 0% discount (price unchanged)
3. A 100% discount (price becomes 0)
4. Negative discount (should raise ValueError)
5. Discount over 100% (should raise ValueError)
Format: use pytest style with clear test function names.
Include a brief comment explaining what each test checks.
"""
# The generated tests will cover:
# - Happy path (normal inputs)
# - Boundary cases (0%, 100%)
# - Error cases (invalid inputs)
print("Good test prompts specify the framework and all edge cases.")
print("Review AI-generated tests and add any cases it missed.")Diagram
Loading diagram…
Deep Learning ⊂ Machine Learning ⊂ Artificial Intelligence
Key Takeaways
- Test case prompts should specify the testing framework (pytest, Jest, etc.)
- Ask for normal cases, boundary cases, and error cases
- Include the function code in the prompt so AI understands what to test
- Review AI test cases and add any cases it missed
- AI-generated tests are starting points; your knowledge of edge cases matters too
Key Takeaways
- AI can generate test cases for functions quickly.
- Test case prompts should specify the testing framework (pytest, Jest, etc.)
- Ask for normal cases, boundary cases, and error cases
- Include the function code in the prompt so AI understands what to test