I'm new to PHP development but have experience developing Python web applications. In Python, there is a package called Coverage that analyzes the code and identifies functionality that is missing Unit Tests.
Does such a package exist in the PHP world? I have searched Google and SO and come up short. Thank you for your help!
In computer science, code coverage is a measure used to describe the degree to which the source code of a program is tested by a particular test suite. A program with high code coverage has been more thoroughly tested and has a lower chance of containing software bugs than a program with low code coverage.
It isn't realistic -- or necessary -- to expect 100% code coverage through unit tests. The unit tests you create depend on business needs and the application or applications' complexity. Aim for 95% or higher coverage with unit tests for new application code.
How to Calculate Test Coverage. Calculating test coverage is actually fairly easy. You can simply take the number of lines that are covered by a test (any kind of test, across your whole testing strategy) and divide by the total number of lines in your application.
PHPUnit has coverage built in. You can generate a html coverage report by using
phpunit --coverage-html /[path where to save report]
Another option is --coverage-clover
instead of --coverage-html
. That will generate an xml report about what is covered.
If you use an advanced IDE like phpStorm you can just right click on the test and select "Run with coverage", it will display the coverage in the editor's file explorer.
PHPUnit itself has the coverage tool which uses PHP_CodeCoverage
This page shows all the different coverage options: https://phpunit.de/manual/current/en/textui.html
An example html output coverage command line would be:
phpunit ./report tests/*
This will create a folder called report and contain all the coverage for all the tests under tests/ folder
PHPUnit supports code coverage and is the de facto standard. Integrates with Jenkins et al.
https://phpunit.de/manual/current/en/code-coverage-analysis.html
Yes There are number of code coverage tools. Add below linkes to your phpunit.xml
<logging>
<log type="coverage-html" target="./mainreport" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80" />
</logging>
install XDEBUG , (ex: for ubuntu and php7 - sudo apt-get install php7.0-xdebug)
This will log your report to the directory specified in target attribute (target="./mainreport"). Also the report will be in html format
create a mainreport
directory in your root .
run the unit test
open index.html in browser and you can see coverage report .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With