How to Use AskCodi to Write Unit Tests for Legacy Code
Legacy code is hard to test because nobody knows what it is supposed to do. AskCodi will generate tests for any function you select, but it writes tests for what the code does now, bugs included.
That is actually the right starting point. Characterisation tests lock current behaviour first, then you change the code and the tests tell you what broke.
What is the first step before generating a single test?
Get the function running in isolation. If a 60-line method talks to the database, a payment API and three static helpers, generated tests will mock the wrong thing or skip the assertions.
Before you select anything:
- Move external calls behind an interface or a thin wrapper. Forty minutes of extraction saves hours of broken tests.
- Confirm the test runner works. Run one trivial test in the module first.
- Check the coverage baseline with your existing tool and write the number down. You will compare against it later.
How do you prompt AskCodi for characterisation tests?
Select the function and use the Generate Tests action. Then refine with a specific instruction in chat:
“Write Jest tests that assert the current output for these inputs. Do not suggest fixes. Mock the fetch call and the logger.”
Three parts to that instruction, all necessary:
- Framework — Jest, pytest, JUnit, whatever your repo uses. Otherwise you get the default.
- No fixes — the model loves to improve code. For characterisation you want the bug preserved.
- Mocks named — tell it exactly which dependencies to stub.
Why do generated tests fail on the first run?
Mostly imports and mocking style. AskCodi writes plausible tests that call a module path based on the file layout it sees, which may differ from your test config.
Common fixes:
- Wrong import path — align with your
conftest.pyorjest.configmodule resolution. - Mocking the wrong level — patch where it is used, not where it is defined.
- Async handling — Python tests need
async defwith a runner, JS needsawaiton the mock. - Test data assumptions — hardcoded dates fail on day boundaries.
Budget two to three rounds of fixing per file. The generator gets you 70% there; the last 30% is you.
How much coverage should you actually aim for?
Not 100%. For characterisation work, target the functions that are both high-traffic and hard to reason about.
A realistic order:
- Functions on the request path that handle money or permissions first.
- Utility functions with branches, second.
- Data mappers next.
- Pure formatting helpers last, or never.
Raising a legacy module from 12% to 55% coverage typically catches the majority of regressions. Chasing the last 40% costs far more than it saves.
What does this workflow not solve?
Testing code you cannot instantiate. Classes with a constructor that opens a socket need refactoring before any test tool helps.
Timing-dependent code. Sleeps, cron boundaries and race conditions produce flaky generated tests. Mark them skipped and handle separately.
And it does not find the bug you are afraid of. If the current behaviour is wrong, characterisation tests lock the wrong behaviour in. You need a human who knows the domain to say “this test should fail”.
For changes that span many files rather than many tests, Aider in architect mode plans the refactor across the repo and commits each step, which pairs well with a freshly written test suite.
Quick Summary
- Extract external dependencies before generating tests, or the tests will be brittle.
- Ask for characterisation tests explicitly: name the framework, forbid fixes, name the mocks.
- Expect two to three rounds of fixing imports and mocks per file.
- Target high-traffic, branch-heavy functions. 12% to 55% coverage catches most regressions.
- Characterisation tests preserve existing behaviour, bugs included. Domain review is still your job.
