Example: Add tests to existing code
Prompt examples are illustrative; commands are verified against official docs.
Scenario: a legacy module (src/reports/) ships without tests. Goal: discover the project’s test setup, generate tests that match existing conventions, run them, and iterate until green.
Workflow
- Discover the test framework, conventions, and how to run tests.
- Generate tests for one unit at a time, following existing test style.
- Run the tests; iterate on failures.
- Widen coverage incrementally.
Claude Code
claude
Discover the setup:
Figure out how this project runs tests: framework, config files, existing
test layout, and the exact command to run a single test file.
Generate tests that match house style:
Look at an existing well-tested module for style, then write tests for
src/reports/summary.ts covering the happy path and edge cases (empty input,
missing fields, date boundaries). Put them where this repo's tests live.
Run and iterate:
Run the new tests, fix any failures, and keep iterating until the file
passes. Then run the full suite to check for regressions.
Pre-approve just the test command so runs don’t prompt every time (permission rule syntax):
claude --allowedTools "Bash(npm test *)"
For unattended generation, print mode works too (no interactive session):
claude -p "Write unit tests for src/reports/summary.ts following the conventions in tests/, run them, and fix failures." --max-turns 30
Codex
Interactive, with the workspace-write sandbox so tests can run freely:
codex --sandbox workspace-write --ask-for-approval on-request
Inspect the repo's test setup: framework, config, directory conventions, and
the command to run one test file.
Following the style of the best existing test file, write unit tests for
src/reports/summary.ts (happy path + edge cases), run them, and fix
failures until green.
Scripted/CI variant — codex exec runs non-interactively (streams progress to stderr, prints the final message to stdout):
codex exec --sandbox workspace-write "Add unit tests for src/reports/summary.ts matching the conventions in existing tests. Run them and iterate until they pass."
Note: codex exec requires a Git repo by default (override with --skip-git-repo-check), and --full-auto is deprecated — use --sandbox workspace-write.
Kimi Code
kimi
Discover and generate:
Find how tests are organized and run in this repo (framework, config, test
file locations, single-file run command).
Write unit tests for src/reports/summary.ts following the conventions of the
existing test files: happy path plus edge cases for empty input, missing
fields, and date boundaries.
Run and iterate. Switch to Ask When Needed so repeated test runs don’t each prompt:
/yolo
Run the new tests and iterate until they pass, then run the whole suite to
catch regressions.
One-shot, fully non-interactive (kimi -p streams output to stdout and runs with auto permission — use only in trusted directories):
kimi -p "Add unit tests for src/reports/summary.ts matching existing test conventions, run them, and fix failures until green."
Related
- Claude Code workflows: Claude Code — Workflows
- Codex workflows: Codex CLI workflows
- Kimi Code workflows: Workflows
- Quick-reference tables: All Tools Cheat Sheet
Last verified: 2026-09-07
AI coding tools evolve quickly. If you find outdated information, please open an issue or pull request.