← IndexEntry № 099·coding

Write Unit Tests for an Existing Function

Generate a thorough unit test suite covering happy path, branches, edges, and errors.

Optimized for
ClaudeChatGPT
§ When to use this

This prompt generates a thorough unit test suite for a function you paste in, in the test framework you specify. It explicitly asks for coverage across the happy path, every branch, edge cases, and error conditions, which is exactly the set most hand-written tests skip under time pressure. Requiring the AAA pattern and plain-English behavioral test names produces tests that double as living documentation of what the function is supposed to do.

§ The Prompt— fill in the fields, then copy or open in a tool
§ Customize0/2 fields filled
your prompt — fill the fields above
Write unit tests for this function. Code:

[PASTE CODE]

Test framework: [VITEST/JEST/PYTEST/ETC]. Cover: (1) happy path, (2) all branch conditions, (3) edge cases (empty, null, max, boundary), (4) error cases. Use the AAA pattern (Arrange-Act-Assert). For each test, the name must describe the behavior in plain English. Add one comment for any test that needs context.
Open with your prompt →ChatGPTClaudeSends your filled-in prompt straight into a new chat.
§ Example Output

What you can expect back

import { describe, it, expect } from 'vitest';
import { applyDiscount } from './applyDiscount';

describe('applyDiscount', () => {
  it('applies a standard percentage discount to a positive price', () => {
    // Arrange
    const price = 100, percent = 20;
    // Act
    const result = applyDiscount(price, percent);
    // Assert
    expect(result).toBe(80);
  });

  it('returns the full price when the discount is 0 percent', () => {
    expect(applyDiscount(50, 0)).toBe(50);
  });

  it('returns 0 when a 100 percent discount is applied (boundary)', () => {
    expect(applyDiscount(50, 100)).toBe(0);
  });

  it('throws when price is negative', () => {
    expect(() => applyDiscount(-1, 10)).toThrow('price must be non-negative');
  });

  it('throws when percent is above 100 (out of range)', () => {
    expect(() => applyDiscount(100, 150)).toThrow('percent out of range');
  });
});

Illustrative example — your results will vary by tool and inputs.

§ Pro Tips

Get sharper results

  • 01Paste the function's real dependencies or type definitions too, so the generated tests mock collaborators correctly instead of guessing their shape.
  • 02Ask the model to list which branches and edge cases it identified before writing tests, so you can confirm nothing important was missed.
  • 03Tell it whether to mock external calls (network, database, time) and how, since untold assumptions here lead to flaky or non-runnable tests.
  • 04Request a coverage rationale comment for tricky cases so a reviewer understands why an unusual input was chosen.
  • 05Run the generated suite immediately; if a test fails, that's a signal either the test is wrong or you've found a real bug to investigate.
§ Variations

Adapt it for your case

Property-based tests

Ask for tests using a property-based library (fast-check, Hypothesis) that assert invariants across generated inputs rather than fixed examples.

Add tests to existing suite

Paste your current test file too and ask only for the missing cases, matching the existing style and helpers.

Find bugs first

Before generating tests, ask the model to review the function for bugs and edge cases it can't handle, then write tests that expose them.

Use For — Tasks
Tags#testing#tdd#quality
§ FAQ

Common questions

Will the tests run without changes?

Usually they need minor fixes, import paths, mock setup, or framework config. Run them right away and adjust; treat the output as a strong draft, not final.

Do generated tests guarantee my function is correct?

No. They verify behavior against what the model infers the function should do. If a test encodes a wrong assumption, it can pass while a real bug remains.

How do I handle functions with side effects?

Tell the model exactly what to mock (APIs, databases, timers) and how, since it can't know your environment. Clear mocking instructions are key to runnable tests.

§ Related Entries

You may also need