subscribe

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;) )

Web mentions

Comments

  • Jack D.

    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.
  • David Rodger

    David Rodger

    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.
  • Hans

    Hans

    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.
  • Thomas Koch

    Thomas Koch

    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.
  • Evert

    Evert

    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.
  • mackyman

    mackyman

    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?
  • Evert

    Evert

    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.