
Table of contents
Infection: Mutation testing for better PHPUnit tests
Infection is a powerful mutation testing framework for PHP that helps you improve the quality and effectiveness of your tests. It works by specifically changing (mutating) your source code and then checking whether your tests recognise these changes1.
How does it work?
- Mutation: Infection modifies your code, e.g. by changing comparison operators such as == to != or removing conditions.
- Test execution: After each mutation, Infection executes your existing tests.
- Analysis: If the tests still exist despite the change, this indicates a gap in your test coverage1.
This process allows you to recognise which parts of your code are not sufficiently tested and improve your tests in a targeted manner.
Installation and configuration
You have several options for installing Infection.
As a development dependency with Composer:
composer require --dev infection/infection
As a global CLI tool:
composer global require infection/infection
As a PHAR archive:
wget https://github.com/infection/infection/releases/download/0.26.19/infection.phar
chmod +x infection.phar
Konfiguration
Create an infection.json file in the root directory of your project. Here is an example:
{
"source": {
"directories": [
"src"
]
},
"logs": {
"text": "infection.log"
},
"mutators": {
"@default": true
}
}
Adapt the settings to your project structure, e.g. by replacing the src folder with your actual source code directories.

Reach our PHP Consultant specialists
We are experts in PHP and help you to master your digital challenges. Our experienced team supports you with PHP updates, PHP refactoring and advises you remotely on all questions relating to PHP. With our fully automated CI/CD deployments and a robust Docker infrastructure, we take your PHP projects to the next level. Rely on our expertise for reliable and scalable PHP solutions.
Use of the PHP Infection Testing Framework
Basic execution
To execute Infection, use the following command in your project directory:
vendor/bin/infection
Infection then carries out the mutations and shows you the results.
Advanced options
Filter: Test only certain files or directories:
vendor/bin/infection --filter=src/Service/WordpressService.php
Threads: Use multiple threads for faster execution:
vendor/bin/infection --threads=4
Minimum MSI: Set a minimum value for the Mutation Score Indicator (MSI):
vendor/bin/infection --min-msi=70
Only covered code: Only test code that is already covered by tests:
vendor/bin/infection --only-covered
Existing coverage: Use already generated code coverage data:
vendor/bin/infection --coverage=build
Focus on changes: For large projects, you can restrict Infection to recently changed files4:
INFECTION_FILTER=$(git diff origin/master HEAD --name-only | grep /src/ | paste -sd "," -)
vendor/bin/infection --filter="${INFECTION_FILTER}"
Reuse code coverage: If you already have code coverage data, transfer it to Infection4:
vendor/bin/phpunit --coverage-xml=build/coverage-xml --log-junit=build/phpunit.junit.xml
vendor/bin/infection --coverage=build
Examples from practice
- Detecting test gaps
A team uses Infection and discovers that a critical condition in payment processing is not sufficiently tested. They then add a corresponding test. - Improving the test assertions
Infection shows that the removal of a password validation function is not recognised by the tests. The team revises its test assertions to cover this case. - Optimising error handling
When mutating the exception handling, it turns out that some error cases are not tested. The team implements additional tests for these borderline cases.
Frequently asked questions FAQ
What is the difference between mutation testing and code coverage?
Code coverage measures how much of the code is executed by tests, but says nothing about the quality of the tests. Mutation testing goes one step further and checks whether the tests can also recognise errors in the code by specifically changing the code.
Can I use Infection if I don't have unit tests?
No, Infection requires the existence of unit tests. If you do not yet have any tests, you should first write testable code and implement unit tests before you can use Infection.
Which PHP versions does Infection support?
Infection currently supports PHP 7.2 and higher. Make sure you are using a compatible PHP version before installing Infection.
Can Infection automatically fix test gaps?
No, Infection only identifies vulnerabilities in your tests, but does not fix them automatically. You must manually add or improve the missing or inadequate tests.
How do I interpret the results of Infection?
Infection generates a report with the Mutation Score Indicator (MSI), which indicates how well your tests recognise mutations. A higher MSI means better test quality. The results also show you which mutations have survived and where exactly you should improve your tests.
Can I integrate Infection into my CI/CD pipeline?
Yes, it is even advisable to integrate Infection into your CI/CD pipeline. This allows you to continuously monitor the test quality and ensure that no regressions occur. You can run Infection as an additional step after your unit tests.
Does mutation testing slow down my development cycles?
Mutation testing can be time-consuming, especially with large codebases. However, it is an investment in the quality of your software. You can initially apply infection only to critical parts of your code or speed up execution with options like --threads.
What alternatives are there to Infection?
There are other mutation testing tools for PHP such as Humbug or Mutatest. Similar tools exist in other languages, such as PIT for Java or Stryker for JavaScript. However, Infection is the most widely used mutation testing framework for PHP.
Can I only apply Infection to certain parts of the code?
Yes, you can apply infection specifically to individual files, directories or changed parts of code. Use the --filter option or combine Infection with tools such as git diff to test only the relevant parts.
How often should I carry out mutation testing?
Carry out mutation tests regularly, ideally with every push in the CI/CD pipeline. In larger projects, you can also carry out infection at longer intervals, e.g. weekly or before each release. Find the right rhythm for your team to keep the test quality consistently high without slowing down the development process too much.
Conclusion
Infection is a valuable tool for any PHP developer who prioritises code quality and robust testing. By using mutation testing, you can identify weaknesses in your tests, close gaps in test coverage and ultimately develop more reliable software.
Integrate Infection into your development process, start with a low bar and gradually improve your tests. Over time, you will gain more confidence in your test suite and make it more effective.
Try Infection and see for yourself how it can take the quality of your PHP applications to the next level!
Further resources
Official documentation of the Infection Framework
The official documentation is the best starting point to learn all about installing, configuring and using Infection.
https://infection.github.io/guide/
Infection Framework on GitHub
The GitHub repository of the Infection Framework contains the source code, issues, discussions and releases. Ideal for developers who want to delve deeper into the technology.
https://github.com/infection/infection
Introduction to mutation testing with Infection (blog post)
A detailed blog post explaining what mutation testing is.
https://www.tomasvotruba.cz/blog/2018/04/23/how-to-improve-your-code-quality-with-mutation-testing/
Instagram pictures on the topic of mutation testing
List of images from the hashtag #mutationtesting without further details.

Mann mit langen Haaren hält einen Vortrag auf einer Usergroup vor einem großen Monitor

Dekoratives Ad Image mit dem Text "Tips für Unit Testing"

Clip Art auf der linken Seite eine Lupe unter der ein Bug auf einem Monitor abgebildet ist. Rechts ein Monitor mit Error und Warnausgabe.

Instagram Quote Bild zu guter Code Coverage und der Hilfe mit Mutation Testing.

Hinweis auf einen neuen Blog Post, wie man die Codequalität mit Mutation Testing erhöht

Dunkle Publikumsaufnahme bei einer Usergroup

Instagram Ad für einen Mutation Testing Vortrag 2023 mit Adam Piotrofski

Mutations-Test-Report mit dunkelviolettem Hintergrund. Zeigt 148 generierte Mutationen: 3 getötet, 139 nicht durch Tests abgedeckt, 6 nicht erkannt. Metriken: MSI 2%, Code-Abdeckung 6%, MSI der abgedeckten Code 33%. Ausführungszeit 1s mit 22MB Speicherverbrauch. Wesentliche Test-Qualitätsmetriken auf einen Blick dargestellt.

PHP Code-Snippet einer getItemsFromBlog Funktion mit Mutation Testing Markup, das zwei Varianten der toArray() Methode zeigt - eine mit throw:true (rot markiert) und eine mit throw:false (grün markiert).

Terminal-Ausgabe eines erfolgreichen PHPUnit Test Refactoring mit Inflection Mutation Testing Durchlaufs mit 14 Mutationen und einem hohen MSI von 92%, angezeigt in einem violetten Terminal-Fenster.
