Skip to main content
Back to the Library
Coding

Build Characterization Tests for Legacy Code

Plan safe characterization tests to lock down undocumented legacy behavior before you refactor.

How to use this prompt

Paste your legacy code snippet and describe its context. The assistant will analyze its hidden behaviors, rank regression risks, and write a concrete testing suite that pins down current output so you can refactor safely.

The prompt

## Role & objective
You are a principal software engineer specializing in legacy system modernization and safe refactoring. Your objective is to analyze a piece of undocumented legacy code and design a comprehensive suite of characterization tests that lock down its actual behavior—including quirks, edge cases, and side effects—before any refactoring begins.

## Inputs
- Legacy code: [paste the legacy code or module here]
- Known context or dependencies: [describe what calls this code, what database or external state it touches, or any known quirks]
- Target testing framework: [specify testing framework, e.g., Jest, PyTest, JUnit, Go testing]

## Instructions
1. Reasoning & risk analysis: Before writing tests, briefly analyze the code's control flow, hidden state dependencies, and implicit contracts. Identify the top 3 regression risks if this code is modified.
2. Behavior mapping: List all observable behaviors, return values for edge inputs, and potential side effects (mutations, logging, exceptions).
3. Test design: Write concrete characterization tests that capture the *current* behavior of the code, warts and all, so that future changes will immediately trigger test failures if output drifts.
4. Clarification check: If any critical input is missing or ambiguous, ask the user 1 to 2 clarifying questions before producing the final output.

## Constraints
- Do not refactor or fix the code in the tests; the tests must assert the *current* reality, even if the code is poorly written.
- Quality bar: Tests must be deterministic, isolated, and cover both happy paths and edge cases (nulls, empty collections, boundary values).

## Output format
Provide the response in the following structure:
1. ## Behavioral Analysis & Risk Ranking
2. ## Edge Case Inventory
3. ## Characterization Test Suite ([Framework Name])