PHPUnit3 : first impressions

PHPUnit

I finally started writing unittests with my code.. That should get rid after yet another bad habbit.. I started using versioning and writing docblocks a few years ago, but shamefully I never really did proper testing.

For a testing framework I picked PHPUnit. My main reason for picking this was that its fully written in PHP5, it can produce some pretty good reports and the biggest reason is simply because I hear a lot of good stuff about it.

PHPUnit3 is well documented, which is helpful. I have to say that I did have a some trouble getting started. Even though its stated clear in the manual, I just sort of had to figure out where to put the files, how big the scope of one unit test is and when to write a separate testing suite..

So my solution was to actually look at the Zend Framework, and watch how they did it, which was really helpful..

One thing seemed a bit weird to me.. You use the ->addTestSuite method when you add a unittest.. and you use the ->addTest when your adding a whole suit of tests.. Seems a little reversed.

The other thing was that its actually quite some effort to add suits of tests.. Every single suit needs to have a fairly complex class that needs to be repeated every time. I'm thinking of writing this with a simple xml configuration file that includes unit tests, suits and can include other xml files. It should probably also be able to add tests based on wildcards.

But overall, a great testing framework. Especially the code coverage report rocks (albeit a big buggy;) )


7 Responses to PHPUnit3 : first impressions

  1. 445 Jack D. 2007-05-09 6:20 am

    In SimpleTest, you create a TestSuite object and only have to add the files containing your TestCases to this TestSuite. The rest is done automatically.
    When it comes to usability, SimpleTest is really a lot "simpler" (and imo better) than PHPUnit.

  2. 446 David Rodger 2007-05-09 7:45 am

    PHPUnit may have the feature too, although I haven't found any reference to it in the documentation I've looked at (admittedly not much). The nice thing about Simpletest is that it has CLI and HTML output. I prefer to run the tests in a browser, so the latter suits me. It also appears that the CLI output of PHPUnit is a bit cryptic whereas Simpletest tells you in words what happened.

  3. 444 Hans 2007-05-10 12:38 pm

    Take a look at the PHPUnit support in Phing (http://phing.info). Using PHPUnit from Phing removes a lot of the annoying stuff related to test-suites, etc. since you can use Phing's filesets to just include all the the unit test files you want to run. In other words, you wouldn't need to modify any suite files when you want to add a new phpunit testcase class to your environment.

  4. 447 Thomas Koch 2007-05-10 1:27 pm

    Sebastian Bergmann, the creator of PhpUnit is working in the eZ Components project. I've learned a lot looking how they organized there test suites and the whole development process.

  5. 443 Evert 2007-05-10 1:35 pm

    Hans, thats a good tip.. I used Phing before in the past, but didn't look at the integration features.. good stuff

    Thomas,
    I will check that out, thanks.

  6. 441 mackyman 2008-07-07 12:35 pm

    I have been trying to get started with PhpUnit, wich is a real pain in the ass for me, it might be me who is stupid... But I have tried UnitTest++ before, and it was as simple as it could be!
    The documentation in PhpUnit never even mentioned how to run the tests without the commandline, can you even do it without the commandline?

  7. 442 Evert 2008-07-07 1:18 pm

    Not sure Mackyman, but running them in a browser wouldn't make an awful lot of sense to me, as having just a few hundred tests in place can take a while to process.



About

My name is Evert, and I've been writing semi-regularly on this blog since 2006.

I'm currently available for contract work.

more info.

Subscribe

Dropbox

Dropbox is a simple cross-platform online backup and sync application. The first 2GB of space is free, and both you and me get an extra 250MB extra space if you sign up through this link.