21. May 2007, 11:10, by Maarten Manders

Testing Framework for PHPUnit3

Welcome to tilllate’s first English contribution. In order to get this blog onto PlanetPHP there will be a series of English and PHP-only posts. My name is Maarten Manders and I’m one of tilllate’s newest members in the family. You might know me from Sitepoint or Webtuesday.

The PHPUnit3 manual says: “One of the goals of PHPUnit is that tests should be composable.” This can be done using The PHPUnit test suites.

$suite->addTestSuite(‘Second Test’);

// running suite

Deadweight code

Quite simple so far. In chapter 6, it gives an example how to organize your tests suites. Here’s an AllTests.php:

// …

return $suite;

if (PHPUnit_MAIN_METHOD == ‘AllTests::main’) {

And here’s an example of one component’s test suite.

// …

return $suite;

if (PHPUnit_MAIN_METHOD == ‘Framework_AllTests::main’) {

Not so simple anymore. Unfortunately, there’s a lot of deadweight code in both files:

  • Too many includes. Shouldn’t we autoload in one way or another?
  • PHPUnit_MAIN_METHOD checks seem hacky to me
  • Recursive suite() methods seem elegant but need a lot of attention when adding/removing tests

Even the test files themselves have all the includes and MAIN_METHOD checks. Clearly, the intention was that one can run a test or a testing suite by executing php MyClassTest.php in the console. That’s why we’re hacking together unit tests wrapped in a lot of verbose and repetitive code. Isn’t there a better way?

Roll your own?

Luckily, PHPUnit3 doesn’t force you to use their proposed testing architecture. Just create your own PHPUnit_Framework_TestSuite and add your tests the way you like. So how about creating a smart testing architecture that automatically finds and adds the needed tests, loads appropriate libraries and runs them? By default it would run all tests it can find but we’d certainly need a way to define testing packages or run single tests, like this:

# Run all tests
php Tester.php

# Run just a few by command line
php Tester.php -f MyFramwork_*

# Run them by config
php Tester.php -c MyFramework.conf

# Run a single test
php Tester.php -r MyFramework_Package_ClassTest

Solution: Zend Framework’s Test Manager

So has it been done before? After googling a bit without any reasonable result (what keywords to search for, by the way?) I started writing this post. Eventually, I found something that looks quite like that. It’s called ZFTestManager and belongs to the Zend Framework. Quite embarassing since I’ve been working with ZF for the past two months… ;-)

Did you have the same issue? Do you know any other solutions? I will report about my experiences with ZFTestManager.

Filed under: PHP,Programming,Web Development


  1. At local.ch I use a combination of these things with SimpleTest:
    * SelectiveReporter
    * glob
    * addTestFile

    The code looks more or less like that:
    $test = &new TestSuite(‘All tests’);
    foreach (glob($testDir . ‘/*.php’) as $file) {
    $result = $test->run(new SelectiveReporter(new MyTextReporter(), $onlyCase, $onlyTest));

    $onlyCase is a param from the command line for executing only a specific case by the class name. $onlyTest is an additional param for only executing a specific method inside that class.

    Comment by patrice — 21. May 2007 @ 15:53

  2. The “PHPUnit_MAIN_METHOD” thing looks like it’s attempting to be python’s

    if __name__ == ‘__main__':
    # do stuff…

    Think there is a need for _something_ like so it’s easy to execute a single test script on it’s own, as well as a whole suite. In SimpleTest that tends to look like;

    if (!defined(‘TEST_RUNNING’)) {
    define(‘TEST_RUNNING’, true);
    // run only the tests in this script…

    For example http://phputf8.cvs.sourceforge.net/phputf8/utf8/tests/cases/utf8_strrev.test.php?view=markup – at the same time I have a “test runner” which uses a similar approach to patrice’s – file naming convention to detect tests – http://phputf8.cvs.sourceforge.net/phputf8/utf8/tests/runtests.php?view=markup

    Welcome ;)

    Comment by Harry Fuecks — 22. May 2007 @ 00:33

  3. […] Maarten’s post at Tillate finally brought the motivation to document the PHP testing approach we use at local.ch. […]

    Pingback by PHP Testing with SimpleTest — 22. May 2007 @ 10:23

  4. Voilà, I have now posted the way we do it at local.ch: http://weblog.patrice.ch/articles/2007/05/22/php-testing-with-simpletest

    Comment by patrice — 22. May 2007 @ 10:24

  5. […] Recently, I was wondering about a way to simplify unit test management. I finally stumbled over Zend Frameworks Test Manager and promised to report about my experiences. […]

    Pingback by techblog.tilllate.com » So what does ZFTestManager do? — 7. June 2007 @ 16:25

RSS feed for comments on this post.

Sorry, the comment form is closed at this time.

© 2017 tilllate Schweiz AG - Powered by WordPress