Created by Bobby Lough http://www.bobbylough.com
our unit tests are awesome!!!
The tests pass so the project must be defect free
Line coverage and conditional code coverage means the tests are good
public static int sum(int x, int y) {
return x + y;
}
@Test
public void testSum(
int result = Code.sum(2,2);
)
100% line coverage but no asserts
but does that mean there are no bugs?
public static int sum(int x, int y) {
return x * y;
}
@Test
public void testSum(
int result = Code.sum(2,2);
assertEquals(4, result);
)
Unit tests, aka Sentinels, protect our projects from bugs
But who protects our projects from our tests?
A mutation is a single syntactic change to the code. Each mutant is different from the original by a single mutation.
These small changes can have big results; such as changing an operator from addition to subtraction.
Then our unit tests execute against the mutant code and in theory our tests should fail because the code is wrong.
The test fails
The mutant is caught/killed. Hooray!!!
The test times out because the mutant caused an infinite loop
The test runs out of memory.
The mutant was dead on arrival and JVM just didn't like it.
number of mutants killed / total number of mutants
What are some of the different types of Mutants?
Original | Mutant |
---|---|
> | >= |
< | <= |
>= | > |
<= | < |
Original | Mutant |
---|---|
== | != |
!= | == |
<= | > |
>= | < |
Original | Mutant |
---|---|
+ | - |
- | + |
* | / |
/ | * |
% | * |
if (condition1 == condition2) {
doSomething();
}
changes to:
if (true) {
doSomething();
}
always return true
always return false
always return null
always return 0
always return 3.145
public int additionOrSubtraction(boolean isAddition, int x, int y) {
if (isAddition) { //mutant: replace isAddition with !isAddition
return x + y; //mutants: replace + with -,*,/,%
} else {
return x - y; //mutants: replace - with +,*,/,%
}
}
Mutant testing is CPU and memory intensive. There are really too many possible mutants to run against your unit tests so the mutation engine chooses likely mutants.
"PIT is a state of the art mutation testing system, providing gold standard test coverage for Java and the jvm. It's fast, scalable and integrates with modern test and build tooling."
PIT reporting:
PitClipse results:
Configuring PIT Build in maven pom - Part 1
org.pitest
pitest-maven
1.1.5
com.bobbylough.MutationTestingDemo.*
com.bobbylough.MutationTestingDemo.*
Configuring PIT Build in maven pom - Part 2
org.pitest
pitest-maven
1.1.5
report
> mvn clean install org.pitest:pitest-maven:mutationCoverage