Solar: Modified Testing Strategy

In the current release of Solar (version 0.10.0) I use the .phpt test format, which is supported very well by Greg BeaverTomas V.V. Cox's "pear run-tests" code. The .phpt testing strategy compares the output of a test script to an expected output string, and passes if the outputs match.

I noticed some time ago that Greg has adopted a testing strategy that combines .phpt with techniques found in PHPUnit and SimpleTest. These testing packages use assertions to compare method returns, rather than comparing output at the end of a test script. Greg's strategy uses assertions within .phpt tests: if there is no output, the test succeeds, but if an assertion fails, it generates unexpected output, which causes the .phpt test to fail.

(Aside: While true testing gurus may not think of this as a robust solution [hi Jason! hi Marcus! hi Sebastian!], I have thus far found it to be a "good-enough" approach that combines simplicity with ... well, more simplicity. :-)

Inspired by Greg's PEAR_PHPTest class, I've developed a bare-bones Solar_Test_Assert class that tests assertions and throws PHP5 exceptions on assertion failures. Add to that a quick hack to run all tests at once recursively through subdirectories (courtesy of the "pear run-tests -r" switch), and you get a convenient all-at-once testing tool for Solar. Thanks, Greg, for producing such useful tools.

I spent one day over the weekend rewriting all the existing Solar tests to use this new strategy, and met with great success. I did run into one problem, though. The class-method tests in the subdirectories use a common prepend file. Normally, you would think that "include '../prepend.php'" would work, but because the tests are running at the command line, PHP doesn't include ".." with respect to the subdirectory; instead, it tries to include ".." with respect to the current directory. This is the proper behavior, but it was causing every test to fail.

Mike Naberezny pointed out a cool trick to work around that behavior; instead of using "..", you can use "dirname(dirname(__FILE__))" to get to the next-higher directory. After applying this fix, all my tests ran from the main directory using "pear run-tests -r". Thanks, Mike.

Update: (2006-02-01) Greg pointed out that the "pear run-tests" is from Tomas V.V. Cox, so I update the above link. Thanks, Tomas, for this great testing tool.

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving your codebase, all while keeping it running the whole time.