Mutation testing (or mutation analysis or program mutation) is used to design new software tests and evaluate the quality of existing software tests.
How can we measure test quality ?
Use our intuition ?
Not really a good indicator : too much subjective.
Use code coverage ?
Code coverage is a quantitative metric not a qualitative one.
Let's take a simple example :
Imagine I have written a Calculator.add method that simply add an integer x and an integer y. I have written some tests on it :
It could seem fine at the first look with a 100% lines covered according to my code coverage tool :
But when you take a closer look the tests the assertions are really shitty :
Mutation testing to our rescue
What is it ?
Mutation testing is based on two hypotheses :
The first is the competent programmer hypothesis. This hypothesis states that most software faults introduced by experienced programmers are due to small syntactic errors.
The second hypothesis is called the coupling effect. The coupling effect asserts that simple faults can cascade or couple to form other emergent faults.” - wikipedia
The concept behind :
Test our tests by introducing MUTANTS (fault) into our production code during the test execution :
To check that the test is failing
If the test pass, there is an issue
3 steps
Generate mutants
Launch tests
Check result / Generate report
What is a mutant ?
A mutant is created by altering the production in various ways :
For conditions :
Change
Reverse conditions
Constant
For math operations :
Change increment with decrement (from x++ to x--)
Change binary operations
Remove function calls
Rename constants
...
How to generate mutants ?
To use mutation testing we can use tools like pitest (for java) but others are available like stryker.
Start with pitest
Simply add a dependency in your pom to the pitest-maven plugin :