Join WhatsApp
Sample output Ā· AI-generated

Step 1: Cases
Normal: 10 percent off 200. Edge: 0 percent, 100 percent, rounding to 2 decimals. Errors: negative percent, percent over 100.

Step 2: Tests
```python
import pytest
from pricing import apply_discount

def test_applies_regular_discount():
assert apply_discount(200, 10) == 180

def test_zero_percent_keeps_price():
assert apply_discount(99.99, 0) == 99.99

def test_full_discount_makes_item_free():
assert apply_discount(50, 100) == 0

def test_rounds_to_two_decimals():
assert apply_discount(19.99, 15) == 16.99

@pytest.mark.parametrize("bad_percent", [-5, 101])
def test_rejects_invalid_percent(bad_percent):
with pytest.raises(ValueError):
apply_discount(100, bad_percent)
```

Step 3: Notes
The function accepts a negative price and returns a negative result. The rules do not say whether that is allowed. If it is not, add a check and a test for it.

Text prompt Ā· ChatGPT Ā· Claude Ā· Gemini

Unit test prompt for edge cases in pytest or Jest

Get a list of test cases for a function, then ready-to-run tests for normal input, edge cases and errors in your framework.

You are a [language] developer who writes clear, focused tests. Write unit tests for the code below using [test framework, e.g. pytest, Jest, JUnit].

Code under test:
```
[paste function or class]
```
Business rules it must follow: [rules, e.g. discount cannot be over 100 percent]
Dependencies to mock: [database, API, time, or none]

Step 1: List the test cases you plan to write, grouped as normal cases, edge cases and error cases. One line each.
Step 2: Write the tests.
- One behaviour per test, with a descriptive test name.
- Use parametrised tests where several inputs check the same rule.
- Do not test private details of how the code works, only its behaviour.
Step 3: Point out any behaviour in the code that looks like a bug or is unclear from the rules.
Type
Text
Fill in
5 blanks
Copies
Be the first

Updated Ā· By

Tip: Run the new tests once with a deliberate bug in the code; a test that never fails is not protecting you.

1. CopyTap "Copy prompt" above.
2. Fill the blanksReplace each [bracket] with your own details.
3. Paste + refinePaste it into the chat, then ask for changes.

Why this prompt works

This unit test prompt asks for a plan of cases before any code, so you can spot missing scenarios in a short list instead of reading through long test files. Grouping cases into normal, edge and error makes gaps obvious.

Sharing the business rules matters because tests should check what the code is supposed to do, not just repeat what it currently does. Asking the AI to flag behaviour that looks like a bug turns test writing into a quick code review as well.

How to use this prompt

  1. Tap "Copy prompt".
  2. Replace every part in [square brackets] with your own details.
  3. Paste it into ChatGPT or Claude and send.
  4. Ask a follow-up like "make it shorter" or "give me 3 more options" to fine-tune it.

Variations to try

Tests for a function with an API call

ChatGPT Claude Gemini
Write [framework] tests for this function that calls an external API: [paste code]. Mock the API with [mocking tool]. Cover a successful response, a timeout, a 500 error and an unexpected response shape. Assert on what the function returns, not on internal calls, except where the call itself is the behaviour.
Type
Text
Fill in
3 blanks
Copies
Be the first

Add tests before refactoring

ChatGPT Claude Gemini
I want to refactor this legacy [language] code without changing its behaviour: [paste code]. Write characterisation tests that capture what it does today, including odd behaviour. Mark any test that captures behaviour that looks like a bug.
Type
Text
Fill in
2 blanks
Copies
Be the first

Make it your own

  • Change the framework line to Jest, Vitest, JUnit, RSpec or Go's testing package.
  • Add "Use the Arrange, Act, Assert layout with blank lines" for team style.
  • Ask for property-based tests (for example with Hypothesis) for functions with many possible inputs.

Common problems and fixes

  • Tests only cover the happy path. Ask the AI to add at least 3 edge cases and 2 error cases before writing code.
  • Tests break every time the code changes. Keep the rule to test behaviour, not private details; avoid asserting on internal calls.
  • Tests pass even when the code is wrong. Break the function on purpose and check that at least one test fails.

FAQ

Can AI-written tests replace my own?

They are a good start, but you know the real rules. Review every case and add the ones only your team knows about.

How many tests does one function need?

Enough to cover each rule, the edges and the errors. For a small function, 4 to 8 focused tests is common.

More Coding prompts

See all
ChatGPT Claude Text prompt

Debug code prompt that finds the root cause before fixing

Paste your code and the error message to get the real cause explained first, then a small fix and a test for it.

4 prompts
ChatGPT Claude Text prompt

Explain code prompt for beginners, line by line

Paste any snippet and get a plain-English summary, a line-by-line walkthrough and the concepts you need to understand it.

4 prompts
ChatGPT Claude Text prompt

Refactor code prompt for cleaner, more readable functions

Clean up a messy function while keeping its behaviour the same, with a short list of every change and the reason for it.

4 prompts
ChatGPT Claude Text prompt

SQL query prompt: describe your tables, get the query

Describe your tables and the question you want answered, and get a correct query for your database with a short explanation.

4 prompts
ChatGPT Claude Text prompt

Regex prompt that writes and explains a pattern with tests

Describe what you want to match and get a working pattern for your language, explained piece by piece, with examples that should and should not match.

4 prompts
ChatGPT Claude Text prompt

Code review prompt for bugs, security and readability

A structured review of a pull request or snippet, with issues sorted by severity, each tied to a line and a suggested fix.

3 prompts
ChatGPT Claude Text prompt

API design prompt for clean REST endpoints and errors

Plan a REST API for a feature: endpoints, example requests and responses, status codes, error format and the decisions behind them.

4 prompts
ChatGPT Claude Text prompt

Vibe coding prompt: write an app spec before you build

Turn an app idea into a clear spec with features, screens, data and build steps that an AI coding assistant can follow one step at a time.

4 prompts