subscribe

PHPUnit: A second look

PHPUnit

Somewhere in 2007 I had a deep dive into PHPUnit, and there were a couple of things that bugged me.

Looking into it again, it turns out that since then everything has been fixed, making it perfect for integrating it into sabredav. Most of the protocol-level WebDAV stuff is all tested with litmus, but having good unit tests will help ensuring a high quality of the inner business logic. Already it has identified 2 spelling mistakes :).

My highlights:

  • No need to write 'TestSuites' anymore.
  • A 'bootstrap' setting. This file contains all the application specific logic to setup the test.
  • The Code coverage analysis is perfected.
  • The ability to specify which directories to include in the code coverage.
  • As a bonus, all these settings can be specified through a single XML configuration file. This way I can simply tell users to run 'phpunit --configuration=config.xml' and they're off..

Web mentions

Comments

  • Matthew Weier O'Phinney

    Matthew Weier O'Phinney

    If you name your configuration file "phpunit.xml", you don't even need to specify the --configuration argument. :)
  • Dell Sala

    Dell Sala

    Glad to hear someone else has had a problem with PHPUnit's suite API. "Totally Backwards" is exactly how I describe it. And adding new tests to your suites is very verbose, with lots of boiler plate code.

    It is helpful that the command line runner now supports targeting a directory of tests so you don't have to manually build out huge suite scripts. However, if you use this technique you have to give up suite-level organization altogether, and suite-level setUp and tearDown.

    I've been really excited about getting into testing, mainly because of this guy: http://misko.hevery.com/ But PHPUnit's suite API has been a thorn in my side.

    Out of frustration I implementing a suite builder that automatically adds tests that are in the same directory as the suite, and recursively in any sub-directories. If it hits a child suite, it just adds it and stops searching sub-directories.

    You use the builder to implement the suite() factory method of each suite class. Just 2 lines of code. All the configuration comes from the location of the suite source file in your test directory:

    $builder = new TestSuiteBuilder();
    return $builder->build(__CLASS__,dirname(__FILE__));
  • Evert

    Evert

    Great tip Matthew!

    Dell, thus far I've been able to living without these advanced features, but I can see how that could become annoying..

  • Stuardo -StR- Rodríguez

    Stuardo -StR- Rodríguez

    There is no documentation about the bootstrap in PHPUnit.

    Where did you learn how to use it?
  • Evert

    Evert

    Stuardo,

    Simply check out the xml configuration file document. The bootstrap file is simply included() so you don't have to do any OOP stuff.

    I use it to set up include_dirs, create temporary directories and such.
  • Snowcore

    Snowcore

    I like PHPUnit, but I so frustrated of new Zend_Tool component in Zend Framework. It is incompatible with last version of PHPUnit (3.4)