Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Unit Testing Coverage

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!

like image 770
nicholas Avatar asked Oct 15 '14 15:10

nicholas


People also ask

What is PHP code coverage?

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.

How much should a unit test cover?

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 do you test for coverage?

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.


4 Answers

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.

like image 61
thesolidphp Avatar answered Oct 24 '22 18:10

thesolidphp


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

like image 33
Diego Fu Avatar answered Oct 24 '22 17:10

Diego Fu


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

like image 3
bcmcfc Avatar answered Oct 24 '22 18:10

bcmcfc


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 .

like image 3
User123456 Avatar answered Oct 24 '22 18:10

User123456