4

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 generator 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.

halfer
  • 19,824
  • 17
  • 99
  • 186
Dave
  • 155
  • 1
  • 2
  • 10
  • 1
    The obvious answer is to writes tests that invoke the untouched files. – Ira Baxter Apr 20 '11 at 23:26
  • This is true, but I'm trying to avoid that as I would like to get a relatively accurate code coverage percent in order to set goals. The PHPUnit docs online seem to give the impression that adding these files to the whitelist will include them, but I'm not too sure. – Dave Apr 21 '11 at 00:51
  • I dont know if I really understand your question or not, but why do you want to include the untouched files in your code coverage report? e.g. if there is a script A which has nothing to do (hence untouched) while running the testX, why should script A be included in the code coverage report??? – Obaid Maroof Mar 20 '14 at 13:29

5 Answers5

5

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.

edorian
  • 38,542
  • 15
  • 125
  • 143
  • Yeah the issue is I have three separate apps they run in. So it has to be three separate phpunit calls. Thank you for the info on the whitelist I will double check my paths. – Dave Apr 21 '11 at 16:05
  • And why can't you run all those three apps in one php process? – edorian Apr 23 '11 at 03:52
  • we have a phpunit-admin to run the admin app, a phpunit-reporting for our reporting app. – Dave Apr 25 '11 at 16:03
  • You have custom wrapper scripts around your phpunit call or are those .xml files or what ? :) Sorry you are not really giving me something to work with here except for saying "Create a phpunit-all that runs all apps" – edorian Apr 25 '11 at 21:53
  • yes wrapper scripts which basically just define a set of constants for that app. Each app require the constants to be set differently. – Dave Apr 28 '11 at 16:34
2

Use <whitelist processUncoveredFilesFromWhitelist="true"> like so:

<filter>
  <whitelist processUncoveredFilesFromWhitelist="true">
    <directory suffix=".php">../folder/</directory>
  </whitelist>
</filter>
Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
0

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

ts.
  • 10,510
  • 7
  • 47
  • 73
  • Did not know about PHPUnit being able to generate skeleton code! Thanks for that! It looks like I will still run into the issue of our apps sharing code and generating separate code coverage reports. – Dave Apr 22 '11 at 16:10
  • @Dave Dohr : you should run then three suites in one script and use PHP_CodeCoverage::merge() to join data from those instances, then generate HTML/Clover report – ts. Apr 22 '11 at 22:13
  • I am running the suites in an ant script with cruise control. How would I go about doing this? Googled PHP_CodeCoverage::merge() to no avail. – Dave Apr 25 '11 at 16:05
-1

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.

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 3
    PHPUnit also supports listing code that is not run in its coverage reports, just add it to the whitelist in your `phpunit.xml`. Stop spreading FUD. – cweiske Apr 30 '11 at 11:51
-1

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.

james
  • 3,543
  • 8
  • 31
  • 39
  • Brute force! Would this actually work? If so it may be a quick fix until I can figure something less "hacky" out. Thanks James! – Dave Apr 22 '11 at 16:17
  • Also, we are using the symfony framework, some of our actions that are being tested currently have duplicate names. (e.g. listAction) We have a class name mangler that we use on our site to avoid some problems associated with this. Won't running require_once cause errors if the filenames are the same? – Dave Apr 22 '11 at 17:56
  • Same filename in different directories isn't a problem. – Edson Medina Oct 11 '12 at 15:05
  • The problem with this approach is that some files might contain executable code (ie: outside of methods/functions) that you might not want to execute – Edson Medina Oct 11 '12 at 15:06
  • hey guys I said it was crazy! – james Dec 17 '12 at 19:42