← IndexEntry № 022·coding

Generate Unit Tests for a Function

Generate comprehensive unit tests with happy path, edge cases, and failure modes.

Optimized for
ClaudeChatGPT
§ When to use this

This prompt asks a model acting as a TDD expert to write a full test suite for one function: happy path, edge cases, and failure modes, with sentence-style test names and mocked dependencies. Reach for it when you have working code with thin or no coverage and want a thorough starting suite fast. The explicit edge-case checklist (empty, boundary, null/undefined) is what separates this from the one-or-two-tests an LLM writes by default.

§ The Prompt— fill in the fields, then copy or open in a tool
§ Customize0/2 fields filled
your prompt — fill the fields above
You are a test-driven development expert. Write comprehensive unit tests for the function below using [TEST FRAMEWORK: e.g. Jest / pytest / JUnit]. Cover: (1) the happy path, (2) edge cases (empty inputs, boundary values, null/undefined), (3) expected failure modes. Each test should have a descriptive name that reads like a sentence. Mock any external dependencies.

FUNCTION:
[PASTE FUNCTION]
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 { calculateDiscount } from './calculateDiscount';

describe('calculateDiscount', () => {
  it('applies no discount for the free tier', () => {
    expect(calculateDiscount(100, 'free')).toBe(100);
  });

  it('applies a 10 percent discount for the pro tier', () => {
    expect(calculateDiscount(100, 'pro')).toBe(90);
  });

  it('applies a 20 percent discount for the enterprise tier', () => {
    expect(calculateDiscount(50, 'enterprise')).toBe(40);
  });

  it('returns zero when the price is zero', () => {
    expect(calculateDiscount(0, 'pro')).toBe(0);
  });

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

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

§ Pro Tips

Get sharper results

  • 01Name the exact framework version — Jest, Vitest, and pytest differ in mocking syntax, and an unspecified framework gives you tests you have to rewrite.
  • 02Paste the function's real dependencies or their signatures so the mocks match your actual interfaces rather than invented stand-ins.
  • 03Ask it to flag any branches it could not test because of how the code is written — that often reveals a function that needs refactoring to be testable.
  • 04If the function is pure, tell it so; the model can then skip mocking entirely and focus the budget on boundary and property-style cases.
  • 05Request one parameterized/table-driven test for the tier-style variations to keep the suite compact instead of one near-duplicate test per case.
§ Variations

Adapt it for your case

Coverage-gap audit

Paste the function plus your existing tests and ask only for the cases you're currently missing.

Property-based tests

Add 'Use property-based testing (fast-check / Hypothesis) for the numeric inputs' to generate invariant checks instead of fixed examples.

Test-first from a spec

Replace the function with a plain-English spec and ask it to write the failing tests first, before any implementation exists.

Use For — Tasks
Tags#testing#unit-tests#tdd
§ FAQ

Common questions

Will the generated tests actually pass against my code?

Usually for the happy path, but always run them. The model infers behavior from the code it sees, so mismatches around mocks, imports, or edge-case expectations are common and need a quick fix.

How do I get it to mock a database or network call?

Include the dependency's import and call site in what you paste, and name your mocking tool. Without seeing how the dependency is called, it can only guess at the mock surface.

Can it test private functions?

It will try, but testing through the public API is better practice. Ask it to exercise private logic via the exported function rather than reaching into internals.

§ Related Entries

You may also need