Skip to main content
UseCasePilot

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

prompt.txt
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

  1. 1Copy the prompt template above.
  2. 2Paste it into ChatGPT at chat.openai.com.
  3. 3Specify your testing framework and programming language.
  4. 4Replace [PASTE FUNCTION HERE] with the function you want to test.
  5. 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 plan

CursorAI-native code editor built for pair programming with LLMs.

Try Cursor

Recommended Tool

Free trial

GitHub CopilotAI pair programmer that suggests code completions in real time.

Try GitHub Copilot

Recommended Tool

Free plan

TabninePrivacy-focused AI code assistant for teams.

Try Tabnine

Related Prompt Templates

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.