Fail a phpunit test if coverage goes below a threshold

Dec 30, 2020 by Thibault | 129 views

PHP GitLab DevOps

Tools like maven allow to fail a build pipeline if the coverage of unit tests goes below a given threshold. For phpunit and PHP project, there is no such option. So here is a trick to fail your pipeline if the coverage of your phpunit tests goes below a threshold.

The trick consists of 2 steps:

  1. run phpunit with the option --coverage-xml to create an xml coverage report;
  2. use a custom script (we will call it phpunit-threshold to parse the xml report and fail if the coverage goes below a provided threshold.


For the first step, you have to run phpunit with the option --coverage-xml:

./vendor/bin/phpunit --coverage-xml target/coverage

This will create a directory called target/coverage, with a bunch of xml report files, including one file called index.xml


We can now create a script called phpunit-threshold.php that will parse this xml report:


 * phpunit-threshold.php
 * check if the coverage of phpunit is above specified threshold

if ($argc != 3) {
    echo "Usage: " . $argv[0] . " <path/to/index.xml> <threshold>\n";

$file = $argv[1];
$threshold = (double) $argv[2];

$coverage = simplexml_load_file($file);
$ratio = (double) $coverage->project->directory->totals->lines["percent"];

echo "Line coverage: $ratio%\n";
echo "Threshold: $threshold%\n";

if ($ratio < $threshold) {
    echo "FAILED!\n";

echo "SUCCESS!\n";

To use this script, we must indicate that path to index.xml as the first argument, and the desired coverage (in percent) as the second argument:

php phpunit-threshold.php target/coverage/index.xml 90


Now you can add this test to your .gitlab-ci.yml:

  image: cylab/php72
    # Install all project dependencies
    - COMPOSER_CACHE_DIR=composer-cache composer install
    - vendor/bin/phpunit --coverage-text --coverage-xml target/coverage-xml
    - php phpunit-threshold.php target/coverage-xml/index.xml 80