Using AI for Tests, Documentation and Refactoring Safely
Workflows for AI-generated tests, docs and refactors: characterization tests first, mutation testing to grade tests, and safe step-by-step refactoring.
Published November 27, 2026
Tests, documentation and refactoring are the chores that most often get postponed, and they are also where AI assistants deliver some of their most reliable value. The catch is that each has a specific failure mode: tests that assert nothing meaningful, docs that describe what the code should do rather than what it does, and refactors that subtly change behaviour. These workflows avoid all three.
The concept
For tests, the risk is false confidence. An AI can generate dozens of tests that pass and cover lines without checking anything important, or that encode existing bugs as expected behaviour. Coverage tells you which lines ran, not whether assertions are meaningful. Mutation testing (tools such as Stryker, PIT or mutmut) introduces small bugs into the code and checks whether tests catch them, which is an excellent way to evaluate AI-written tests.
For refactoring, the safety net comes first. Before asking an AI to restructure legacy code, have it write characterization tests that pin down current behaviour, including odd edge cases. Then refactor in small steps, running the tests after each. For documentation, generate from the code and tests rather than from intentions, and have a human verify the important claims.
The workflow
- Ask AI to write characterization tests for current behaviour
- Review: do assertions check outputs and side effects, not just "no exception"?
- Run mutation testing to find weak tests; ask AI to strengthen them
- Refactor in small steps with tests green after each
- Generate docs from the final code; human reviews key claims
Coverage vs mutation score
Where AI helps and what to check
| Task | AI is good at | Watch out for |
|---|---|---|
| Unit tests | Enumerating edge cases, boilerplate fixtures | Asserting current bugs, weak assertions |
| Characterization tests | Capturing existing behaviour quickly | Missing hidden side effects (DB, files, time) |
| Refactoring | Mechanical restructuring, renames, extracting functions | Subtle behaviour changes, performance regressions |
| Docs / docstrings | Explaining code, examples, READMEs | Describing intended rather than actual behaviour |
| Migration guides | Listing changed APIs from diffs | Missing non-obvious breaking changes |
Testing what matters
Ask for tests that describe behaviour from the caller's point of view: given inputs, expected outputs and side effects. Request property-based tests for pure functions (Hypothesis, fast-check) to explore inputs no one thought of. And tell the AI what not to do: no mocking of the unit under test, no snapshot tests of large objects unless asked, and no changes to production code while writing tests.
Practical examples
Prompt for characterization tests
Pin behaviour before changing anything.
"Write characterization tests for calculateShipping() in src/billing/shipping.ts.
Capture current behaviour exactly, including rounding and the free-shipping threshold,
even if it looks wrong. Do not modify shipping.ts. Use Vitest and table-driven cases."Property-based test (fast-check)
Properties catch edge cases example-based tests miss.
import fc from 'fast-check'
import { encode, decode } from './base62'
test('decode(encode(n)) === n for all safe integers', () => {
fc.assert(fc.property(fc.nat({ max: Number.MAX_SAFE_INTEGER }), n => decode(encode(n)) === n))
})Mutation testing to grade the tests
Stryker for JavaScript/TypeScript; similar tools exist for most languages.
npx stryker run --mutate "src/billing/**/*.ts"
# Mutation score: 81.4% (killed 114, survived 26)
# Then: "Here are the surviving mutants; add tests that would catch them."Common mistakes
- Trusting coverage numbers as proof that AI-written tests are good.
- Letting the AI change production code and tests in the same step.
- Refactoring without characterization tests on legacy code.
- Publishing generated docs without checking them against real behaviour.
- Large refactor commits that mix renames, moves and logic changes.
Key takeaways
- Write characterization tests before AI-assisted refactors.
- Use mutation testing to judge and improve AI-generated tests.
- Refactor in small, test-verified steps; keep logic changes separate.
- Generate docs from actual code and have humans verify key claims.
FAQ
Can AI write tests for code without tests?
Yes, and it is one of the best uses: characterization tests make legacy code safe to change. Review them for hidden side effects.
Should AI-generated tests be labelled?
Not necessarily. Hold them to the same review standard as any test. Some teams add a note in the PR description for reviewer context.
Is TDD compatible with AI tools?
Very much so. Write or agree the failing test first, then ask the AI to make it pass. The test becomes a precise specification.