ChatGPT Prompt for Writing Python Tests
Use this ChatGPT prompt to generate Python unit tests. Copy the template, paste your function, and get a complete test suite covering edge cases and error conditions.
Prompt Template
You are a senior software engineer specialising in test-driven development. Write a comprehensive unit test suite for the following Python function. Include: 1. Tests for the happy path (expected inputs and outputs). 2. Tests for edge cases and boundary conditions. 3. Tests for error and exception handling. 5. Clear test names that describe what each test verifies. Testing framework: Pytest Function to test: [PASTE FUNCTION HERE]
How to Use This Prompt
- 1Copy the prompt template above.
- 2Paste it into ChatGPT at chat.openai.com.
- 3Specify your preferred testing framework (Pytest).
- 4Replace the placeholder with the Python function you want to test.
- 5Review the generated tests, run them, and adjust as needed.
When to Use This Prompt
- You need to add tests to an existing Python function quickly.
- You want to ensure edge cases are covered before a release.
- You are practising TDD and want to see what tests should look like.
- A code review requires test coverage before merging.
Example Input
def clamp(value, min_val, max_val):
if min_val > max_val:
raise ValueError('min_val must be <= max_val')
return max(min_val, min(value, max_val))Expected Output
The AI will generate a Pytest test suite covering: the happy path, boundary values, error conditions, and invalid input handling. Each test will have a descriptive name.
Recommended AI Tools
Recommended Tool
Free planCursor — AI-native code editor built for pair programming with LLMs.
Recommended Tool
Free trialGitHub Copilot — AI pair programmer that suggests code completions in real time.
Recommended Tool
Free planTabnine — Privacy-focused AI code assistant for teams.
Related Prompt Templates
ChatGPT Prompt for Debugging JavaScript Code
Use this ChatGPT prompt template to debug JavaScript code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging React Code
Use this ChatGPT prompt template to debug React code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging Node.js Code
Use this ChatGPT prompt template to debug Node.js code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Debugging SQL Code
Use this ChatGPT prompt template to debug SQL code faster. Copy the prompt, paste it into ChatGPT, and get a clear explanation of the bug plus a corrected version.
ChatGPT Prompt for Refactoring Python Code
Use this ChatGPT prompt to refactor Python code. Copy the template, paste your code, and get a cleaner, more maintainable version with an explanation of each change.
Frequently Asked Questions
Can ChatGPT write Python unit tests?
Yes. ChatGPT can generate Python tests using Pytest. It covers happy paths, edge cases, and error conditions when given a clear function to test.
What testing frameworks does ChatGPT support for Python?
ChatGPT is familiar with Pytest and several other frameworks. Specify your framework in the prompt to get correctly formatted test code.
How good are AI-generated unit tests?
AI-generated tests are a strong starting point and often catch edge cases developers miss. However, they should be reviewed for correctness and supplemented with tests for business logic that the AI cannot infer from code alone.
Can I use this prompt for integration tests as well?
The prompt is optimised for unit tests. For integration tests, modify it to ask specifically for integration or end-to-end tests, and include information about external dependencies such as databases or APIs.