LogoPhly, boy, phly
the weblog and site of Matthew Weier O'Phinney

Thursday, December 28. 2006

Getting XDebug working with PHPUnit for code coverage reports

I've been playing with PHPUnit a lot of late, particularly with framework development. One thing that's always hard to determine is how well your code is exercised -- basically, how much of the code is tested in the unit tests?

In PHPUnit 3, you can now generate code coverage reports using XDebug, and the usage is very simple:

matthew@localhost:~/dev/zend/framework-svn/tests$ phpunit --report ~/tmp/report AllTests

The above command creates a coverage report directory 'report' under my tmp directory. You can then browse through the reports in a web browser and visually see which lines of code were executed during tests, and which were not, as well as a synopsis showing the percentage of coverage for any given file or directory -- useful stuff indeed!

So, what's the problem? Getting XDebug running.

The executive summary:

  • Enable the extension using zend_extension = /full/path/to/xdebug.so, not as extension = xdebug.so, in your php.ini
  • Use the setting xdebug.default_enable = Off in your php.ini.
  • If compiling using pecl or pear, make sure it compiles against the correct PHP; if not, hand compile it using:
    $ /path/to/phpize
    $ ./configure --with-php-config=/path/to/php-config
    $ make
    $ make install
    

For the detailed narrative, read on.

First off, I tried installing XDebug using pecl and pear. Even though my 'pear config-show' showed my correct PHP install and extension directory, for some reason it found the PHP 4.4.1 installation I have elsewhere in the filesystem, and it compiled against that. So, I followed the directions for compiling by hand, and all was mostly well. I discovered, however, that you need to specify the --with-php-config=/path/to/php-config switch to ensure that it uses the appropriate php-config (particularly if you have multiple PHP installs on your system).

Next up was getting it to work with PHP. I edited my php.ini file, and did a standard extension=xdebug.so. What was odd is that I then showed xdebug as present (using php -m), but not as a Zend extension. I tried zend_extension=xdebug.so, but then nothing showed. Then, in the end, I followed the instructions, and used zend_extension=/full/path/to/xdebug.so, and it was available.

Okay, let's test it out... I started running tests and... segmentation fault. Disabling the extension brought everything back to normal... only when enabled did the segmentation fault occur. I decided to look at the xdebug php.ini settings to see what I could find.

After some trial and error, I discovered that setting xdebug.default_enable = Off fixed the issue, and I was able to start generating some wonderful coverage reports.

Now, to write more tests...

Posted by Matthew Weier O'Phinney in PHP at 13:59 | Comments (8) | Trackback (1)

Trackbacks
Trackback specific URI for this entry

Getting XDebug working with PHPUnit for code coverage reports
Getting XDebug working with PHPUnit for code coverage reports I've been playing with PHPUnit a lot of late, particularly with framework development. One thing that's always hard to determine is how well your code is exercised — basically, how muc...
Weblog: Nio's Weblog
Tracked: Dec 29, 09:18

Comments
Display comments as (Linear | Threaded)

Where is my backtrace? :-) Xdebug should in no way segfault if you don't use .default_enable = Off...
#1 Derick (Link) on 2006-12-28 14:38 (Reply)
Hi Matthew -

I don't know if you use Zend Debugger (the server component of Zend Studio) but if you ever installed it, try to disable it - XDebug and Zend Debugger kind of collide with eachother and cause problems...
#2 Shahar (Link) on 2006-12-28 15:21 (Reply)
This clash is why I have a php-cli.ini now :-)

xdebug worked fine for me on Windows. Though I did update the ZF Testing Standards wiki page so that I could find my notes next time I reinstall!

Regards,

Rob...
#2.1 Rob... (Link) on 2006-12-29 15:00 (Reply)
Shahar -- I'm aware of the conflicts between XDebug and Zend Debugger. It would be nice if PHPUnit could support either one (or even APD) instead of requiring XDebug for this functionality.
#2.2 Matthew Weier O'Phinney (Link) on 2007-01-06 23:12 (Reply)
Fantastic - I had the same segmentation fault thanks!
#3 Chris Thomson on 2007-01-12 06:28 (Reply)
Wow! This will help me a ton ;-)
#4 Matt (Link) on 2007-02-02 22:51 (Reply)
Quick tip for everyone using Windows (for example, on development machines):

Use "zend_extension_ts" instead of "zend_extension"
#5 Matthew Ratzloff (Link) on 2007-03-26 20:47 (Reply)
Anyone know how to get the coverage to only report and a specified directory? it reports on my tests directory as well since it's run code and is causing very bad metrics on cruise control.

thanks in advance
#6 Marcus on 2008-01-04 16:56 (Reply)

Add Comment

Standard emoticons like :-) and ;-) are converted to images.
E-Mail addresses will not be displayed and will only be used for E-Mail notifications

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

 
 
  • Home
  • Resume
  • Blog
  • Phly PEAR Channel
  • Contact Me
  • About this site

ZCE

Zend Education Advisory Board Member

Add to Technorati Favorites

Calendar

Back October '08
Mon Tue Wed Thu Fri Sat Sun
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Quicksearch

Links

  • PHLY - PHp LibrarY
  • Paul M. Jones
  • Mike Naberezny
  • Shahar Evron
  • Planet PHP
  • Zend Where I now work
  • Garden.org Where I once worked

Archives

October 2008
September 2008
August 2008
Recent...
Older...

Categories

XML Linux
XML Personal
XML Aikido
XML Family
XML Programming
XML Dojo
XML Perl
XML PHP

All categories

Syndicate This Blog

XML RSS 0.91 feed
XML RSS 1.0 feed
XML RSS 2.0 feed
ATOM/XML ATOM 0.3 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Comments

Show tagged entries

xml best practices
xml books
xml conferences
xml dojo
xml dpc08
xml file_fortune
xml linux
xml mvc
xml oop
xml pear
xml personal
xml php
xml phpworks08
xml programming
xml ubuntu
xml webinar
xml zendcon
xml zendcon08
xml zend framework
© 2004 - present, Matthew Weier O'Phinney
matthew-web <at> weierophinney.net