What is mutation testing?
Mutation testing is a way to check your tests. It asks: if we make a small bug in the code, will the tests fail?
The idea (simple)
- Take your real code.
- Make one tiny change (a mutation).
- Run your tests.
- If tests fail, good: the mutation is killed. If tests still pass, not good: the mutation survived.
Why people use it
- Your tests can be green but still miss important behavior.
- Mutation testing shows where assertions are missing or too weak.
- It gives a repeatable signal in CI (not just "coverage").
A small example (what Gooze finds)
Code:
func label(balance int) string {
if balance > 0 {
return "positive"
}
return "not-positive"
}Test (too weak): it only checks balance = 1.
import "testing"
func TestLabel_Positive(t *testing.T) {
got := label(1)
if got != "positive" {
t.Fatalf("expected positive, got %q", got)
}
}One common mutation is changing > to >=. That means balance = 0 becomes "positive". If you do not test balance = 0, this mutation can survive.
- if balance > 0 {
+ if balance >= 0 {
return "positive"
}Fix: add a test for balance = 0 (and maybe negative too).