テストのない既存コードに安全網テストを追加(特性テスト自動生成・Python/JS)
Legacy code test harness: characterization tests for untested Python and JS code
テストのない Python・JavaScript・TypeScript のコードで危険度の高い関数を順位づけし、実際の入出力を記録して現在の動作を固定するテスト(pytest・jest・vitest)を作ります。リファクタリング前の安全網づくりに。
できること
テストがほとんどない既存のコードを安全に直せるように、「今の動作」をそのまま記録して固定するテスト(特性テスト、ゴールデンマスター)を追加します。
- 関数の一覧と危険度の順位づけ(
inventory.py): 関数・メソッドごとに行数、分岐の多さ(Python は構文解析、JS/TS は簡易解析)、他のファイルからの呼び出し回数、テストでの言及の有無、git の変更頻度を集計し、テストを先に書くべき順に並べます。ファイル操作・通信・DB・時刻・乱数への依存も印付けします - 既存のテスト環境の検出: pytest・unittest・jest・vitest・mocha、設定ファイル、TypeScript、カバレッジツール、実行コマンドを調べます
- 実際の入出力の記録(
record_calls.py、record-calls.cjs): いつもどおりプログラムやテストを動かしている間に、対象の関数への引数、戻り値や例外、引数やオブジェクトの状態の変化を JSON に保存します(Python と CommonJS の Node.js) - テストの自動生成(
gen_characterization.py): 記録した入出力をそのまま使う pytest・jest・vitest のテストを作ります。記録がない関数には、引数名から推測した仮の入力を「要置き換え」と明記して入れます。メソッド、非同期関数、ESM と CommonJS、TypeScript に対応。既存ファイルは上書きしません - カバレッジの穴の一覧(
coverage_gap.py): coverage.py・Cobertura・LCOV・Istanbul の結果と組み合わせ、「危険度が高いのに実行されていない関数」を順に示します - 手順書とテンプレート: リファクタリング前に現在の動作を記録する手順、時刻・乱数・環境変数・通信を固定する方法(pytest・jest・vitest)、手書き用のテスト例、ユーザー向け報告書のひな形、3種類のサンプルと実行例
使い方
python scripts/inventory.py . --json inventory.json --md inventory.md
python scripts/gen_characterization.py --inventory inventory.json --top 5
python scripts/record_calls.py --root . --target shop.pricing:calc_total --out tests/characterization/cases -- main.py
エージェントは変更予定の範囲と危険度から対象を選び、記録と手書きのケースをそろえ、テストを2回実行して安定していることを確かめます。わざと小さな変更を入れてテストが失敗することも確認し(確認後は元に戻します)、残っている穴と、見つかった「バグかもしれない現在の動作」を報告します。Claude、Codex、Hermes Agent、Cursor で使えます。
必要な環境
- スクリプトは Python 3(標準ライブラリのみ)。変更頻度の集計には git(任意)。JavaScript の入出力の記録には Node.js
- 生成したテストの実行には、プロジェクトのテストツール(pytest、jest、vitest)が必要です。カバレッジには pytest-cov などが必要です
注意点
- JS/TS の解析と危険度の点数は推定です。仮の入力はあくまで推測なので、実際の値に置き換えてください
- 記録中はプログラムが実際に動きます(ファイル書き込みや通信も起こります)。テスト環境で実行し、記録した JSON に個人情報が含まれていないか確認してください
- ESM 形式の JavaScript は自動記録できないため、ケースを手書きします
- 見つかったバグは勝手に直さず、報告して判断を仰ぎます
English description
Ranks risky functions in untested Python, JavaScript or TypeScript code, records real inputs and outputs, and generates golden master / snapshot tests for pytest, jest or vitest before a refactor.
What it does
Makes untested legacy code safe to change by recording what it does today and pinning that behavior with characterization (golden master) tests.
- Function inventory and risk ranking (
inventory.py): per function and method it measures size, branching complexity (Python AST; a heuristic scanner for JS/TS), fan-in from other files, whether tests mention it, and git churn, then ranks what to test first. It also flags dependencies on files, network, databases, the clock and randomness - Test tooling detection: pytest, unittest, jest, vitest, mocha, config files, TypeScript, coverage tools and the commands to run them
- Record real inputs and outputs (
record_calls.py,record-calls.cjs): while you run the program or its existing tests as usual, it saves arguments, return values or exceptions, and changes to arguments and object state as JSON (Python and CommonJS Node.js) - Test generation (
gen_characterization.py): pytest, jest or vitest tests that replay the recorded cases. Functions without recordings get placeholder inputs guessed from parameter names, clearly marked for replacement. Handles methods, async functions, ESM and CommonJS, and TypeScript. Existing files are never overwritten - Coverage gaps (
coverage_gap.py): combines coverage.py, Cobertura, LCOV or Istanbul results with the ranking to list risky functions that never run - Guides and templates: a step-by-step procedure for capturing behavior before refactoring, how to freeze time, randomness, environment and network in pytest, jest and vitest, hand-written test examples, a report template, and three sample projects with a worked example
How to use
python scripts/inventory.py . --json inventory.json --md inventory.md
python scripts/gen_characterization.py --inventory inventory.json --top 5
python scripts/record_calls.py --root . --target shop.pricing:calc_total --out tests/characterization/cases -- main.py
The agent picks targets from the planned change and the ranking, assembles recorded and hand-written cases, runs the tests twice to confirm they are stable, makes a small deliberate change to confirm the tests catch it (then reverts it), and reports remaining gaps and any suspicious current behavior. Works with Claude, Codex, Hermes Agent and Cursor.
Requirements
- Python 3 for the scripts (standard library only); git for churn (optional); Node.js for recording JavaScript calls
- The generated tests need the project's test runner (pytest, jest or vitest). Coverage needs pytest-cov or the runner's coverage option
Caveats
- JS/TS parsing and the risk score are heuristics. Placeholder inputs are guesses and must be replaced with real values
- Recording runs the program for real (file writes, network calls). Use a test environment and check recorded JSON for personal data
- ESM JavaScript cannot be recorded automatically; write those cases by hand
- Bugs found along the way are reported, not silently fixed
インストール方法
- ダウンロードしたZIPを展開します(中に
legacy-code-test-harness/フォルダとSKILL.mdがあります)。 - フォルダごと、使うエージェントのスキル置き場にコピーします。
- Claude:
~/.claude/skills/(プロジェクト単位なら.claude/skills/) - Hermes Agent:
~/.hermes/skills/ - Codex:
~/.agents/skills//Cursor:~/.cursor/skills/ - Claude アプリ(claude.ai):「カスタマイズ → スキル」からZIPをアップロード
- Claude:
- エージェントを再起動し、スキル一覧に表示されることを確認します。
出品者
Skills that make everyday developer work faster and safer: Git, testing, APIs and security.
出品スキル 8 件