Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code coverage is a great tool to find gaps in your code coverage and making sure your code is covered with tests. High code coverage does not guarantee great test quality so please make sure the tests are well written. At the end this will likely be more important than to cover every line of code.

PHPUnit and code coverage

Traditionally PHPUnit comes with support for generating code coverage reports in different formats.

...

To generate code coverage PHPUnit relies on either XDdebug or pcov. The performance of pcov is superior to XDebug but we will show how you can run it with both.

How to configure and generate code coverage reports

Initialise PHPUnit

The first step is to initialise the PHPUnit environment.

...

Code Block
php admin/tool/phpunit/cli/init.php

Whitelists

In order to generate code coverage you need to provide a whitelist in the phpunit.xml file otherwise you get an error when trying to generate the report.

...

Add the following to the phpunit.xml file directly after the </php>tag.

Totara 14 and higher

File: test/phpunit/phpunit.xml

Code Block
<coverage>
    <include>
        <directory suffix=".php">/var/www/html/server/totara/core/classes/local</directory>
        <directory suffix=".php">/var/www/html/server/totara/core/classes/task</directory>
        <file>/var/www/html/server/totara/core/classes/visibility_adviser.php</file>
    </include>
</coverage>

Totara 13

File: test/phpunit/phpunit.xml

Code Block
<filter>
    <whitelist>
        <directory suffix=".php">/var/www/html/server/totara/core/classes/local</directory>
        <directory suffix=".php">/var/www/html/server/totara/core/classes/task</directory>
        <file>/var/www/html/server/totara/core/classes/visibility_adviser.php</file>
    </whitelist>
</filter>

Totara 12

File: phpunit.xml

Same as for Totara 13 except that you have to add the processUncoveredFilesFromWhitelist="false" addUncoveredFilesFromWhitelist="false" attributes.

...

The more files are included in the whitelist the longer it takes to generate the coverage report. It is advised to limit it to the files / directories you are interested in.

Generating coverage

XDebug

Please make sure you have xdebug installed.

...

Please refer to the PHPUnit documentation for the available formats.

pcov

pcov is an alternative to XDebug. It is much more performant.

...

  1. Code Block
    [pcov]
    extension=pcov.so
    pcov.enabled=1
    pcov.exclude='~(vendor|tests|node_modules|.git|client|.scannerwork)~'
    pcov.initial.memory=1073741824
    pcov.initial.files=30000
  2. Generate the code coverage

    Code Block
    cd /path/to/code
    XDEBUG_MODE=off php -d pcov.directory=`pwd` vendor/bin/phpunit --filter=totara_core --coverage-html /var/www/html/coverage_report

PHPStorm and XDebug/pcov

PHPStorm has the ability to run code coverage anaylsis in product using XDebug or pcov. The analysis is automatically loaded into the platform and displayed both in the Coverage tool, and in-editor when looking at files. Their documentation on code coverage explains how to set it up.

...

  • You still need to define a whitelist

  • As a product we support several different versions of PHP - if you're using php-fpm and have them installed you can configure PHPStorm so that you can choose which version you run tests and coverage on

  • When looking at a testcase file you can click on the little arrows next to test case classes and test case methods and choose to run specific tests

Further reading