sisi 0.1.0: a shared rulebook and verification tool for AI coding agents
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
ci / tests (macos-latest, python 3.11) (push) Canceled after 0s
ci / tests (macos-latest, python 3.13) (push) Canceled after 0s
ci / tests (macos-latest, python 3.9) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.11) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.13) (push) Canceled after 0s
ci / tests (ubuntu-latest, python 3.9) (push) Canceled after 0s
Contracts with a ladder of checks, an end-of-turn gate, retrospectives and shared lessons, time-to-delivery measurement, a library of 140 verification techniques and 20 recipes, personal data kept outside the clone, and releases with rollback. Standard library only. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
commit
2fc1fe9c2d
223 files changed
+22163
No files matched your search
@@ -0,0 +1,53 @@
|
||||
---
|
||||
id: assertion-strength-review
|
||||
title: Review that tests assert on outcomes, not just that code ran
|
||||
answers: Do these tests check the result that matters, or would they pass for wrong behaviour too?
|
||||
rung: 5
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, money, parsing, state-machine
|
||||
cost: low
|
||||
detect:
|
||||
ran_here: no
|
||||
verified: docs
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You wrote or were handed tests and want to know they would fail on wrong output.
|
||||
- A suite has high coverage and passes but a bug shipped.
|
||||
- Before reporting done: a last read of your own new tests, with the checklist below.
|
||||
|
||||
## Do not use when
|
||||
- A mutation tool is available and fast enough: it measures this mechanically. This review is judgement (rung 5) and is for cases the tool cannot reach or as a quick first pass.
|
||||
|
||||
## Set up
|
||||
No tool. Read each new test and ask the questions below. A second agent or reviewer, given only the tests and the spec (not your reasoning), makes the review less biased. Cheap mechanical aids: `grep` for the smells; run mutation testing on the changed file (`mutation-on-diff`).
|
||||
|
||||
## Minimal example
|
||||
Smells and what to do instead:
|
||||
|
||||
| Smell | Why it is weak | Instead |
|
||||
|---|---|---|
|
||||
| `assert!(result.is_ok())` | passes for any success value | compare the value: `assert_eq!(result.unwrap(), expected)` |
|
||||
| Test calls the function and asserts nothing, or "does not panic" | passes if the output is garbage | assert the output, the state change or the error variant |
|
||||
| Snapshot of the whole output, accepted blindly | records the current behaviour, right or wrong | review the snapshot line by line against the spec; assert key fields too |
|
||||
| Assertion computed with the same code under test | tests the code against itself | use a hand-calculated constant or an independent oracle |
|
||||
| `assert!(x > 0)` where an exact value is known | allows many wrong values | assert the exact value |
|
||||
| Mock returns what the test expects; test checks the mock was called | tests the mock | assert the observable effect |
|
||||
| Assertion inside a loop over an empty list, or in a callback that may not fire | vacuous | assert the count first; see `vacuity-check` |
|
||||
| Only the happy path | misses boundaries | add the boundary, the empty input, the error case |
|
||||
|
||||
## Run
|
||||
For each new test, write down in one line what wrong implementation it would catch. If you cannot name one, the test is weak. Then check it mechanically: temporarily change the code (flip a comparison, return a constant) and run the test; it must fail.
|
||||
|
||||
Passing looks like: every test has a named wrong behaviour it rejects, and the temporary breakage turns it red. Failure: a test stays green under a plausible bug.
|
||||
|
||||
## As a contract check
|
||||
{"id": "assertion-review", "claim": "Each new test fails when the behaviour it covers is broken", "kind": "general", "rung": 5, "cmd": "cargo mutants --in-diff /tmp/change.diff", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 1800}
|
||||
|
||||
The review itself is judged; the mechanical stand-in is mutation testing of the diff, with the same limits as in `mutation-on-diff`.
|
||||
|
||||
## What a pass does not prove
|
||||
- A judged review is only as good as the reviewer's reading of the spec. It can miss subtle gaps.
|
||||
- Strong assertions on the wrong expected value are still wrong. Derive expected values from the specification, not from the output you just got.
|
||||
- It does not show the tests were run. See `vacuity-check`.
|
||||
@@ -0,0 +1,58 @@
|
||||
---
|
||||
id: cargo-mutants
|
||||
title: Mutation testing for Rust with cargo-mutants
|
||||
answers: Would my tests notice if this Rust code were wrong?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: rust
|
||||
kinds: general, money, parsing, state-machine
|
||||
cost: medium
|
||||
detect: cargo mutants --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You wrote tests for new or changed logic and want evidence they can fail, not just that they pass.
|
||||
- The code is money, parsing, validation or a state machine where a flipped comparison is costly.
|
||||
- Coverage is high but you doubt the assertions. Mutation testing is a check on your other checks: it adds confidence in the tests, which is why it sits at rung 2.
|
||||
|
||||
## Do not use when
|
||||
- You only touched a few lines and a full run is too slow; use `mutation-on-diff`.
|
||||
- The tests are slow end-to-end tests; each mutant reruns them. Narrow with `--test-package`.
|
||||
- The code has no tests yet. Everything survives.
|
||||
|
||||
## Set up
|
||||
cargo install cargo-mutants # or: cargo binstall cargo-mutants
|
||||
cargo mutants --version
|
||||
|
||||
Optional `.cargo/mutants.toml` holds `exclude_globs`, `exclude_re`, `timeout_multiplier`. To mark a function as not worth mutating, add the `mutants` crate as a normal dependency and use `#[mutants::skip]` (source: mutants.rs docs).
|
||||
|
||||
## Minimal example
|
||||
`src/lib.rs`, with a boundary the tests miss:
|
||||
|
||||
pub fn price(qty: u32, unit: u32) -> u32 {
|
||||
let total = qty * unit;
|
||||
if qty >= 10 { total - total / 10 } else { total }
|
||||
}
|
||||
#[cfg(test)] mod tests { use super::*;
|
||||
#[test] fn small() { assert_eq!(price(2, 100), 200); }
|
||||
#[test] fn big() { assert_eq!(price(20, 100), 1800); } }
|
||||
|
||||
## Run
|
||||
cargo mutants -p mycrate --timeout-multiplier 3 --minimum-test-timeout 60 -j 2
|
||||
|
||||
Passing looks like: `N mutants tested ...: M caught` with no `MISSED` lines, exit 0. Failure shows `MISSED src/lib.rs:4:12: replace >= with > in price` (here: the `qty >= 10` boundary, caught only after adding `assert_eq!(price(10, 100), 900)`), exit 2. Missed mutants are in `mutants.out/missed.txt`, timeouts in `timeout.txt`, unviable (does not compile) in `unviable.txt`.
|
||||
|
||||
Reading survivors: for each, decide kill or equivalent. Kill it with a test that fails under the mutant. A mutant is equivalent when the changed code cannot alter observable behaviour; record the reason and exclude it with `#[mutants::skip]` or `-E <regex>`. Never delete a test or loosen an assertion to make it pass.
|
||||
|
||||
Long runs: `--shard k/n` splits work; `--iterate` reuses `mutants.out` and skips mutants already caught or unviable. Keep `mutants.out` between interrupted runs; the per-mutant build copy is large, so point `TMPDIR` at a disk with space. Use `--list` to preview the mutants without running.
|
||||
|
||||
## As a contract check
|
||||
{"id": "mutants-core", "claim": "Every viable mutant of the pricing module is caught by tests", "kind": "general", "rung": 2, "cmd": "cargo mutants -p mycrate --file src/pricing.rs --timeout-multiplier 3", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 3600}
|
||||
|
||||
## What a pass does not prove
|
||||
- Caught mutants show the tests react to those small edits only. They do not show the specification is right.
|
||||
- Equivalent mutants and `skip`ped code are not checked. List each exclusion and why.
|
||||
- A timeout counts as caught by default but may mean a hang from a weak test; read `timeout.txt`.
|
||||
- If the baseline run (unmutated tests) fails, nothing is tested; the exit is non-zero. Do not read it as survivors.
|
||||
@@ -0,0 +1,53 @@
|
||||
---
|
||||
id: check-the-checker
|
||||
title: Plant a known bug to confirm the verification pipeline catches it
|
||||
answers: If the code were wrong, would my whole verification pipeline actually report it?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, concurrency, money, parsing
|
||||
cost: low
|
||||
detect: git --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You wrote a new gate, contract check, CI job or script and want to know it can fail.
|
||||
- A gate has passed for a long time and you have never seen it red.
|
||||
- You changed the toolchain, a flag or an environment, and the pass might now be vacuous.
|
||||
|
||||
This is a check on another check; mutation testing is the automated form of it for unit tests. A planted bug (a canary) proves one specific path through the pipeline, so it adds rung 2 confidence in that gate.
|
||||
|
||||
## Do not use when
|
||||
- You can run a mutation tool over the area: it plants many bugs and reads the result for you.
|
||||
- The pipeline touches shared or production systems. Plant the bug only in a local copy.
|
||||
|
||||
## Set up
|
||||
Work in a throwaway branch, worktree or copy so the bug cannot be committed. Choose a canary that the gate is meant to catch: flip a comparison, return a constant, drop a validation, swap two arguments, remove a lock. Keep it small and obvious.
|
||||
|
||||
git worktree add /tmp/canary HEAD && cd /tmp/canary
|
||||
|
||||
## Minimal example
|
||||
Gate: `cargo test price`. Canary: change `qty >= 10` to `qty > 10`.
|
||||
|
||||
sed -i 's/qty >= 10/qty > 10/' src/lib.rs
|
||||
cargo test price; echo "exit=$?" # must be non-zero
|
||||
|
||||
Better, keep a permanent canary: a test marked `#[should_panic]` or a script that deliberately feeds a known-bad input into the gate and asserts the gate rejects it (for example, a linter fixture with a known violation, which the lint must flag).
|
||||
|
||||
## Run
|
||||
Run the full gate, not just the unit test, with the bug in place: the same command the contract uses.
|
||||
|
||||
Passing looks like: the gate fails, naming the canary (the right test, the right message). Failure of the pipeline: it exits 0 with the bug in place. Then restore the file (`git checkout -- src/lib.rs`, or remove the worktree) and confirm the gate passes again.
|
||||
|
||||
## As a contract check
|
||||
{"id": "canary", "claim": "The gate fails when a known bug is planted and passes when it is removed", "kind": "general", "rung": 2, "cmd": "sed -i 's/qty >= 10/qty > 10/' src/lib.rs && ! cargo test price; r=$?; git checkout -q -- src/lib.rs; test $r -eq 0 && cargo test price", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 600}
|
||||
|
||||
The contract above edits the working tree and restores it with `git checkout`. Do not run it with uncommitted changes in that file.
|
||||
|
||||
## What a pass does not prove
|
||||
- That the gate catches other bugs. One canary shows one path; plant several kinds (value, boundary, state, concurrency).
|
||||
- A canary that is too blatant is caught by anything. Make it realistic for the change.
|
||||
- It must fail for the right reason. Read the failure output; a compile error from your sed is not a catch.
|
||||
- Always revert the canary. A forgotten planted bug is worse than no check.
|
||||
@@ -0,0 +1,51 @@
|
||||
---
|
||||
id: coverage-floor
|
||||
title: Coverage as a ratchet that may not fall
|
||||
answers: Did my change make any code or branch stop being exercised by tests?
|
||||
rung: 3
|
||||
section: test-quality
|
||||
languages: rust, python, javascript, typescript, go
|
||||
kinds: general
|
||||
cost: low
|
||||
detect: cargo llvm-cov --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- A change might delete, narrow or replace tests, and the loss should not go unnoticed.
|
||||
- You want a cheap tripwire in CI: a recorded floor that the number may not drop below.
|
||||
- New code should come with tests that execute it.
|
||||
|
||||
## Do not use when
|
||||
- You want to know whether the code is correct. Coverage shows what ran, not what was checked. In my run the Python function had 100% line and branch coverage and 3 of 7 mutants survived. Use `cargo-mutants`, `mutmut` or `stryker`.
|
||||
- You would set a high target and chase it. A floor that only ratchets up is the use; a target invites empty tests.
|
||||
|
||||
## Set up
|
||||
- Rust: `cargo llvm-cov` (ran 0.9.1). Branch coverage needs nightly: `--branch`.
|
||||
- Python: `pip install coverage`; `coverage run --branch -m pytest`.
|
||||
- JavaScript: `npx c8 <test command>`, with `--check-coverage --lines 80` (from c8's docs; not run here). Vitest and Jest have built-in coverage with thresholds.
|
||||
- Go: `go test -cover ./...` and `-coverprofile=c.out`, then `go tool cover -func=c.out` (from the Go docs; not run here).
|
||||
|
||||
## Minimal example
|
||||
Record the floor in a file (the value of the last good run), then compare:
|
||||
|
||||
cargo llvm-cov --summary-only
|
||||
# coverage.py
|
||||
coverage run --branch -m pytest -q && coverage report --fail-under=100
|
||||
|
||||
The ratchet: store the total in a doc or a file, fail if the new total is more than a small tolerance below it, and raise the stored number when coverage rises. One Rust project does this with a script that reads the TOTAL row of the report, compares line cover to the recorded floor with a 0.5 point tolerance, and is wired in as a recipe.
|
||||
|
||||
## Run
|
||||
cargo llvm-cov --fail-under-lines 80
|
||||
|
||||
Passing looks like: the report prints and exit is 0. Failure: exit 1 when lines are below the limit (checked here: 85.71% failed at 100, passed at 80). Python: `coverage report --fail-under=N` exits 2 below N.
|
||||
|
||||
## As a contract check
|
||||
{"id": "coverage-floor", "claim": "Line coverage has not fallen below the recorded floor of 80", "kind": "general", "rung": 3, "cmd": "cargo llvm-cov --fail-under-lines 80", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 1200}
|
||||
|
||||
## What a pass does not prove
|
||||
- Only that code was executed, not that results were checked. Tests with no assertions give full coverage.
|
||||
- Excluded files and generated code lower the denominator; list what is excluded.
|
||||
- Lowering the floor to get a pass defeats the check. A floor change in the diff needs a stated reason.
|
||||
- Check the check: delete a test and confirm the number drops and the gate fails.
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
id: flaky-test-detection
|
||||
title: Detect flaky tests with repeat runs, random order and fixed seeds
|
||||
answers: Does this test give the same result every time, in any order?
|
||||
rung: 3
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, concurrency
|
||||
cost: low
|
||||
detect: cargo nextest --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You added or changed a test and want to know it is stable before calling it done.
|
||||
- A test failed once and passed on rerun.
|
||||
- You suspect order dependence: a test passes alone and fails in the suite.
|
||||
|
||||
## Do not use when
|
||||
- You are looking for a concurrency bug in the code itself. Repetition samples; use `loom`, `shuttle` or `stress-repeat` with the right framing.
|
||||
- The failure is deterministic. Fix it; do not repeat it.
|
||||
|
||||
## Set up
|
||||
- Rust: `cargo install cargo-nextest`. Repeat with `--stress-count N` (also `--stress-duration`), retry with `--retries N`, which marks a test that failed then passed as flaky (nextest 0.9.143; `--flaky-result` controls whether that counts as pass or fail, see `--help`). Shuffled order needs nightly: `cargo +nightly test -- -Zunstable-options --shuffle` (from memory; confirm).
|
||||
- Python: `pytest-randomly` shuffles by default and prints the seed; rerun with `-p randomly -p no:randomly` to disable, or `--randomly-seed=<seed>` to reproduce an order (from its docs). `pytest-repeat` adds `--count=N`.
|
||||
- Go: `go test -count=20 -shuffle=on -race ./pkg`; it prints `-test.shuffle <seed>`, reuse it as `-shuffle=<seed>`.
|
||||
- JavaScript: Vitest `--sequence.shuffle`; Jest `--randomize`.
|
||||
|
||||
## Minimal example
|
||||
`cargo nextest run --stress-count 50 my_test` reruns one test 50 times.
|
||||
|
||||
## Run
|
||||
cargo nextest run --stress-count 50 -E 'test(my_test)'
|
||||
cargo nextest run --retries 2 # report which tests needed a retry
|
||||
|
||||
Passing looks like: every iteration passes and no `FLAKY` label is shown. Failure shows the failing iteration and its output. When a test is flaky, capture the seed, or the shuffle seed, from the first failure before rerunning.
|
||||
|
||||
Quarantining honestly: if a test is flaky and cannot be fixed now, do not delete it or add a blanket retry. Mark it ignored with a comment naming the issue and the date, keep it running in a separate non-gating job so its failure rate stays visible, and list it in the final report as an open problem. A quarantined test is not a passing test.
|
||||
|
||||
## As a contract check
|
||||
{"id": "no-flaky", "claim": "The new test passes 50 consecutive runs", "kind": "general", "rung": 3, "cmd": "cargo nextest run --stress-count 50 -E 'test(my_test)'", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 900}
|
||||
|
||||
## What a pass does not prove
|
||||
- Fifty passes bound the failure rate only roughly: a 1-in-100 flake passes fifty runs more than half the time. Run more, or use controls from `test-determinism`.
|
||||
- Order dependence appears only in the orders tried. Record seeds.
|
||||
- Retries hide flakiness. A contract that relies on `--retries` to pass is hiding the problem; require zero retries.
|
||||
- Check that the filter matched a test (`no tests to run` exits 4 in nextest unless `--no-tests=pass` is set).
|
||||
@@ -0,0 +1,62 @@
|
||||
---
|
||||
id: go-mutation
|
||||
title: Mutation testing for Go with gremlins
|
||||
answers: Would my Go tests notice if this code were wrong?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: go
|
||||
kinds: general, money, parsing
|
||||
cost: medium
|
||||
detect: gremlins --version
|
||||
ran_here: no
|
||||
verified: docs
|
||||
---
|
||||
|
||||
## Use when
|
||||
- A Go package has tests and you want evidence they can fail.
|
||||
- Line coverage is high but you doubt the assertions.
|
||||
- It is a check on other checks; it gives rung 2 confidence in the tests.
|
||||
|
||||
## Do not use when
|
||||
- Gremlins is maintained slowly, not abandoned: its latest release is v0.6.0 (December 2025) and its last commit was March 2026. A more recently active alternative is the `avito-tech/go-mutesting` fork (commits in October 2026); the original `zimmski/go-mutesting` has been idle since July 2024. Pick one of the first two.
|
||||
- The package has no tests, or its tests are slow; each mutant reruns them. The README says gremlins works well on smallish modules and takes hours on very big ones.
|
||||
- You need a stable CLI: the README warns that flags and config can change between 0.x minor releases, and only the current minor is maintained.
|
||||
|
||||
## Set up
|
||||
Needs Go (the repository's `go.mod` says 1.25). Install a release package (`.deb`, `.rpm`, or a binary from the GitHub releases page; Homebrew via `brew tap go-gremlins/tap && brew install gremlins` on macOS) or:
|
||||
|
||||
go install github.com/go-gremlins/gremlins/cmd/gremlins@v0.6.0
|
||||
|
||||
Pin the version, as the install page does. Not installed on this machine, so the example below was not run.
|
||||
|
||||
## Minimal example
|
||||
`price.go`:
|
||||
|
||||
package shop
|
||||
|
||||
func Price(qty, unit int) int {
|
||||
total := qty * unit
|
||||
if qty >= 10 { return total - total/10 }
|
||||
return total
|
||||
}
|
||||
|
||||
`price_test.go` with `Price(2,100)==200` and `Price(20,100)==1800` only. The boundary mutation (`>=` to `>`) lives, because no test uses `qty == 10`.
|
||||
|
||||
## Run
|
||||
Run from the module root, with no package argument; gremlins takes its target from the current module:
|
||||
|
||||
gremlins unleash --threshold-efficacy 80
|
||||
|
||||
Gremlins reports each mutant as KILLED, LIVED, NOT COVERED, TIMED OUT or NOT VIABLE (`RUNNABLE` with `--dry-run`) and prints a test efficacy and mutant coverage figure. Failure shows LIVED mutants with file and line, for example `LIVED CONDITIONALS_BOUNDARY at price.go:5:12`.
|
||||
|
||||
Passing looks like: exit 0. Exit codes from the docs: `--threshold-efficacy N` makes gremlins exit 10 when efficacy is below N; `--threshold-mcover N` exits 11 when mutant coverage is below N. Without either flag gremlins never fails on survivors, so a bare `gremlins unleash` exits 0 with LIVED mutants. `--silent` prints only errors; `--diff origin/main` limits the run to changed code; `--exclude-files REGEX` skips generated files.
|
||||
|
||||
## As a contract check
|
||||
{"id": "go-mutation", "claim": "Gremlins efficacy for the module meets the threshold", "kind": "general", "rung": 2, "cmd": "gremlins unleash --threshold-efficacy 80 --threshold-mcover 90", "expect": {"exit": 0}, "timeout_s": 3600, "tier": "deep"}
|
||||
|
||||
## What a pass does not prove
|
||||
- Efficacy is `KILLED / (KILLED + LIVED)`; NOT COVERED mutants (no test reaches the line) are left out of it. Mutant coverage is `(KILLED + LIVED) / (KILLED + LIVED + NOT COVERED)`. That is why the contract sets `--threshold-mcover` too. Read both figures.
|
||||
- LIVED mutants can be equivalent; justify each.
|
||||
- The tool covers a fixed set of mutation operators (arithmetic, conditional boundary and negation, and similar), so passing does not mean the tests are thorough.
|
||||
- Without a threshold flag the exit code says nothing. Check the check: delete an assertion and confirm the run exits 10.
|
||||
- Commands and flags were read from the project's documentation sources in the repository (docs/docs), not run here.
|
||||
@@ -0,0 +1,49 @@
|
||||
---
|
||||
id: mutation-on-diff
|
||||
title: Mutation testing only on the lines a change touched
|
||||
answers: Do my tests catch wrong versions of the exact code I just changed?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: rust, javascript, typescript, python, any
|
||||
kinds: general, money, parsing
|
||||
cost: low
|
||||
detect: cargo mutants --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You changed a handful of functions and a full mutation run (hours) does not fit in one task.
|
||||
- You want a gate in a pull request: new or changed logic must have tests that can fail.
|
||||
- It is a check on other checks, so it is rung 2 like the full run, but over a smaller area.
|
||||
|
||||
## Do not use when
|
||||
- The change deletes code or only edits tests; there is nothing to mutate in a diff of removed lines.
|
||||
- You need the whole-codebase mutation score. A diff run says nothing about untouched code, and an edit in one place can weaken the tests of another; run the full tool on a schedule.
|
||||
|
||||
## Set up
|
||||
Rust, with cargo-mutants (ran 27.1.0): `--in-diff <file>` restricts mutants to lines in a unified diff. The diff must name files with `a/` and `b/` prefixes (or none). If your git config sets `diff.mnemonicPrefix`, the prefixes become `i/` and `w/` and cargo-mutants matches nothing: it prints `No mutants to filter` and exits 0. Force the prefixes:
|
||||
|
||||
git diff --src-prefix=a/ --dst-prefix=b/ main...HEAD > /tmp/change.diff
|
||||
|
||||
Other tools: StrykerJS takes `--mutate "src/file.js:10-40"` line ranges, so generate them from the diff yourself; Stryker.NET has a `--since` option (not confirmed here); mutmut can target names, `mutmut run "module.function*"`; PIT has an SCM goal (see `pitest`; not confirmed).
|
||||
|
||||
## Minimal example
|
||||
`price` has full tests; a new `shipping` function was added with none.
|
||||
|
||||
pub fn shipping(weight: u32) -> u32 { if weight > 5 { 500 } else { 100 } }
|
||||
|
||||
## Run
|
||||
git diff --src-prefix=a/ --dst-prefix=b/ > change.diff
|
||||
cargo mutants --in-diff change.diff
|
||||
|
||||
Passing looks like: `N mutants tested: N caught`, exit 0. Failure: `MISSED src/lib.rs:15:49: replace > with == in shipping`, exit 2. In my run 5 of 5 mutants on the new function were missed; the old function's mutants were not run. Check the count: `cargo mutants --list --in-diff change.diff` must be non-empty, or the check is vacuous.
|
||||
|
||||
## As a contract check
|
||||
{"id": "mutants-on-diff", "claim": "Tests catch mutants of every line changed against main", "kind": "general", "rung": 2, "cmd": "git diff --src-prefix=a/ --dst-prefix=b/ main...HEAD > /tmp/c.diff && test -s /tmp/c.diff && cargo mutants --in-diff /tmp/c.diff", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 1800}
|
||||
|
||||
## What a pass does not prove
|
||||
- Only changed lines were mutated. The rest of the code is untested by this check.
|
||||
- Non-Rust files, and changes with no mutable code (config, comments, test-only edits), produce zero mutants and so pass. Assert a non-zero mutant count in the contract when the task is meant to change logic.
|
||||
- The same limits as the full run apply: equivalent mutants, and tests that pass for the wrong reason.
|
||||
- Check the check: break a changed line in a harmless way and confirm the run fails.
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: mutmut
|
||||
title: Mutation testing for Python with mutmut
|
||||
answers: Would my Python tests notice if this function were wrong?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: python
|
||||
kinds: general, money, parsing
|
||||
cost: medium
|
||||
detect: mutmut --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You wrote pytest tests and want proof they fail when the code changes.
|
||||
- Line coverage is 100% but you doubt the assertions (this is common; see `coverage-floor`).
|
||||
- The code is pure logic in functions. Mutation testing is a check on other checks, giving rung 2 confidence in the tests.
|
||||
|
||||
## Do not use when
|
||||
- The target is module-level code outside functions; mutmut 3 mutates functions. mutmut 2 works differently.
|
||||
- Windows without WSL: mutmut needs `fork`.
|
||||
- Tests are slow or depend on network. Fix that first.
|
||||
|
||||
## Set up
|
||||
uv pip install mutmut pytest # or: pip install mutmut
|
||||
# pyproject.toml
|
||||
[tool.mutmut]
|
||||
source_paths = ["src/"]
|
||||
pytest_add_cli_args_test_selection = ["tests/"]
|
||||
[tool.pytest.ini_options]
|
||||
pythonpath = ["src"]
|
||||
|
||||
Tests must import the code by its package name (`from shop.pricing import price`), not through a path like `src.pricing`; the latter fails with `Failed trampoline hit`. Tool version tried: mutmut 3.8.1. An alternative with a different design is `cosmic-ray` (session file, `cosmic-ray init`, `exec`); I did not run it.
|
||||
|
||||
## Minimal example
|
||||
`src/shop/__init__.py` (empty), `src/shop/pricing.py`:
|
||||
|
||||
def price(qty: int, unit: int) -> int:
|
||||
total = qty * unit
|
||||
if qty >= 10:
|
||||
return total - total // 10
|
||||
return total
|
||||
|
||||
`tests/test_pricing.py` with only `price(2, 100) == 200` and `price(20, 100) == 1800`.
|
||||
|
||||
## Run
|
||||
mutmut run
|
||||
mutmut results
|
||||
mutmut show shop.pricing.x_price__mutmut_3
|
||||
|
||||
Passing looks like: `mutmut results` lists no `survived` lines. Failure shows survivors such as `shop.pricing.x_price__mutmut_3: survived`; `mutmut show` prints the diff (here `qty >= 10` became `qty > 10`). Note: `mutmut run` exits 0 even with survivors, so the contract must test the `results` output. Stop and rerun to resume; delete the `mutants/` directory for a fresh run. `# pragma: no mutate` skips a line.
|
||||
|
||||
## As a contract check
|
||||
{"id": "mutmut-pricing", "claim": "No surviving mutants in the pricing module", "kind": "general", "rung": 2, "cmd": "mutmut run && test -z \"$(mutmut results | grep survived)\"", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 1800}
|
||||
|
||||
## What a pass does not prove
|
||||
- It shows the tests kill small edits, not that behaviour matches the requirement.
|
||||
- Equivalent mutants cannot be killed; mark them `# pragma: no mutate` with a reason, never silently.
|
||||
- Only functions are mutated. Check `mutmut results` counts a plausible number of mutants, or the run is vacuous (0 mutants, source_paths wrong).
|
||||
- In my run, 3 of 7 mutants survived while coverage.py reported 100% line and branch coverage.
|
||||
@@ -0,0 +1,65 @@
|
||||
---
|
||||
id: pitest
|
||||
title: Mutation testing for the JVM with PIT (pitest)
|
||||
answers: Would my Java or Kotlin tests notice if this code were wrong?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: java, kotlin
|
||||
kinds: general, money, parsing
|
||||
cost: medium
|
||||
detect: mvn --version
|
||||
ran_here: no
|
||||
verified: docs
|
||||
---
|
||||
|
||||
## Use when
|
||||
- A Maven or Gradle JVM project has JUnit tests and you want evidence they can fail.
|
||||
- You want a pass/fail gate on mutation score in CI.
|
||||
- It is a check on other checks; it gives rung 2 confidence in the tests.
|
||||
|
||||
## Do not use when
|
||||
- The build is not on Maven, Gradle or an IDE that PIT supports.
|
||||
- Tests are slow integration tests; scope `targetTests` tightly.
|
||||
|
||||
## Set up
|
||||
Not run here (no JDK configured on this machine); taken from pitest.org's Maven quickstart. Add to `pom.xml`:
|
||||
|
||||
<plugin>
|
||||
<groupId>org.pitest</groupId>
|
||||
<artifactId>pitest-maven</artifactId>
|
||||
<version>LATEST</version> <!-- pin a real version in your project -->
|
||||
<configuration>
|
||||
<targetClasses><param>com.example.pricing*</param></targetClasses>
|
||||
<targetTests><param>com.example.pricing*</param></targetTests>
|
||||
<mutationThreshold>80</mutationThreshold>
|
||||
<withHistory>true</withHistory>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
For JUnit 5, also add the `pitest-junit5-plugin` as a plugin dependency (from memory of the PIT docs; confirm in your version). Gradle uses the separate `info.solidsoft.pitest` plugin (`gradle pitest`; not checked here).
|
||||
|
||||
## Minimal example
|
||||
`Pricing.java`:
|
||||
|
||||
public class Pricing {
|
||||
public static int price(int qty, int unit) {
|
||||
int total = qty * unit;
|
||||
return qty >= 10 ? total - total / 10 : total;
|
||||
}
|
||||
}
|
||||
|
||||
With a JUnit test for `price(2,100)` and `price(20,100)` only, PIT reports a survivor for the boundary `>=` changed to `>`.
|
||||
|
||||
## Run
|
||||
mvn test-compile org.pitest:pitest-maven:mutationCoverage
|
||||
|
||||
Passing looks like: the build succeeds and the console shows a mutation score at or above `mutationThreshold`; the HTML report is in `target/pit-reports/`. Failure: the build fails with a score below the threshold, and the report lists `SURVIVED` mutations with the line. `withHistory` stores results to speed up reruns. To check only changed code PIT has an SCM goal (`scmMutationCoverage`); I could not confirm its options from the docs I read, so read the PIT docs before relying on it.
|
||||
|
||||
## As a contract check
|
||||
{"id": "pitest-score", "claim": "Mutation score of the pricing package is at least 80", "kind": "general", "rung": 2, "cmd": "mvn -q test-compile org.pitest:pitest-maven:mutationCoverage", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 3600}
|
||||
|
||||
## What a pass does not prove
|
||||
- Only the classes matched by `targetClasses` are mutated; a too-narrow glob yields a vacuous pass. Check the report names your classes.
|
||||
- Surviving mutants may be equivalent; exclude with `excludedMethods` or `avoidCallsTo` and say why.
|
||||
- A score is a ratio of sampled changes, not correctness.
|
||||
- With no `mutationThreshold`, the build passes whatever the score. Check it is set.
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: red-before-green
|
||||
title: Prove a new test can fail by seeing it fail before the fix
|
||||
answers: Does this new test actually fail without my change, so its passing means something?
|
||||
rung: 3
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, money, parsing, state-machine
|
||||
cost: low
|
||||
detect: git --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You fixed a bug and wrote a regression test.
|
||||
- You added a feature and a test for it.
|
||||
- Any time a test is the evidence for done: a test that cannot fail is not evidence.
|
||||
|
||||
This is a check on another check. The test itself is rung 3 (an example test). Seeing it fail on the old code adds confidence that the test is sensitive to the bug, but it does not raise the claim above rung 3; a property test or mutation run is what reaches rung 2.
|
||||
|
||||
## Do not use when
|
||||
- The change is a pure refactor with no behaviour change; there is no red to see. Use existing tests and `mutation-on-diff`.
|
||||
- The failure would only be a compile error because the new API does not exist yet. Stub the API first so the test compiles and fails on behaviour.
|
||||
|
||||
## Set up
|
||||
Two ways to get the red run. Either write the test first and run it before editing the code, or after the fix revert only the fix and rerun. With git:
|
||||
|
||||
git stash push -- src/ # park the fix, keep the new tests
|
||||
# or: git checkout HEAD~1 -- src/file # restore the old source
|
||||
|
||||
## Minimal example
|
||||
Bug: `price(10, 100)` returned 1000 instead of 900. Add the test in a separate file, `tests/threshold.rs`: `#[test] fn threshold_discount() { assert_eq!(mycrate::price(10, 100), 900); }`. If it lives in the same file as the fix, stashing the fix stashes the test too.
|
||||
|
||||
git stash push -- src/
|
||||
cargo test threshold_discount # red
|
||||
git stash pop
|
||||
cargo test threshold_discount # green
|
||||
|
||||
## Run
|
||||
Run the exact new test by name on the old code, then on the new code.
|
||||
|
||||
Passing looks like: the first run fails with an assertion about the behaviour you fixed (`left: 1000, right: 900`), the second passes with `1 passed`. Failure of the check: the test passes on the old code (it tests nothing about the bug), or fails for a different reason (a compile error, a missing file, a typo). Read the failure message, not just the exit status. Also confirm the test ran: `0 passed` or `no tests to run` is not red.
|
||||
|
||||
## As a contract check
|
||||
{"id": "red-green", "claim": "The regression test fails on the pre-fix source and passes on the fixed source", "kind": "general", "rung": 3, "cmd": "git stash push -q -- src/ && ! cargo test threshold_discount; r=$?; git stash pop -q; test $r -eq 0 && cargo test threshold_discount", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 600}
|
||||
|
||||
## What a pass does not prove
|
||||
- That the test fails for the right reason. Inspect the red failure output yourself.
|
||||
- That the test is strong: it may fail on one input and miss nearby ones. Pair with `cargo-mutants` or a property test.
|
||||
- If the fix and the test touch the same files, stashing the wrong paths removes the test too; check that the red run still contains the test.
|
||||
- Always restore the fix afterwards (`git stash pop`) and rerun the green run.
|
||||
@@ -0,0 +1,61 @@
|
||||
---
|
||||
id: stryker
|
||||
title: Mutation testing for JavaScript, TypeScript and C# with Stryker
|
||||
answers: Would my JS, TS or C# tests notice if this code were wrong?
|
||||
rung: 2
|
||||
section: test-quality
|
||||
languages: javascript, typescript, csharp
|
||||
kinds: general, money, parsing, ui
|
||||
cost: medium
|
||||
detect: npx --no-install stryker --version
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You wrote Vitest, Jest, Mocha or .NET tests and want evidence they can fail.
|
||||
- You need a numeric gate: Stryker computes a mutation score and exits non-zero below a threshold.
|
||||
- It is a check on other checks; it gives rung 2 confidence in the tests.
|
||||
|
||||
## Do not use when
|
||||
- The change is a few lines and a full run is slow; mutate only those files or use `--incremental`; see `mutation-on-diff`.
|
||||
- Tests are mostly snapshots of rendered output; they may kill many mutants without asserting anything meaningful.
|
||||
|
||||
## Set up
|
||||
StrykerJS (ran version 10.1.1):
|
||||
|
||||
npm i -D @stryker-mutator/core @stryker-mutator/vitest-runner vitest
|
||||
# stryker.config.mjs
|
||||
export default {
|
||||
testRunner: 'vitest',
|
||||
mutate: ['src/**/*.js'],
|
||||
reporters: ['clear-text', 'json'],
|
||||
thresholds: { high: 90, low: 80, break: 80 },
|
||||
};
|
||||
|
||||
Other runners are separate plugins (`@stryker-mutator/jest-runner`, `mocha-runner`). Stryker.NET (docs only, not run here): `dotnet tool install -g dotnet-stryker`, then `dotnet stryker` from the test project directory; options in `stryker-config.json`.
|
||||
|
||||
## Minimal example
|
||||
`src/price.js`:
|
||||
|
||||
export function price(qty, unit) {
|
||||
const total = qty * unit;
|
||||
if (qty >= 10) return total - Math.floor(total / 10);
|
||||
return total;
|
||||
}
|
||||
|
||||
`test/price.test.js` with `price(2,100) === 200` and `price(20,100) === 1800`.
|
||||
|
||||
## Run
|
||||
npx stryker run
|
||||
|
||||
Passing looks like: the table shows a mutation score at or above `break`, and the log line `Final mutation score of 87.50 is greater than or equal to break threshold 80`, exit 0. Failure: a score below `break` exits 1; survivors are listed with the mutated code (here `qty >= 10` mutated to `qty > 10`). Useful options: `--mutate "src/a.js:10-40"` for line ranges, `--incremental` (state in `reports/stryker-incremental.json`), `--concurrency`, `--timeoutMS`, `coverageAnalysis: 'perTest'` to run only tests that cover a mutant.
|
||||
|
||||
## As a contract check
|
||||
{"id": "stryker-score", "claim": "Mutation score of src/ is at least 80", "kind": "general", "rung": 2, "cmd": "npx stryker run", "expect": {"exit": 0, "stdout_contains": "Final mutation score"}, "tier": "deep", "timeout_s": 1800}
|
||||
|
||||
## What a pass does not prove
|
||||
- The score is a percentage over sampled mutations; 80 means 20% of changes went unnoticed.
|
||||
- Survivors may be equivalent; disable with `// Stryker disable next-line <mutator>: reason` and list them.
|
||||
- `break` must be set or the command always exits 0. Check `thresholds` has all three keys (high, low, break) or none.
|
||||
- A very high score with a tiny mutant count means little. Confirm the intended files were mutated.
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: test-determinism
|
||||
title: Make tests deterministic with fixed seeds, fake clocks and no network
|
||||
answers: Will this test give the same result on any machine, at any time, with no network?
|
||||
rung: 3
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, concurrency, money
|
||||
cost: low
|
||||
detect:
|
||||
ran_here: no
|
||||
verified: docs
|
||||
---
|
||||
|
||||
## Use when
|
||||
- A test uses randomness, the current time, timeouts, sleeps, the file system, the network or environment variables.
|
||||
- You are writing tests an agent will rerun many times and compare across runs.
|
||||
- A test result changes with the date, the time zone, the locale or the order of map iteration.
|
||||
|
||||
## Do not use when
|
||||
- You are testing the integration itself against a real service. Keep that in a separate, labelled suite.
|
||||
|
||||
## Set up
|
||||
Control each source of nondeterminism:
|
||||
- Randomness: pass an explicit seed; print it on failure so the run can be replayed. Property tests: proptest stores failures in `proptest-regressions/`; Hypothesis in `.hypothesis/` (use `@seed` or `--hypothesis-seed`). Go: `rand.New(rand.NewSource(seed))`.
|
||||
- Time: inject a clock. Rust Tokio: `tokio::time::pause()` with `#[tokio::test(start_paused = true)]`. Python: `freezegun` or `time-machine`. JavaScript: `vi.useFakeTimers()` or Jest fake timers. Never call `sleep` to wait for a result; wait on the event.
|
||||
- Network: no real sockets. Use in-process fakes, a local listener bound to port 0, recorded fixtures or a simulator (see `deterministic-simulation`). Run the suite once with the network off (`unshare -rn cargo test` on Linux, or a sandbox) to prove it.
|
||||
- Files: use a fresh temp directory per test; set `TMPDIR` explicitly.
|
||||
- Order: do not depend on map or set iteration order; sort before comparing. Do not share mutable global state between tests.
|
||||
- Environment: set `TZ=UTC`, `LC_ALL=C`, fixed locale.
|
||||
|
||||
## Minimal example
|
||||
A test with an explicit seed, and a way to replay it:
|
||||
|
||||
let seed = std::env::var("SEED").ok().and_then(|s| s.parse().ok()).unwrap_or(12345u64);
|
||||
eprintln!("seed = {seed}");
|
||||
let mut rng = StdRng::seed_from_u64(seed);
|
||||
|
||||
## Run
|
||||
SEED=12345 cargo test my_test
|
||||
unshare -rn cargo test --offline my_test # no network, Linux user namespace
|
||||
|
||||
Passing looks like: the same result on repeated runs and with the network off. Failure: a test needing the network fails with a connection error, or results differ between two runs with the same seed. Not run here; `unshare` is Linux-specific and needs unprivileged user namespaces.
|
||||
|
||||
## As a contract check
|
||||
{"id": "hermetic", "claim": "Tests pass with no network access", "kind": "general", "rung": 3, "cmd": "unshare -rn cargo test --offline", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 900}
|
||||
|
||||
## What a pass does not prove
|
||||
- That the code is correct; it proves the test is repeatable.
|
||||
- Fixed seeds explore one input. Vary them in a separate property-test job.
|
||||
- A fake clock hides real timing bugs. Keep a few real-time tests, labelled.
|
||||
- Check the check: run twice with the same seed and diff the outputs; add `--test-threads=1` and the default to expose shared state.
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
id: vacuity-check
|
||||
title: Check that tests and assertions actually ran
|
||||
answers: Did my tests really execute and assert something, or did the run pass because nothing ran?
|
||||
rung: 3
|
||||
section: test-quality
|
||||
languages: any
|
||||
kinds: general, parsing, state-machine
|
||||
cost: low
|
||||
detect:
|
||||
ran_here: yes
|
||||
verified: ran
|
||||
---
|
||||
|
||||
## Use when
|
||||
- You ran a test with a name filter, a path, or a marker and it said "ok".
|
||||
- A property or generated test might discard every input, so its body never runs.
|
||||
- A pass is the only evidence for done. A pass with zero tests is a false finish.
|
||||
|
||||
## Do not use when
|
||||
- It is not a separate check of the code. It guards the other checks; use it beside them.
|
||||
|
||||
## Set up
|
||||
Know what "nothing ran" looks like in your runner. Measured here:
|
||||
|
||||
- `cargo test nomatch`: prints `0 passed; ... 2 filtered out`, exit 0.
|
||||
- `cargo nextest run nomatch`: `error: no tests to run`, exit 4 (nextest 0.9.143). `--no-tests=pass` makes it exit 0, so do not add that flag.
|
||||
- `pytest -k nomatch`: `2 deselected`, exit 5. An empty collection is exit 5, not 0.
|
||||
- `go test -run Nomatch`: prints `testing: warning: no tests to run` and exits 0 (from the Go docs, not run here).
|
||||
- Jest and Vitest fail on no tests by default, unless `--passWithNoTests` is set.
|
||||
|
||||
## Minimal example
|
||||
Assert that the count of executed tests is at least one. For cargo:
|
||||
|
||||
out=$(cargo test my_test 2>&1); echo "$out"
|
||||
echo "$out" | grep -E 'test result: ok\. [1-9][0-9]* passed' >/dev/null
|
||||
|
||||
For preconditions: assert the interesting case happened, for example in a property test count how many inputs were accepted and fail if none. In proptest use `prop_assume!` sparingly and check the rejection rate; in Hypothesis use `event()` and `target()`, and read `--hypothesis-show-statistics`; in a loop test keep a counter and `assert!(count > 0)` after the loop. For distributed tests, assert that a fault was actually injected.
|
||||
|
||||
## Run
|
||||
Run the tests with a name filter, then check the count line as above. Also check skipped and ignored tests: `cargo test -- --include-ignored` runs them; pytest `-rs` lists skips; Go prints `SKIP`.
|
||||
|
||||
Passing looks like: at least one test is reported passed, none skipped that matter, and the assertion-count or event check is above zero. Failure is a zero count, or all tests skipped or filtered out.
|
||||
|
||||
## As a contract check
|
||||
{"id": "tests-ran", "claim": "At least one test matching the filter ran and passed", "kind": "general", "rung": 3, "cmd": "cargo test my_test 2>&1 | grep -E 'test result: ok\\. [1-9][0-9]* passed'", "expect": {"exit": 0}, "tier": "deep", "timeout_s": 600}
|
||||
|
||||
## What a pass does not prove
|
||||
- That the assertions inside the tests are meaningful; see `assertion-strength-review`.
|
||||
- That the test is sensitive to the bug; see `red-before-green`.
|
||||
- Counting tests does not count assertions. A test with an `if` that skips its assertion, or an assert in a callback that never fires, still passes. Add a counter or use mutation testing.
|
||||
- Cargo's output format can change; keep the pattern tested against your version.
|
||||
Reference in new issue
Block a user