Because PHPUnit is derived from xUnit and that's how xUnit does it.
Why does xUnit do it that way? I'm glad you asked. The original reason, as Robert points out, is that xUnit comes from Smalltalk and was popularized by JUnit in Java. Both are OO-or-nothing languages so they had no choice.
This is not to say there are not other advantages. OO tests can be inherited. This means if you want to test a subclass you can run all the parent's tests and just override the handful of test methods for the behaviors you've changed. This gives you excellent coverage of subclasses without having to duplicate test code.
Its easy to add and override assert methods in PHPUnit. Just subclass PHPUnit_Framework_TestCase
, write your own assert
methods and have your test classes inherit from your new subclass. You can also write default setup
and teardown
methods.
Finally, it guarantees that the test framework's methods aren't going to clash with the thing that they're testing. If the test framework just dumped its functions into the test and you wanted to test something that had a setup
method... well you're in trouble.
That said, I hear your pain. A big test framework can be annoying and cumbersome and brittle. Perl doesn't use an xUnit style, it uses a procedural style with short test function names. See Test::More for an example. Behind the scenes it does just what you suggested, there's a singleton test instance object which all the functions use. There's also a hybrid procedural assert functions with OO test methods module called Test::Class which does the best of both worlds.
Considering that PHP's syntax is so ugly for calling methods
I guess you don't like the ->
. I suggest you learn to live with it. OO PHP is so much nicer than the alternative.