Shrinks test suites by combining fact-matrix analysis with per-test branch coverage, ensuring no asserted fact or covered branch is lost. For Python/pytest suites with pytest-cov.
Ships in test-sloc-cut. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them; scripts run unread.
A repeatable way to shrink a test suite while keeping every asserted fact and every covered branch. Developed on a billing feature spread over two stacked branches: four test files, 61 tests, 757 code lines went to three files, 46 tests, 608 code lines, and the per-module branch coverage measured after the cut was identical to the map measured before it.
The method rests on one rule and two oracles.
The rule. A test, or a line of a test, may go only if every fact it asserts is still asserted by a surviving test on the same production path, and the production modules lose no covered line or arc — compared item by item, not by count.
The two oracles. Neither is sufficient alone.
| Oracle | Answers | Blind to |
|---|---|---|
| Fact matrix (read the code) | which propositions each test pins; which are duplicated, which are pinned once | branches nobody thought to name |
| Per-test branch coverage (run the code) | which lines and arcs each test uniquely reaches | assertion strength: two tests can walk the same lines and assert different values; outcomes inside a line — a zero-iteration loop, the operand that decides an and/or, a ternary arm, a comprehension filter — which the script lists per test |
"Coverage-redundant" and "assertion-redundant" are different properties. A test with an empty unique-coverage set may still be the only one that fails when a value is wrong. Delete on the intersection of both oracles, never on one.
Use it when a suite has grown by accretion: several review passes, tests added per bug, three files that each provision the same object their own way. Signs: the same value asserted in a pure test, a DB test and an end-to-end test; tests whose names differ but whose bodies differ by one argument; comments that narrate the assertions below them.
Do not use it to reach a number. Every cut is justified by a fact that survives elsewhere or a measured coverage identity. If a proposed cut cannot show either, it is not a simplification — it is a coverage loss with a smaller diff.
The method keeps every fact, including a worthless one: an expected value computed by the code under test, a copied inventory, a mock that supplies the answer its test asserts. Whether a fact is worth pinning at all is a value decision with its own evidence — use the test-audit skill for it, and bring the tests it keeps back here to consolidate.
Pin the tree at one commit and record three numbers per file: total lines, code lines (non-blank, non-comment; docstrings count as code because a reader has to read them), test count. Count with the tokenizer, not grep -v '^#', so multi-line strings and trailing comments are classified correctly:
python3 scripts/count_code_lines.py tests/test_a.py tests/test_b.py
# 26 total 14 code 1 comment-only 5 tests tests/test_a.py
Record which tests pass: a test that fails on the baseline is a possible product bug to report, not a cut candidate.
If the files live on stacked branches, write down which branch owns which file. A helper added on the base branch is visible to the dependents; the reverse is not true, and the cut has to be committed base-first.
Spawn four agents at once, each writing a markdown report to a scratch directory outside the repo. Three are read-only; exactly one may run tests, because a shared test database does not tolerate concurrent runs. Keep the prompts lean: file list, production modules, the deliverable's shape, the constraints. The full brief for each is in references/investigation.md.
Fact matrix. One row per test: file, name, the production function or branch it drives, and the facts it asserts as short propositions with stable ids (F12: an override for an unpublished category creates no entry). Then: facts already owned outside the files under review (look one layer down — a parity test there may already pin by exact comparison everything these files re-assert); cross-file duplicates with one owner per fact, where the cheapest layer that can prove the fact wins (pure over DB, DB over end-to-end) except facts about wiring, persistence, metric emission or legacy behaviour, which only the layer that exercises them can prove; strict subsets, proven against the mutants each would kill, not by name; single-pinned facts — the do-not-touch set; fold proposals with honest line estimates. Record the matrix with scripts/fact_matrix.py: seed lists every assertion site with an exact anchor, the agent maps each to its facts, and check refuses unmapped sites, orphan facts, open questions and drifted anchors, and computes the single-pinned and duplicate tables instead of taking them on trust.
Helper and arrangement economics. Inventory every helper with call-site count and lines saved versus cost. A parametrized block costs N + 7 lines against 2–3 per replaced test, so break-even is seven two-line tests — and every row must be width-checked at its real indent; rows that need shortening devices pay for those too. Refuse line-neutral parametrization: it deletes the descriptive names. Check what the constructor already accepts before proposing a builder; keep shared helpers local below ten lines net; adopt a width alias everywhere in a file or nowhere.
Comment audit (prose-heavy files). Every comment block passes or fails the three-clause test — true at this layer, not visible in the code in front of the reader, not stated better nearby — with a verdict of keep, trim (with replacement text) or delete. A comment the code contradicts is never a keep; the fix is often to import the production constant the literal was. The comment-slop skill is the method for this angle.
Per-test branch coverage (the runner). Run only the files under review, serially, with per-test contexts; compute each test's unique contribution (its pytest setup, call and teardown merged, so coverage a fixture supplies stays with its test) and a greedy minimum covering set, and verify the set by running only those tests and diffing the result against the baseline item by item. Commands, the script, and the nine traps that produce a wrong map are in references/coverage-and-mutants.md.
Leave-one-out is not a licence. If exactly two tests reach a line, neither is unique, yet dropping both loses the line. The covering set is the floor, not a target: in the worked example 11 of 61 tests reproduced all 514 covered items, and the other 50 had to justify themselves on assertion strength.
Read all four reports before touching a file. Dedupe findings that name the same test or mechanism, then decide each with this table.
| Candidate | Take when | Refuse when |
|---|---|---|
| delete a test | the fact matrix shows a strict subset on the same path and coverage shows no unique arcs | the coverage runner lists it in the covering set, any fact is single-pinned, or it reaches a sub-line outcome (the map's subline) that no surviving test pins and no mutant proves |
| fold N tests into one | they share a fixture and the merged test asserts every fact of each; a shared patch (a distinct sentinel value) makes the merged assertions stronger | the merge collapses two distinct input paths into one row label |
| move a fact to a cheaper layer | the fact is about a pure function but was only pinned through a DB or end-to-end test | the fact is about wiring, persistence, metric emission, or legacy behaviour |
| parametrize | net lines saved after counting shortening devices, and ids carry what the names carried | line-neutral, or the rows exceed the width limit |
| shared helper | at least 10 lines net and no house convention against it | below that, or it needs a wrapper per file anyway |
| alias for width | ten or more wrapped calls; apply everywhere in the file | fewer than about four sites |
| trim a comment | a clause narrates the assertions below it or restates a sibling file | the clause is the only statement of a why |
Two rules override the table:
mutant field of the after-the-cut matrix; fact_matrix.py compare refuses a fact that moved to other tests without one.Multi-value assertions can be one tuple comparison (assert (standard, downsampled) == (365, 90)): pytest prints both tuples on failure and the names sit on one line.
$files variable is not word-split, so a linter receives one bogus path and passes having checked nothing — three over-width lines reached a pushed tip that way. After the linter, read git diff --stat to see what hooks rewrote.python3 scripts/coverage_map.py --diff baseline.json after.json. Equal counts are not enough — a cut can lose one arc and gain another. Exit 0 means nothing was lost; exit 1 lists each lost line or arc — find the deleted test that owned it in the baseline's per-test table and restore its assertion somewhere; exit 2 means production or tooling changed between the runs, so re-measure the baseline. Then re-seed the fact matrix from the baseline (fact_matrix.py seed … --out facts-after.json --from facts.json keeps the mapping of every unchanged assertion), map the new and moved sites, and run fact_matrix.py compare facts.json facts-after.json: exit 1 names each fact that was lost, weakened to a weaker kind of check, or moved without a recorded mutant.| before | after | |
|---|---|---|
| tests | ||
| code lines | ||
| total lines | ||
| branch coverage per module | baseline | identical / differences |
Then, per branch: which tests were deleted and which test now owns each of their facts; which facts moved to a cheaper layer and the mutant that proved them; which comments were trimmed and which false claims fixed; what was refused on the reports' own advice and why. Name the process defects found on the way. The condensed worked example — what the oracles said, what was done, what was refused — is references/worked-example.md.
scripts/count_code_lines.py — the tokenizer line counter (stdlib).scripts/fact_matrix.py — seed the assertion sites of the files under review, check a filled matrix (with --coverage, that some test asserting each fact runs its production lines), and compare the baseline and after-the-cut matrices (stdlib).scripts/coverage_map.py — per-test unique coverage, the covering set, the sub-line sites coverage cannot see, and --diff to compare two runs item by item, from a serial per-test-context run (needs coverage importable in the test environment)..coveragerc, the pytest command, reading the map, the nine traps, verifying the covering set, the identity diff after the cut, and the mutant check for a relocated fact.Related skills: test-audit for whether a test earns its place at all; comment-slop for the comment audit; pytest for the runner's mechanics.
What SKILL.md sets; muted values are Claude Code's defaults
/test-sloc-cut:test-sloc-cutfrom test-sloc-cut
npx claudepluginhub dashed/claude-marketplace --plugin test-sloc-cutOther plugins' skills for the same job