Blog Posts

Visual Code Coverage with SimpleTest

... and a little bit help of PHPUnit2.

For our CMS, we use SimpleTest as testing framework. We used it in other projects, and as you should never change a running team, we sticked to that for Flux CMS as well. And the WebTesting classes are just great for something webbased (besides the "traditional" unit test classes provided by SimpleTest).

But one thing I saw over at PHPUnit2 and badly wanted to have, was the Code-Coverage Analysis. The pure coverage data comes from Xdebug PHPUnit2 "just" displays it nicely based on that. A quick look into the code revealed that it was easy to just use the CodeCoverage classes and so I integrated it in our testing framework. The output looks then something like this:

200601051024

As you can see, we almost covered all lines of the makeUri method :)

And the needed code for adding CodeCoverage output basically was:

if (function_exists("xdebug_start_code_coverage")) {
    xdebug_start_code_coverage();
}
$ret = $psuite->run($preporter);
if (function_exists("xdebug_start_code_coverage")) {
    $cc =  PHPUnit2_Util_CodeCoverage_Renderer::factory('html',array('tests' => xdebug_get_code_coverage()));
    $cc->renderToFile('cov.html');
}

Thanks for the great jobs to Sebastian, Derick and Marcus. And Andreas for the glue of the testing framework.

Related Entries:
- Wsunit - the first release
- Liip @ linuxwochen.at
- A workaround for the missing git submodule support in Bamboo 3.1
- webtuesday: Selenium

About the author

Comments [6]

k7k0, 05.01.2006 19:42 CEST

Great! Thanks!
Note: For linux users the fist parameter of ' factory' must be 'HTML' (in uppercase).

perrick, 07.01.2006 02:00 CEST

You may want to have a look at http://developer.spikesource.com/projects/phpcoverage/ as well.

Sebastian, 16.05.2007 15:42 CEST

Any Idea if you can release a patch for selenium or a downloadable piece of code ?

Andrew, 27.08.2008 20:46 CEST

In which file(s) did you need to add the code for the CodeCoverage output?

Sorry if this is a stupid question. I'm still trying to wrap my head around OO PHP, testing, coverage, using Aptana and SimpleTest, and the Zend Framework. =]

Chregu, 27.08.2008 20:51 CEST

Andrew: That was a long time ago, I really can't remember.

Search for
$ret = $psuite->run($preporter);
somewhere in your code and you maybe find it :)

badr, 26.03.2012 11:01 CEST

hi , where can i put this code in the simpletest framwork ?

Add a comment

Your email adress will never be published. Comment spam will be deleted!