Characterization Tests Before a Refactor

Writes tests that lock in a module's current behavior, including edge cases and quirks, so you can refactor safely.

You get: Working code with an explanation

More like this

The prompt

characterization-tests-before-a-refactor.md
You are a senior engineer who specializes in safely refactoring legacy code. I am about to refactor this module or file: {paste your module or file here}. If you do not know the language or test framework, or the code depends on things you cannot see, ask me up to two short questions first. Write characterization tests that pin down what the code does today, not what it should do. Rules: do not change the source; cover happy paths, boundaries, empty and null inputs, error paths and side effects; stub external dependencies and say what you stubbed; when current behavior looks like a bug, keep the test and add a comment starting with QUIRK. Output format: 1) Behavior inventory as a table with function, input, observed output. 2) Test code in one block. 3) List of quirks. 4) Untested areas and risks, such as time, randomness or I/O. 5) Next actions before starting the refactor. The answer is good if every test would pass against the unchanged code and a behavior change would make a test fail.

Example output

AI-written example
# test_pricing_characterization.py
# Characterization tests for pricing.py (pins CURRENT behavior, not intended behavior).
#
# 1) BEHAVIOR INVENTORY
# | function         | input                              | observed output          |
# |------------------|------------------------------------|--------------------------|
# | apply_discount   | (100, "SAVE10")                    | 90.0                     |
# | apply_discount   | (9.99, "HALF")                     | 4.995 (not rounded)      |
# | apply_discount   | (100, None) / (100, "")            | 100                      |
# | apply_discount   | (100, " save10 ")                  | 90
Generated from this prompt by AI. Your results will vary.

Tips and common edits

Use before changing code that has few or no tests. Paste the file or module, and mention your test framework if you know it.

Questions about this prompt

More in Coding

Codebase Tour for a Specific Task

Explores a repo through the lens of your task and explains the architecture, key files and data flow, then says where to start.

Failing Test First Bug Fix

Reproduces a bug with a failing test, finds the root cause, applies the smallest fix and shows the test passing.

Unit Test Plan and Test Code for an Existing Function

Builds a behavior-based test matrix for a function or module and then generates test code in your framework. Covers edge cases, failures, and mocking boundaries, with coverage gaps stated honestly.

Pull Request Review with Merge Recommendation

Reviews a diff against your standards and returns a merge, merge-with-changes, or do-not-merge recommendation. Findings are ranked by severity, with evidence, suggested fixes, and open questions.

Root Cause Analysis from Stack Trace and Logs

Turns an error report, stack trace, and logs into ranked root-cause hypotheses. Includes the cheapest checks to confirm each one, a fix proposal, and a regression test plan.

Architecture Decision Record for a Technology Choice

Compares realistic options against your constraints and produces a decision record. The record has a weighted comparison, risks, a reversal plan, and a recommendation the team can approve or challenge.

Explain This Error with Ranked Causes

Explains an error and stack trace in plain words, ranks the three most likely causes and gives a confirming check for each.

Regex with Match and Non-Match Test Strings

Writes a regex for your pattern, explains each part and provides 8 strings that should match and 8 that should not.

SQL Query from Schema and Question

Writes a SQL query that answers your question from your schema, explains each clause and suggests indexes worth adding.

Cursor .mdc Rules to Portable AGENTS.md Converter

Converts your pasted Cursor .mdc rules into a single AGENTS.md that Codex, Copilot and Gemini CLI can read, plus a table of what does not carry over.

Edge-Case Test Suite for One Function in Your Existing Test Framework

Writes tests for a single function that cover normal inputs, boundaries, bad input and error paths, in the test framework your project already uses.

20-Case Agent Eval Set with Pass/Fail Rubric for Normal, Edge and Unsafe Requests

Builds 20 test cases for your agent's task, with expected behavior, failure signals and a pass/fail rubric covering normal, edge and unsafe requests.