Mutation Testing - the missing link

Dec 15, 2021

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

by Jan Keller

Long time developer, architect and CTO with a real love for the backend. On this blog, I give out insight gathered through working with Vue, Nuxt, Laravel and more, when building and maintaining our SaaS.

Catch me on Twitter and Github

Restructuring a Laravel application using Domains, Actions and more

In coding tutorials, much is often left out in order not to be too verbose, to keep the complexity down or because it feels over engineering to follow through with the examples. I will give a full structural suggestion based on my experience that can be applied directly, regardless of the project size.

Jan Keller Jun 8, 2022

TDD without tests first approach

TDD is synonymous with writing tests first and implementations later. I want to challenge that approach as a catch-all do-all approach.

Jan Keller Dec 15, 2021

Lessons learned migrating to Laravel Vapor

When moving to Laravel Vapor, there are things to consider. In this article, I name the obstacles I have experienced in a recent project.

Jan Keller Jun 4, 2021

Tree hierarchies in Laravel

Three approaches to parent-child tree structures with relational databases.

Jan Keller Jan 1, 2021