Code Coverage I

Introduction to code coverage and tips for installing code coverage tools

In the previous article in this series, we reviewed the unit testing articles and touched on some design points. In this one, discuss PhpUnit's code coverage measures look at setting up Codeception to produce code coverage reports.

MODX logo

This article assumes that you've installed and configured Codeception and PhpUnit and created the classes and unit tests described in the previous articles. The test files and classes are available at GitHub here.

Code Coverage

Code coverage is a measure of the extent to which you are testing all the parts of your code. There are a number of measures of code coverage. The ones we'll be looking at are line coverage, function and method coverage, and class and trait coverage. We'll look at these in more detail in later articles, but here's a brief description:

  • Line Coverage — The percentage of executable lines in your classes that are executed in your tests.
  • Function and Method Coverage — The percentage of functions and methods that are executed in your tests. A function or method is only considered covered if its line coverage is 100%.
  • Class and Trait Coverage — The percentage of your classes that are fully tested. To be fully tested, The Line Coverage and Function and Method Coverage must both be 100%.


The Xdebug extension to PHP (or some other debugger) is required to get code coverage reports. If you are using XAMPP locally, Xdebug is already there, but must be enabled in the php.ini file (found in the xampp/php directory in recent versions of XAMPP). Look in that file for these lines (this is for Windows):


The first line may not match the one above. Don't change it!. Make sure none of these lines are commented out (comments begin with a semicolon (;). The xdebug.remote_host line should contain the name of your local server. If there is a conflict, you may have to change the port number.

If you don't have Xdebug, or need to configure it for another platform, see this page.

There's also some good information on adding Xdebug to XAMPP installations that don't have it here

After installing and configuring Xdebug, create a .php file with this code and run it in a browser:


Look for the Xdebug section. If it's there, Xdebug has been successfully installed.

Important: Delete the file (or comment out the PhpInfo() ) line when you're finished. It provides too much information for hackers.

Codeception Setup

I like to use Codeception for code coverage runs. It's pre-configured to produce very nice code coverage reports and the setup is fairly simple.

Edit the codeception.yml file in your _build directory. Add these lines at the end (preserve the indentation):

    enabled: true
    low_limit: 35
    high_limit: 70

        - tests/unit/*
        - ../core/model/*
        - tests/unit/_bootstrap.php

In the configuration code above, we've enabled coverage and listed the files we want to include and exclude. The - tests/unit/* line includes all our test files. The line after that includes all our class files (you may need to adjust the path). Including the test files is optional, but will make sure all the lines of our test files are executing, and we'll be using one of them as an example in the next article.

PhpUnit Code Coverage Module

Look in your composer.json and composer.lock files, one or both should contain a line like this:

"phpunit/php-code-coverage": "^6.0",

If you don't find it, install it with:

composer require phpunit/php-code-coverage: ^6.0

Running with Coverage

To generate the code coverage report, issue this command in the _build directory:

codecept run unit --coverage --coverage-html

That command will generate a code coverage report that includes all the tests and classes we listed in the codeception.yml file.

At the end of the output, you'll see a summary of the code coverage results. If the percentages are all 0%, something is not configured correctly.

There is also a --coverage-xml option that generates the reports in XML format, though we won't be using it. Some third-party applications can use the .xml reports, but they're beyond the scope of these articles.

You can also run code coverage tests in PhpStorm, but I find it cumbersome and difficult to configure. Since the tests generate reports that have to be looked at in a browser, I prefer to run them from the command line.

If the code coverage test was successful, you should see a bunch of new files in the tests/_output/coverage directory. Look for the index.php file in that directory and open it in your browser. You should see a nice graphic representation of the report in color, complete with bar graphs and percentages. In the next article well see how to navigate it and update some of our tests and classes to improve the code coverage.

Coming Up

In the next article, we'll see navigate through the code coverage report and make some changes in our test and class files to improve the coverage.

For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.