Generating Code Coverage with PHPUnit and phpdbg
In a previous post (in portuguese) I explained how to identify tests that are taking too long to execute. In this post, I’ll show you how to increase the performance of code coverage report generation using PHPUnit.
In the phpunit.xml file it’s possible to add configurations to generate reports related to the tests that are being executed. For example:
A directory named “tests/_reports” will be created and will contain a series of useful information. In the coverage-html directory we can see code test coverage details that facilitate the analysis. The file clover.xml is just another version of the same information to be processed by services such as Jenkins, CodeCov, Coveralls, Codacy, and others, to automate alerts and scripts.
In addition to changing the phpunit.xml file, to generate this information we also need to install the extension XDebug. However, by installing it we get a substantial decrease in performance. Check the test execution results without generating the reports:
Now, when we add XDebug and execute it again we get:
The execution time jumped from 1.08 to 22.26 minutes!
After some research online I found this post and decided to give phpdbg a try.
Since I’m using MacOS X for this test, I executed the commands below to install all the necessary dependencies.
The main difference is the parameter –with-phpdbg used in php7’s installation.
By following this post from Sebastian Bergmann, PHPUnit’s creator, I found the syntax to run the test again:
phpdbg -qrr ./vendor/bin/phpunit
1.59 minutes is much better than the 22.26 minutes it takes with XDebug.
Excited with the results I twitted the following, even before writing this post:
If you take a look at the comment, Derick Rethans is no one other than the creator of XDebug! Taking his comment into account, I compared the results generated by XDebug and phpdbg.
Below is the comparison of coverage-html generated by XDebug (left) and phpdbg (right).
I then used CodeCov to process the clover.xml file and the result was also a little bit different:
According to the report generated by phpdbg Planrockr has 66.05% of code coverage. XDebug’s report shows the code coverage at 65.92%
Some conclusions I draw from this small test:
- XDebug is an incredible tool and does a lot more than generate code coverage, so I’m not saying here that you should stop using it. I’m just comparing one of its many functionalities.
- I compared the default results, without making any adjustments to the configurations of XDebug or phpdbg, thus other scenarios can have different results.
- Although there is a slight difference between the two results, in my case, it does compensate to use phpdbg due to its better performance.
I’ll continue to use phpdbg for a while and if I happen to see any different results I’ll update this post or will write another one about the experience.
If by any means you think my conclusions are wrong, please don’t hesitate to contact me to let me know.
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMIfamily. We are now accepting submissions and happy to discuss advertising &sponsorship opportunities.
To learn more, read our about page, like/message us on Facebook, or simply, tweet/DM @HackerNoon.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!