Write missing unit tests for legacy code

Last updated

DifficultyMedium
SetupSome code
Best forDeveloper
ToolsClaude Code, OpenAI Codex CLI, Cursor, Aider

Copy this prompt

Paste this into a general agent such as ChatGPT agent or Claude, then add your own inputs.

Help me run this UseCaseProof playbook: Write missing unit tests for legacy code.

The problem: Untested legacy code makes every change risky.

How the agent should work: The agent reads a module, writes characterization tests that capture current behavior, runs them, and iterates until they pass, then reports coverage gains.

Follow these steps:
1. Pick one module at a time
2. Ask for tests of current behavior, not desired behavior
3. Run tests in the loop
4. Review for meaningless assertions
5. Commit in small PRs

Human-approval checkpoint: Developer reviews tests.

Guardrails: Agents may write tests that assert bugs; label them as characterization tests.

Suggested tools from this playbook (not ranked; check current features and pricing): Claude Code, OpenAI Codex CLI, Cursor, Aider.

Do not send messages, move money, grant access, merge code, or take any irreversible action until I approve. If you need an input I have not provided, ask me instead of guessing.

The problem

Untested legacy code makes every change risky.

How the agent works

The agent reads a module, writes characterization tests that capture current behavior, runs them, and iterates until they pass, then reports coverage gains.

Setup steps

  1. Pick one module at a time
  2. Ask for tests of current behavior, not desired behavior
  3. Run tests in the loop
  4. Review for meaningless assertions
  5. Commit in small PRs

Tools that can do this

  • Claude Code
  • OpenAI Codex CLI
  • Cursor
  • Aider

Some tool links may be affiliate links. We only list tools that fit the playbook; recommendations are not sold. Disclosure.

Not ranked. Tools marked Featured pay for a highlighted listing; that never decides which tools we include. Check each vendor's current features and pricing.

Make one of these tools? Feature your tool on every playbook where it is already listed.

Human in the loop

Developer reviews tests.

Risks and guardrails

Agents may write tests that assert bugs; label them as characterization tests.

ROI notes

Makes refactors safer.

Quick answers

How do I write missing unit tests for legacy code?

The agent reads a module, writes characterization tests that capture current behavior, runs them, and iterates until they pass, then reports coverage gains. The full setup is 5 steps.

Do I need to code to set this up?

Some code is needed. We rate the setup Medium.

Which tools can do this?

Claude Code, OpenAI Codex CLI, Cursor, Aider. Not ranked; check each vendor's current features and pricing.

Where does a human stay in control?

Developer reviews tests.

What's a starter prompt I can paste into ChatGPT agent or Claude?

A playbook-specific starter prompt is in the Copy this prompt section above. It is written from this playbook's steps and human-approval checkpoint, for a general agent such as ChatGPT agent or Claude.

Also useful: triage failing CI runs and flaky tests; investigate failures in production logs; run browser QA checks on core user flows; improve docs through agent task failures.

All AI agent examples, by function · More Software Engineering playbooks