Home / Software Development / Unit Test Generator Prompt: Behaviors First, Then Tests

Software Development

Unit Test Generator Prompt: Behaviors First, Then Tests

Generate focused unit tests by listing a function's behaviors and edge cases first, then writing tests that check behavior instead of implementation.

3 min read · Published October 18, 2026 · Last reviewed October 18, 2026

Asking a model to "write tests" often returns a long file that mirrors the implementation line by line. Such tests pass today and break on the next harmless refactor, so they protect nothing.

This prompt flips the order. It first lists what the function is supposed to do and where it could surprise you, asks questions about unclear behavior, and only then writes tests. The questions are often the most valuable part of the output.

The prompt

Copy and paste
Act as a senior engineer who writes maintainable unit tests.

Stack: {STACK}

For the code below:
1. List the behaviors it should have, as plain sentences.
2. List edge cases and inputs that could surprise (empty values, boundaries, unusual characters, large values).
3. Ask me about any behavior that is unclear or looks like a bug. Do not silently assume.
4. Write unit tests that check behavior through the public interface, not internal details. Use clear test names that read as sentences.
5. Note anything that should be tested at another level (integration, end to end).

Rules:
- Do not change the code under test. If you think it has a bug, say so.
- No mocks unless the code has external dependencies.
- Keep each test focused on one behavior.

Code:
{CODE}

What to replace

VariableWhat to put there
{STACK}Language and test framework, for example "TypeScript, Vitest".
{CODE}The function or module under test. One unit at a time works best.

How to use it

  1. Paste a single function or small module with your language and test framework.
  2. Read the list of behaviors. If it describes something you did not intend, you may have found a bug or a missing requirement.
  3. Answer the model's questions, then ask it to update the tests.
  4. Run the tests. Make sure at least one fails when you deliberately break the code.
  5. Delete tests that only repeat each other. A smaller suite you trust is better.

Worked example

This example was written for this page to show the format. Results from your own model and your own material will differ, so run the prompt yourself before relying on it.

Input

export function slugify(title: string): string {
  return title
    .toLowerCase()
    .trim()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '');
}

Output

Behaviors: lowercases text; joins words with single hyphens; removes punctuation; trims leading and trailing separators.
Edge cases: empty string; only punctuation; repeated spaces; accented or non-Latin letters.

Question for you: non-ASCII letters are removed, so "Çay" becomes "ay". Is that intended, or should accented letters be transliterated?

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

describe('slugify', () => {
  it('lowercases and joins words with hyphens', () => {
    expect(slugify('Hello World')).toBe('hello-world');
  });
  it('removes punctuation', () => {
    expect(slugify("What's new?")).toBe('what-s-new');
  });
  it('collapses repeated separators', () => {
    expect(slugify('a --  b')).toBe('a-b');
  });
  it('trims leading and trailing separators', () => {
    expect(slugify('  ...Hello...  ')).toBe('hello');
  });
  it('returns an empty string when nothing usable remains', () => {
    expect(slugify('???')).toBe('');
  });
});

Other levels: if slugs must be unique in a database, test that rule in an integration test.

Variations

SituationWhat to change
Legacy code without testsAsk for characterization tests that record current behavior, and flag anything that looks wrong separately.
Function with network callsDescribe the dependencies and ask which ones to fake and which to leave real.
Property-based testingAsk for properties that should always hold, for example "output never starts with a hyphen".
Test reviewPaste existing tests and ask which behaviors they miss and which tests are redundant.

Common mistakes

Frequently asked questions

Does this work for languages other than TypeScript?

Yes. Change the stack line and the model will adapt the style and framework.

How many tests are enough?

Enough that you would be confident changing the function. Cover each behavior and each risky edge case once.

Can the model run the tests?

Not in a normal chat. Run them in your own environment.

More prompts