Getting started
On this page
Run it #
Run gomagi from a module with go package patterns, as for go test. With no pattern it runs ./....
gomagi run ./...
On a terminal gomagi draws a row per package with one mark per mutant. Written to a pipe or a CI log, it prints a line per package instead. On the fixture module testdata/calc:
example.com/calc: 27 mutants (1 rebuilt one at a time), 9 tests, build 166ms, coverage 72ms
2 survivors never changed a value in a run of the whole suite without a mutant; the suite did not run again for them
1 runs stopped as endless loops ran again with a 10 times higher run count; 0 of them then finished
LIVED CONDITIONALS_BOUNDARY calc.go:13:7 < -> <=
LIVED CONDITIONALS_BOUNDARY calc.go:16:7 > -> >=
Killed: 22, Lived: 2, Not covered: 2
Timed out: 1, Not viable: 0
Mutation score: 91.67%
Mutant coverage: 92.59%
Elapsed: 659ms
Read the result #
| Outcome | Meaning |
|---|---|
| Killed | a test failed with the mutant active |
| Lived | every test passed with the mutant active |
| Not covered | no test runs the mutated line |
| Timed out | the tests ran until their time limit, or looped far past what the line runs without the mutant |
| Not viable | the mutated code does not compile |
The mutation score is killed / (killed + lived): of the mutants some test reaches, the share a test catches. Mutant coverage is the share of all mutants some test reaches.
Fix a lived mutant #
A lived mutant means one of two things: a test is missing, or the change cannot be observed.
A missing test is the usual case. Take a discount that starts at 100:
func Discount(total int) int {
if total >= 100 {
return total - 10
}
return total
}
Tests with totals of 50 and 200 pass whether the condition is >= 100 or > 100, so gomagi reports LIVED CONDITIONALS_BOUNDARY ... >= -> >. A test with a total of exactly 100 kills it.
Some mutants change nothing a caller can observe. Both lived mutants of Clamp above are of that kind:
func Clamp(v, lo, hi int) int {
if v < lo {
return lo
}
v < lo and v <= lo differ only when v == lo, and then Clamp returns lo, which is v. No test can kill such an equivalent mutant. Mark the line, or the doc comment of a function, with //gomagi:ignore and a reason, and gomagi leaves it alone:
if v < lo { //gomagi:ignore returning lo or v is the same when they are equal
See every mutant #
--output report.html writes a page with each file’s lines that hold mutants, their source and what happened to each mutant. --output report.json writes the same results as JSON in the report schema of Stryker, which other mutation testing tools also write. Command line describes both.
Gate pull requests #
On a pull request, mutate only the lines it changes and fail when the score drops:
gomagi run --diff origin/main --min-score 80 ./...
--diff needs the base revision in the clone, so check out with history (fetch-depth: 0). GitHub Actions has a complete workflow.