Code Coverage with PHPUnitSeleniumTestcase Code Coverage with PHPUnitSeleniumTestcase selenium selenium

Code Coverage with PHPUnitSeleniumTestcase


Integration or Functional tests with Selenium aren't covering code inasmuch as they're covering behavior. Code coverage reports for tests like this aren't going to generate any sort of useful information. Unit tests will generate much more meaningful code coverage reports. The tests are being run based on information provided to and from Selenium, it's not really testing your "code" so to speak.


I think frosty's answer could be made even stronger, but I'm such a total noob with both PHPUnit and Selenium that I'm not completely sure of what I'm saying. So I'll say it and see if I get corrected.

Unit tests exercise your application code under the direct control of PHPUnit. You give PHPUnit the method in your code to invoke, and it invokes that method under Xdebug to gather the coverage information. I think of it as having your code running in the same address space as PHPUnit, even though that might not be strictly true - does anybody know if it is?

With tests run under Selenium, your code is not directly under the control of PHPUnit at all. Instead of a method in your code, you give PHPUnit a URL, and it arranges to feed that URL to a real web browser. The web browser itself need not be running on the same host machine as PHPUnit; and even if it is, your application code being tested runs on the webserver designated by the URL. Ain't no way PHPUnit can tell Firefox to tell the server handling a request that if handling the request invokes PHP, then run that PHP code under Xdebug and send trace output back along with the response! PHPUnit only gets to see the URL you specified and the output from the web browser that serviced the request. It has no way to find out what code the webserver handling the request actually ran.

So where the previous answer said that code coverage reports for these tests wouldn't provide useful information, and that unit tests would generate more meaningful reports, I'd go all the way to say that it's not possible for these tests to measure code coverage at all, so you should not ask for code coverage reports when you run them! Or rather that if you do generate code coverage reports for selenium tests, and the reports say that even one line of your code ran, then something is seriously wrong with your setup.