Characterization Tests for Untested Legacy Code

3 Views
0 Uses

Prompt

ChatGPT
Claude
Grok
OpenRouter
You are a senior engineer about to change legacy code that has no tests. Write characterization tests that pin down what it does today, right or wrong, so any change in behavior shows up.

Language and test framework: {{LANGUAGE}} / {{TEST_FRAMEWORK}}
Legacy code: {{LEGACY_CODE}}
Its dependencies (database, network, clock, files, globals): {{DEPENDENCIES}}

Do this:
1. Find the seams: places where you can control inputs and observe outputs without changing behavior (parameters, injectable dependencies, overridable methods, environment variables). If there are none, suggest the smallest safe change that creates one, and label it clearly as a code change.
2. Choose inputs that exercise each branch. List them in a table with the branch each one reaches.
3. Write the tests so they record current behavior. Where you can't determine the output by reading the code, leave a clear placeholder and tell me to fill it by running the code once (the "golden master" approach).
4. Control non-determinism: freeze the clock, seed randomness, and stub network calls.
5. Mark any behavior that looks like a bug with a comment, but test the current behavior anyway.

Don't refactor the code under test. The point is to capture it as it is.

Customize this prompt

Fill in the variables below to personalize this prompt:

ChatGPT
Claude
Grok
OpenRouter

Model Settings

Recommended Models

ChatGPT Claude Gemini

Temperature

0.7

Max Tokens

2000

Additional Notes

Best for: Building a safety net before touching code nobody understands

About this prompt

“Characterization Tests for Untested Legacy Code” is a free prompt for ChatGPT in the Coding & Software category, 198 words long, added to The Prompt Index on October 28, 2026 by The Prompt Index. Copy it with the button above, paste it into ChatGPT or any other AI chat assistant, and send it.

It has 4 placeholders to fill in first: {{LANGUAGE}}, {{TEST_FRAMEWORK}}, {{LEGACY_CODE}}, {{DEPENDENCIES}}. Use the “Customize this prompt” fields to personalise it before you copy.

Recommended models: ChatGPT, Claude, Gemini.

0 upvotes, sent to an AI tool 0 times, viewed 3 times.

Browse all Coding & Software prompts, the AI prompt database or use the AI prompt finder to search every prompt.