Unit testing in PHP

20 Dec 2007

I needed to do some unit testing for some PHP5 utility classes, and wanted some unit test framework. Here are my requirements:

  • Simple. If it's more than one file, something is wrong.
  • Unrestrictive license. I should be able to include the one file in any package I create for distribution, give to clients, whatever.
  • Short namespace. I don't want to type SomeBloatedClass::testIfTrueOnTruesdayOnly(...), when something like assert("foo" == "bar") will do.
  • Easy to run, no need for anything more than a command line

Oddly I couldn't find anything. Lots of links to outdated information. Lots of monster web framework testing things. PHPUnit is baffling. I found out of date versions, links to PEAR that didn't work, etc. And it's huge! Why? Unit testing should be short and sweet.

So screw it: here's my 100-line version. It does just about everything the fat ones do. Here's the "manual" (more samples in source file)

  • Add require_once('UnitTest.php');
  • Create a class that extends UnitTest
  • Make a test by adding a method to the class. The method name must start with test
  • Test values in the test method with assert and assertEquals($val, $expected)
  • Optionally define a public function setup(), public function teardown() functions. These will be called before and after each test.
  • Add at the bottom unittest('classname')
  • Run test with php yourfile
  • Script will return 0, if all tests succeeded, 1 if failures occurred, and 2 if the class couldn't even be loaded

For example:

#!/usr/bin/env php
<?php
require_once('UnitTest.php');

class ATest extends UnitTest
{
 
    public function testPass()
    {
        assert(1 == 1);
    }

    public function testNot()
    {
        assert(1 == 2);
    }

    public function testFail()
    {
        assertEquals("foobar", 10);
        assertEquals("foobar", 10, "optional message goes here";
    }

    public function testError()
    {
        throw new Exception("wtf");
    }
}


// RUN IT
unittest('ATest');
?>

Running this will produce:

$  ./sample.php 
testPass: OK
testNot: Fail : Assertion failed @ line 63 in ATest::testNot
testFail: Fail : foobar != 10 @ line 63 in ATest::testFail
testError: Error : wtf @ line 24 in filename
Total Tests: 4
total = 4, pass = 1, fail = 2, error = 1
$ echo $?
1

Yeah the output isn't so pretty. So... change it! Need more fancy methods? Add 'em! You can modify the base, or subclass it. need "Test Suites" ... use directories!

And go forth and make unit tests!

Oh yeah, improvements and suggestions welcome. One more method might be assertEqualsFloat which would take 2 floats and a tolerance.