Mutation test is a way to put a “mutation” into your code, run the test and then see if the test fails or not. A mutation is a change to production code which should make it behave in a different way. The idea is, if the production code is just enough to pass the test, any mutation should make the test failed due to the behaviour change. If test failure doesn’t happen, we will say that the test can’t kill this mutation. Therefore, some test code smell or production code smell exists, which possibly leads to some learning and improvement of our code and test.
By TDD, we should get “just enough” code that passing all the test. But, how do we know this? Sometimes, since writing test and code is so interactive in TDD, we may not choose the smallest baby step to pass the test. This means we may write some code which is not driven by current test (code smell). On the other side, we may even miss some important test or create some “weak” test by TDD because the code is too “obvious” to us (test smell). By applying Mutation test mindset and methodology, we can identify such code smell and test smell and then help us improve the quality of our code and test.
In this topic, I will first introduce what mutation test is and why we can use it to create some learning of and improve our code and test. Then, I will share a real code example in which I tried the mutation test and some learning I got. Finally, based on what I learned so far, I will have a summarisation about the code smell and test smell which can be detected by mutation test.