I have previously written about code coverage and why it should not be used as measurement of test quality.
Mutation testing is an automated method of testing how your code - and tests - behave when the code changes slightly.
Initially, a mutation testing framework will do a test run with code coverage and note all lines that are not covered - and therefore could be removed completely without a single test failing.
Similar to corona virus mutations, mutation testing frameworks will make small alterations to the code and expect one or more tests to fail. If no tests fail, the mutant is said to have escaped, and just like in Resident Evil, escaped mutants are bad news.
What is a mutant
A mutant in the case of mutation testing is a version of the code with one small change. It could be removing a line in a method calling another method. It could be changing an equality operator to a non-equality operator. It could be switching a greater-than sign to a less-than sign.
It is incredibly valuable to combine code coverage with mutations in the code to make sure that not only is the code covered, the tests are actually making assertions that rely on the code being exactly as it is.
This is power. This is security. This is confidence.
Mutation testing in Laravel
Working with mutation testing in Laravel is a breeze. Laravel already is extremely easy with the PHPUnit framework naturally embedded - and encouraged - in Laravel. Adding mutation testing is simply a matter of installing Infection and running it. Provided all your tests pass, Infection will automatically alter your code and re-run the relevant tests (decided by the test coverage run initially). Any alterations that do not result in one or more failing tests will be logged so you can easily see which lines of code are either unnecessary or untested.
So get started with mutation testing and increase the confidence in your tests