Chapter 16: Unit Testing Framework


One of the main benefits of structured, modular programming in any language is that the components you develop will nearly always be self-contained. They can be cleanly taken away from their mother project and worked on in their own right, and this includes testing.

Making sure that a component does exactly what you want it to do is an essential step in application development. As you will have already learned elsewhere in this book, it is vital that components are tested before they are integrated with the bigger picture application. Always write the application around the components. Never write your components around your application.

For slightly more complex classes (or even hierarchies of several classes), devising a suitable test strategy can appear quite tricky. The temptation to resort to spitting out huge chunks of data with var_dump() to the error log can be overwhelming, but you should resist it because you can save yourself a great deal of time by using a prewritten Unit Testing Framework. Not only that, but a coherent, thorough, and professional approach to testing is something you can actively demonstrate to your clients (or your boss), whereas having hundreds of error_log() statements is not.

There is indeed such a framework available for PHP, a semi-official one called PHPUnit. In this chapter, you'll learn not only how to install and use PHPUnit but also why it's so useful on a day-to-day basis. At the end of the chapter, we cook up a class with some deliberate but hard-to-spot logic errors and use PHPUnit to fix it.

Professional PHP5 (Programmer to Programmer Series)
Professional PHP5 (Programmer to Programmer Series)
Year: 2003
Pages: 182
BUY ON AMAZON © 2008-2017.
If you may any questions please contact us: