15

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!

5 Answers 5

14

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.

Sign up to request clarification or add additional context in comments.

1 Comment

For the terminal just run phpunit --coverage-text.
4

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

Comments

3

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

Comments

3

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 .

1 Comment

<log type="coverage-html" target="./mainreport" lowUpperBound="50" highLowerBound="80" /> Worked for me
0

Also you can generate Cases Report by checking how many cases are pass or fail in your project module Wise using json file.

Add this code into your phpnit.xml file :

<log type="json" target="./log/jsonreport.json" charset="UTF-8"/>

After run, generated file (jsonreport.json) of passed case list should exist.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.