PHPUnit code coverage report
If you want to sleep without worry about your code you should write tests. The most popular test framework in PHP is called PHPUnit. That is an advanced product with a very good documentation. In the new version, it’s even faster and more efficient than before. Today I show you how can you create PHPUnit code coverage report. I will definitely use this with my “Image optimizer API” project.
Why generate a report?
The best practice is to write tests before you start to write code, but some time must we work with old code or with code from someone else. If you have huge project can you not sure which code is already tested.
When you use with PHPUnits with legacy code then you definitely want to see how many codes tested you already too. That can you generate an HTML report and check it or presents someone the results.
In this all case you need code coverage report. Best in a readable form such as HTML. PHPUnit can generate it for you.
TIP! You don’t have to coverage 100% code with tests. It does not make sense. You should have hight percentage code coverage and focus on the most important piece of code. That should reduce the risk of errors.
How to generate the code coverage report
To generate the report need you XDebug debugger too, so first you have to install this tool. That is not complicated. I won’t copy an XDebug manual. All the information can you find here:
Now can you generate yours first report. You must in your console write:
./vendor/bin/phpunit --coverage-html coverage-folder
That’s it. Now can you check a result in your browser. You should see a page some like this:
The instruction how to read a report find you on PHPUnit site:
I hope that from today testing will become easier for you.