Manual:PHP unit testing/Code coverage

This manual explains how to generate code coverage on your local workstation. This can be useful to understand how coverage works and to see progress on your work before submitting your change to Gerrit.

Ensure you have: Run the following script:
 * PHPUnit installed.
 * A code coverage driver installed (pcov is recommended, but other drivers like Xdebug can be used).

Viewing coverage reports
Coverage reports for MediaWiki core, extensions, and all of our separate libraries are published regularly on doc.wikimedia.org. (See the tutorial on how to add your extension to the list).

CoverMe provides an alternative view, sorting functions by how often they are called in Wikimedia production, helping identify functions that will benefit the most from additional test coverage.

Coverage scope
When running PHPUnit on a subset of files or sub directory, the tests are much quicker. However, building the coverage report may still take a long time.

By changing filter whitelist in  to match the scope of your test run, you can generate coverage reports much faster. Copy  to   in the root of your repository, then update the whitelist:

For example: In case the relevant file(s) are in a large directly (e.g. ), you can also specify individual files:

For extensions, you can use the composer script included with MediaWiki core to do this automatically:

PhpStorm
In PhpStorm, you can use the "Run {test} with Coverage" option, and you will see the coverage report directly in the editor. As defined above, make sure to edit the coverage whitelist in  first, doing so results in the coverage report being generated very quickly.

MediaWiki-Vagrant
You can view the coverage report at http://dev.wiki.local.wmftest.net:8080/w/docs/coverage/.

Use a shortcut:

You may get a permissions error about www-data being unable to write to this directory. In that case, try the following:

mediawiki-docker-dev
View the coverage report at http://default.web.mw.localhost:8080/mediawiki/docs/coverage/.