Characterisation Tests for Legacy Code
Plan and draft characterisation tests that pin down what a legacy function actually does today, so it can be changed safely.
When to use it
Use it when you must change code that has no tests and whose intended behaviour nobody can confirm. The prompt proposes inputs that exercise each branch, writes tests that assert current behaviour as observed, and flags behaviour that looks like a bug so you can decide what to do with it.
When not to use it: Do not use it to decide what the code should do: these tests record what it does now, including mistakes. Skip it for code that already has trustworthy tests. Do not paste code that your organisation does not allow you to share.
Required inputs
| Placeholder | What to provide | Example |
|---|---|---|
| {{legacy_code}} | The function or class to characterise, plus the signatures of anything it calls. | A calculateLateFee(booking, today) function with several nested conditions on dates and customer type. |
| {{test_framework}} | Language and test framework, and how tests are laid out in the project. | Python 3 with pytest; tests in tests/ mirror the package layout. |
| {{known_behaviour}} | Anything you already know about expected behaviour, including known bugs and real examples. Use 'unknown' if you have none. | Fees are capped at 30 in the currency's main unit according to support, but nobody has checked the code. |
The prompt
Fill in each {{placeholder}} before you send it.
You are an experienced engineer adding characterisation tests to legacy code. A characterisation test records what the code does now. It does not assert what the code should do.
<legacy_code>
{{legacy_code}}
</legacy_code>
<test_framework>
{{test_framework}}
</test_framework>
<known_behaviour>
{{known_behaviour}}
</known_behaviour>
Do the following in order.
1. Map the behaviour: list each branch, boundary and side effect in the code, with the line or condition it comes from.
2. Choose inputs: for each branch, give a small input set that reaches it, including boundary values just below, at and above each threshold. Note any branch you could not reach and why.
3. Predict outputs by reading the code, and label every expected value "Derived from reading; confirm by running". Do not state expected values with more confidence than a read-through justifies.
4. Write the tests in the given framework. Name each test after the behaviour it pins down, and use synthetic data only. Keep each test independent, and isolate side effects (time, randomness, files, network, databases) with the simplest test double that works.
5. List "Suspicious behaviour": places where the code looks wrong or surprising. Write these tests so they pass against current behaviour, and mark them with a comment saying the behaviour may be a bug.
Rules:
- Do not change the code under test, and do not suggest refactoring before the tests exist, except where a seam is unavoidable to isolate a side effect; if so, propose the smallest one and say why.
- Do not invent behaviour. If the code calls something you have not been shown, state your assumption about it and mark the test as dependent on that assumption.
- Do not claim the tests pass. They have not been run.
- Prefer a modest number of tests that cover every branch over many tests that repeat each other.
Output format:
Behaviour map (table: branch or condition | input that reaches it | predicted result)
Unreached branches
Test code (one code block)
Suspicious behaviour
What to confirm by running the testsExpected output
A behaviour map listing every branch with an input and a predicted result, test code with behaviour-based names and boundary cases, a list of unreachable or suspicious branches, and a clear statement that predicted values must be confirmed by running. For the late-fee example, it should cover each side of every date threshold and the cap, with time injected rather than read from the clock.
Behaviour map (excerpt)
booking returned on the due date | today == due_date | fee 0 (Derived from reading; confirm by running)
one day late, standard customer | today == due_date + 1 day | fee from line 14 (Derived from reading; confirm by running)
Test code (excerpt)
def test_no_fee_when_returned_on_due_date():
booking = make_booking(due_date=date(2026, 3, 10), customer_type="standard")
assert calculate_late_fee(booking, today=date(2026, 3, 10)) == 0
Suspicious behaviour
- Line 21 compares days late using > for standard customers and >= for members. This may be an off-by-one; the test pins the current behaviour.Limitations
- Predicted outputs come from reading the code and can be wrong. Run the tests and correct expectations before relying on them.
- Good characterisation needs inputs that reach every branch. The assistant can miss branches that depend on state it cannot see.
- Tests that pin buggy behaviour will fail when you fix the bug. That is intended, but the team needs to know.
- Real-world data may exercise behaviour that tidy synthetic inputs do not. Add anonymised cases from production incidents where you can.
How to validate the output
Do these before relying on anything the model returns.
- Written by
- PromptFable Editorial
- Last updated 13 Nov 2026
- Review
- Not yet reviewed
PromptFable is independent and is not affiliated with, endorsed by or sponsored by Anthropic, Google, OpenAI or any other vendor. Claude, Gemini, Google AI Studio and other product names are trademarks of their respective owners.