Power PHP Testing
by Chris Shiflett
Related link: http://shiflett.org/archive/176
The tutorial that Geoff Young and I gave at ApacheCon has sparked some discussion (mostly via email) that I think will lead to better testing tools for PHP developers. A PDF of our slides is now available:
- Power PHP Testing (PDF)
One of the tarballs demonstrates how to use the Simple-Test testing library within the Apache-Test testing framework. This is thanks to the work of Mike Lively, who has documented his work in his blog:
- I Think I Finally Understand Apache-Test (Mon, 25 Apr 2005)
- TAP Format Explained (Wed, 04 May 2005)
- Implementing Apache-Test Support into Simple-Test (Thu, 05 May 2005)
- Updated Simple-Test + Apache-Test (Thu, 15 Dec 2005)
His most recent tarball contains everything you need to use these two tools together.
After we mentioned TAP (Test Anything Protocol) to Sebastian Bergmann, he added TAP support to PHPUnit. Now, at least conceptually, you can also use PHPUnit to write your tests. This gives PHP developers three choices for writing tests within the Apache-Test framework:
- The bundled PHP port of Test::More
- Simple-Test with the TAP Reporter
- PHPUnit with the TAP Logger
We've also been discussing the various advantages and disadvantages of each tool as well as how we might be able to help make testing easier for PHP developers. One of the perspectives I've been highlighting is best stated by Matthew Weier O'Phinney in his blog:
I find writing the tests tedious. In Simple-Test, as in PHPUnit, you need to create a class that sets up the testing harness, and then you create a method for each test you wish to run, and so on. I found it incredibly time consuming.
I don't think Matthew knew about Apache-Test's Test::More library (it's as simple and straightforward as phpt tests), but this illustrates one of the disadvantages of testing tools that require a lot of overhead - they raise the barrier of entry (and they don't really fit in with the "PHP way" of solving problems as simply and directly as possible). This was one of the reasons why I ported Perl's Test::More library to PHP - it's very simple and doesn't get in your way. It also works with many mature testing tools already available (such as Apache-Test), because it's TAP-compliant.
Note: Sebastian says he's working on adding phpt support to PHPUnit in an attempt to lower the barrier of entry.
The Perl community has long had a very powerful unit-testing tool available: Test::Harness and friends. It uses what the Perl people call TAP - the Test Anything Protocol. I've used the Perl framework myself to verify correct behaviour in Perl modules and the Apache Web server. It came as a surprise to me that there was apparently no port of the technology to PHP, and so I've done some work toward correcting that. This session will include an introduction to the technology, a description of the implementation, and examples of how it can be used to test PHP applications.
Ken is a few years late to the party, but I think his interest highlights the usefulness of these tools. (He's been notified that this work has already been done, so hopefully it can save him some wasted time.) With any luck, Ken will not only talk about the PHP port of Test::More but also the TAP support that is now available in Simple-Test and PHPUnit.
Do you test your PHP applications?