## 1
1. The Pain Point First: Legacy Project Test Debt—Every Solo Developer Knows It
Anyone who has taken over a project that's been running for five years knows the feeling: thousands of functions, test coverage below 15%, and no confidence even changing a single line. Want to backfill tests? Just do the math:
- For a moderately complex function, hand-writing one test skeleton (mocking dependencies, constructing inputs, setting up assertion structure) takes an average of 8–15 minutes
- For an old module with 300 functions, filling in all the skeletons at 10 minutes each comes out to 50 hours—more than a full week
- And that's just skeletons, not counting edge cases and real assertions
So most solo developers and small teams choose: don't backfill. Then pray every time they touch the code.
What actually pushed me to take action was a bug I introduced last month—an old utility function nobody remembered crashed the moment it received null input. The fix took 10 minutes, but debugging plus regression testing took a whole evening. Test debt doesn't go unpaid—you just pay it back in a more expensive way.
2. What AI Can Do Here: Its Strength Is "Mechanical and Repetitive"
Writing test skeletons happens to be right in AI's sweet spot:
- Reading code and understanding dependencies: Give it a function's source code, and it can identify which parameters need mocking and which are pure functions that don't.
- Batch-generating structure: Normal paths, null values, boundary values—it generates skeletons for 3–5 test cases at once, leaving TODO markers for the assertion parts, with humans filling in the critical logic afterward.
- Maintaining consistent style: Give it one or two existing tests as examples, and the skeletons it generates match your project's existing test style—unlike hand-written ones where every person has their own flavor.
- Free code review along the way: While reading functions, the AI will point out suspicious spots—like missing null checks, magic numbers, dead code. In this run, it flagged 4 potential null-crash points in a 40-function module.
To be clear: what AI generates is a skeleton, not a deliverable, complete test. The specific expected values for assertions and business boundaries still require human confirmation. But even so, it cut the processing time per function from 10 minutes down to around 2 minutes (30 seconds to generate + 90 seconds for human review and edits)—that's the number that matters.
3. My Actual Workflow
Step 1: Prepare materials. Pick out the list of target functions, group them by module; prepare the project's test framework info (e.g., pytest / JUnit / Jest) and one existing test file as a style example.
Step 2: Feed in batches. I give it 10–20 functions at a time; beyond that, the AI's output quality degrades. Use this template:
你是一名资深测试工程师。请为以下函数生成单元测试骨架。
【测试框架】:{{pytest / JUnit5 / Jest,按实际填写}}
【运行环境】:Python 3.11 / JDK 17 / Node 20
【风格范例】:
{{粘贴一个项目里已有的测试文件,200行以内}}
【待测函数】:
{{粘贴函数源码,含类型签名}}
要求:
1. 覆盖正常路径、空值/边界值、异常路径三类场景,每个场景一个测试函数
2. 需要外部依赖(数据库/网络/文件)时使用 mock,说明 mock 的对象
3. 断言处用 assert xxx == TODO_EXPECTED 占位,并加注释说明应填什么
4. 不确定的业务含义,用 # QUESTION: 开头的注释标出,不要猜
5. 只输出测试代码,不要解释Item 4 is crucial—make the AI admit what it doesn't know, instead of making things up. Early on, before I added this, it would produce completely wrong expected values, which actually increased debugging costs.
Step 3: Human wrap-up. Run the generated tests, fix any skeletons that fail, fill in the key assertions. Look up documentation to confirm everything marked with QUESTION.
Step 4: Hook into CI. For modules with completed skeletons, write coverage thresholds into the pipeline to prevent new debt.
4. Before and After: A Time Accounting
Using an old module with 300 functions as an example:
| Item | Pure Hand-Writing | AI-Assisted |
|---|---|---|
| Average time per function | ~10 min | ~2 min |
| Total time for 300 functions | ~50 hours | ~10 hours |
| Actual calendar duration | 2–3 weeks (no one can write continuously) | 2–3 days |
| Byproducts | None | 4 potential bug warnings, a batch of dead-code flags |
Going from 50 hours down to 10 hours—saving roughly 80% of the time—and the psychological difference is total: after hand-writing tests for the 20th function, you're numb; AI doesn't get numb. On quality, my spot checks put the skeleton usability rate at around 85%, with the remaining 15% mainly being complex mocking scenarios that need manual adjustment.
On cost, the token fees for this volume of calls are negligible compared to labor costs—check each model's official pricing page for specifics. If you go through a multi-model gateway, skeleton generation is the kind of task where a cheap mid-tier model is plenty; there's no need to use a flagship model—another expense you can save on.
5. A Few Lessons Learned
- Don't let the AI generate all tests at once—feeding in batches gives more stable quality.
- Always provide a style example, otherwise the generated tests will clash with your project's conventions and become painful to maintain later.
- Skeleton ≠ done. Treat AI output as a "70% draft"; the human 30% review cannot be skipped.
- Prioritize core functions that are called frequently—rank by call frequency rather than scanning from the top of the file to the bottom.
There's no magic one-click fix for legacy project test debt, but AI cut the cost of paying it down by 80%—what used to be "no time to do at all" is now "something you can push forward a chunk of in one afternoon." This is a calculation worth making for anyone maintaining an old project.
If you don't have a usable model account yet, you can register one here to get started: https://api.thistoken.ai/register
---
Ready to try it yourself? Sign up at https://api.thistoken.ai/register to get your API key and start building.
Хотите попробовать Token.AI?
Создайте API Key уровня проекта, включите каналы в консоли и настройте маршрутизацию, бюджеты и журналы аудита.
注册 ThisToken.AI 并获取 API Key