Using php-code-coverage for PHP Code Coverage Analysis and CI Integration
The article introduces php-code-coverage, an open‑source library that collects, processes, and visualizes PHP code‑coverage data, explains its measurement metrics such as line, function, and class coverage, and shows how to integrate it with PHPUnit, Composer, and Jenkins for automated testing and quality assessment.
When testing PHP code, testers often wonder which newly added code should be covered by test cases and whether existing automated cases can cover all code branches for regression testing. php-code-coverage is an open‑source library that gathers, processes, and displays PHP code‑coverage information, available directly from GitHub.
The tool dynamically tracks PHP execution, merges results, and generates coverage reports in HTML, XML, or plain text, as well as XML formats compatible with Clover, Crap4J, and PHPUnit. These reports enable analysis of incremental coverage for new code, overall project coverage, and assessment of test‑case quality.
php-code-coverage supports several measurement methods:
Line Coverage: measures whether each executable line has been executed.
Function and Method Coverage: measures whether each function or method has been called.
Class and Trait Coverage: measures whether all methods of a class or trait have been covered.
The library is highly portable and works on Windows, Linux, and macOS. Adding it to a project is as simple as requiring phpunit/php-code-coverage in the composer.json file. PHPUnit’s whitelist feature can exclude files from coverage using the --whitelist option or configuration settings.
php-code-coverage relies on Xdebug’s coverage functionality and can be integrated into PHPUnit (version 3.3+), which itself can be embedded in IDEs such as NetBeans, providing real‑time coverage reports similar to those available for Ruby or Python.
In practice, the author integrated php-code-coverage’s API into a Jenkins CI pipeline, producing intuitive reports that display line, function, and class coverage, with clickable navigation to drill down into specific code sections. Sample screenshots illustrate the generated reports and the added test cases (over 10) that improved coverage.
References:
PHPUnit Manual – Code Coverage Analysis
NetBeans PHP Blog – Code Coverage for PHP
Baidu Intelligent Testing
Welcome to follow.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.