Tutorial: Introduction to Unit Testing in PHP with PHPUnit
It's a familiar situation: you've been developing on an application for hours and you feel like you've been going round in circles. You fix one bug and another pops up. Sometimes, it's the same one you found 30 minutes ago, and sometimes it's new, but you just know it's related. For most developers, debugging means either clicking around on the site, or putting in a whole load of debugging statements to hunt the problem down.
You've been there, right? You've had these same frustrations with all your applications, and have sat back and thought that there has to be a better way. Well, you're in luck! There is, and it's not as difficult as you might think it. Unit testing your application will not only save you a lot of headaches during development, but it can result in code that's easier to maintain, allowing you to make more fearless changes (like major refactoring) without hesitation.
The key to understanding unit testing is to define what we mean by a€ounit.a€¯ A unit is simply a chunk of functionality that performs a specific action where you can test the outcome. A unit test, then, is a sanity check to make sure that the chunk of functionality does what it's supposed to.
Once you've written up your set of tests, whenever you make a change to your code, all you have to do is run the set of tests and watch everything pass. That way, you can be reassured that you haven't inadvertently broken another part of your application.
Debunking Unit-testing Myths
I'm sure you're sitting there thinking, a€oif this unit testing stuff is so awesome, why doesn't everyone do it for all their applications?a€¯ There are a few answers to that question, but none of them really are very good excuses. Let's run through the common objections, and explain why they're far from compelling reasons to avoid writing tests.
It Takes Too Long
One of the largest concerns about writing up tests is that they just take too much time to generate. Sure, some of the IDEs out there will autogenerate a set of basic tests for you; but sitting down and writing good complete tests for your code takes some time. Like many best practices in development, a little investment of time to do tasks the right way can save you a lot of time over the life of your project. Writing a solid test suite is definitely one of those cases. Moreover, you're probably already testing your code by visiting your site and clicking around every time you add new features. Running an established test suite can be much faster than manually testing all your functionality.
There's No Need to Test: My Code Already Works!
Another common statement you hear from developers about writing up a set of tests is that the application works, so there's no real need to test it. They know the application, and they know right where to drop in and fix a bug, sometimes in seconds. But drop a shiny new developer into the cubicle next to them, and you might start to see why those tests would be a good idea. Without them, the newbie could go changing code without a care in the world, and break who knows what. With a repeatable test run, some of those bugs could be avoided.
It's No Fun
The final reason why developers don't like writing tests is that it's just not much fun to write them. Developers, by their nature, want to solve problems. Writing code is like forming something out of nothing, creating order out of chaos to make something useful. As a result, they see writing tests as boring-a task they might get to one of these days if they have time after the real work is done. So they miss out on the value testing has even during development (especially during development!) to keep the process consistent and clean. Look at it this way: no one thinks that wasting hours chasing a pernicious bug is fun, and testing enables you to put in a little effort up front to avoid a lot of frustration down the track.
Now we get to the good part-a practical example that you can sink your teeth into. For the sake of my examples, I'm only going to use one of the more popular unit-testing tools out there,A PHPUnit. This amazing application was developed by Sebastian Bergmann, and it provides an excellent set of features to help make testing your code a snap.
If you're yet to install PHPUnit, the simplest way to grab it is from its PEAR channel.pear channel-discover pear.phpunit.de pear channel-discover components.ez.no pear channel-discover pear.symfony-project.com pear install phpunit/PHPUnit
If all goes well, you'll have all the tools you need installed. The PEAR installer will grab any dependenci
Truncated by Planet PHP, read more at the original (another 14658 bytes)