Code Coverage with PHPUnit
I am running into an issue while attempting to determine code coverage on our site. I have PHPUnit generating a html code coverage report while running our unit tests on our three apps. We have a public app, an admin app, and a reporting app. I'd like to somehow combine these into one code coverage report since the apps share code.
It seems that the code coverage is only calculating coverage on the files that the tests "touch", so completely untested files aren't being used in the calculation. Does anyone know how to pull these unused files into the calculation? Is there a way to tell the coverage generat开发者_运维问答or to calculate using certain directories so that it pulls in the files that aren't touched? I have a whitelist set up in the config file for phpunit set to the root of our project. All comments, answers, and bits of advice are welcome.
If you have set up a <whitelist>
in your phpunit.xml
configuration file you should see all the not covered files. It might an issue with the pathes, try absolute ones to see that it creates 0% coverage for some files and then make the relative pathses work.
For combining the coverage there is not much you can to with phpunit that i know of. You could combine the coverage results (PHPUnit_Coverage package) by hand and then figure out how to render them but you'd need to do that by hand. At least i don't know of any project/tool/way that does that for you.
The easiest way would be to run all your 3 testsuites in one phpunit run and have it generate the code coverage for the parts you are about.
Use <whitelist processUncoveredFilesFromWhitelist="true">
like so:
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">../folder/</directory>
</whitelist>
</filter>
I faced the same problem, and I just added generation of test stubs (for every class and public method) before running test suites. You have even option for that in phpUnit:
http://www.phpunit.de/manual/current/en/skeleton-generator.html
I think PHPUnit uses XDebug, which provides literally coverage data for files actually executed. It can't provide data for files never executed, because the debugger never sees them. You could hand-patch the PHPUnit machinery to add a list of all the files you believe are in your application, to the result returned from XDebug.
Our PHP Test Coverage tool works differently. You give it a list of all the files you think are part of your application; it helps you build this list. (This is the same list you'd have to patch into the first solution) It instruments all the files mentioned, and collects test coverage data. The coverage data you get now includes everything correctly. It should work fine with PHPUnit.
You can go further. You can create test coverage for each of your "seperate" applications, and run them separately. You can then combine the coverage data for all them, to see coverage for the overall set.
This might sound crazy, but why not just create a test case that loops recursively through your project folder and runs require_once on every file it finds? That should allow XDebug to create a new code coverage html file for each file required.
精彩评论