, ,

What Smart Developers Know About PHP Unit Testing

phptesting

Imagine your toughest class in college. You had a choice: take notes and study throughout the semester—or wait until the last minute to cram before the final exam. Studying little by little might have taken up time when you’d have rather been out with friends. But by the time the final rolled around, you had a bank of clean, organized notes to use as a study guide. You probably sailed through the exam, no problem.

Or you went the other route. Skipped assigned reading and blew off study sessions. Left the hard work for when it really counted: finals week. Problem was, since you weren’t building knowledge and checking your understanding over time, you didn’t even know where to begin. You were starting from scratch—and between caffeine-fueled all-nighters and potential nervous breakdowns, you spent the same amount of time at work as the person who studied all along (although your final grade might have said otherwise).

I won’t ask which student you are in this scenario—but it’s pretty clear which is the better strategy, right? Put in a little extra effort now to make life easier later.

This is a lot like the choice developers have to make between unit testing and overall testing of their code.

Unit testing: Getting down to the nitty-gritty

Unit testing means isolating the smallest possible elements of a program’s source code, then testing them one by one to be sure they work like they should. It’s different than the overall testing you probably do just before releasing software, which simply tests that a program behaves correctly.

With unit testing, if there’s an error anywhere in the program, you don’t have to work back through all of your code to sniff it out. You can see which test failed, and from there, isolate your error and make a quick fix.

One of the more popular unit testing tools out there is PHPUnit, used to test code written in PHP. PHPUnit saves all of the unit tests you’ve previously written, making your code easier to maintain over time. (Think of unit tests like lecture notes you can refer to before a big exam.)

Just say “no” to skipping unit tests

Surely this is a part of every developer’s coding toolkit, right? Not exactly. Just like some students choose cramming over studying in chunks, many developers shrug at the idea of unit-testing their code. Why? Chris Cornutt at Sitepoint offers a few reasons: They think it takes too long. Or that their code is fine the way it is. Or that it’s just plain boring.

Those things may be true. But think about the alternative: You’re down to the wire and ready to send a program to a client—and you discover a bug. You have no way to figure out when or how it popped up, nor any way to verify exactly which parts of your code it affects. Better hope you’ve got some Red Bull handy, because you’re in for a long night.

Refactoring without tears

The other great thing about unit testing is that it makes major code changes, or refactoring, a breeze. For many developers, refactoring can feel like playing Russian roulette with your code. You don’t want to make too many changes for fear of undoing your hard work.

But if you’ve been adding unit tests to your test suite all along, refactoring is no big deal. Simply update your code, and then run your old unit tests against the changes you’ve just made. If any tests fail, you know exactly what to fix.

It’s the surest way to get your clients to give you an “A.”

Still not convinced that unit testing is the way to go? Learn more about PHPUnit here, or listen to this talk by Professor Richard Buckland on why smart programmers test in units.

Need code that works right, every time? Contact Atomic for help with your next web development project.

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *