ChatGPT Prompt for Writing Unit Tests
Use this ChatGPT prompt to generate unit tests for your code. Copy the template, paste your function, and get a complete test suite covering edge cases.
Prompt Template
You are a senior software engineer specialising in test-driven development. Write a comprehensive unit test suite for the following 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. 4. Clear test names that describe what each test verifies. Testing framework: [e.g. Jest, Pytest, JUnit] Programming language: [e.g. TypeScript, Python, Java] 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 testing framework and programming language.
- 4Replace [PASTE FUNCTION HERE] with the 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 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
function divide(a: number, b: number): number {
if (b === 0) throw new Error('Division by zero')
return a / b
}Expected Output
The AI will generate a Jest test suite covering: dividing two positive numbers, dividing with negative numbers, dividing by zero (exception test), dividing zero by a number, and floating point results.
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 unit tests?
Yes. ChatGPT can generate unit tests in most frameworks including Jest, Pytest, JUnit, and Mocha. It covers happy paths, edge cases, and error conditions when given a clear function to test.
What testing frameworks does ChatGPT support?
ChatGPT is familiar with Jest, Vitest, Mocha, Pytest, JUnit, RSpec, Go testing, and more. 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.