To measure code coverage with PHPUnit, you can follow these steps:
composer global require phpunit/phpunit
phpunit.xml
configuration file. Add or modify the <logging>
section as follows:<phpunit ...>
...
<logging>
<log type="coverage-html" target="coverage/" title="Code Coverage"/>
<log type="coverage-clover" target="coverage.xml"/>
</logging>
...
</phpunit>
This will generate an HTML report in the coverage/
directory and a Clover XML report in coverage.xml
.
phpunit --coverage-html coverage/
This command will run your tests and generate an HTML coverage report in the coverage/
directory. You can open the index.html
file in your browser to view the report.
Additionally, the Clover XML report can be used by other tools for further analysis or integration with build systems.
Note: PHPUnit's code coverage feature relies on the Xdebug extension being installed and enabled. Ensure that Xdebug is correctly installed and configured on your system for accurate code coverage measurements.
That's it! You have now set up code coverage measurement with PHPUnit and can use the generated reports to identify areas for improvement in your test suite.