What is Unit Testing?
A non-technical definition of Unit Testing, is “a way to make sure your code won’t break stuff.” In the WordPress world, it means using a provided library of tests to check if your code will return errors. Basically, it’s a program that you put your code files into, and when you run it, you will be notified if there are problems.
It’s important to know that WordPress Unit Testing is typically done with PHPUnit. You’ll see that name thrown around here and there when researching this topic.
Why Automated Testing?
Holy crap, WHY NOT?
In the past, I wasted a lot of time clicking back and forth between posts and settings pages to ensure that my new plugin or theme wouldn’t throw any errors in a WordPress install. Now, you can just type a few commands and your code will be analyzed quickly and thoroughly. Automated Unit Testing lets you quickly check your code in many, many different contexts and scenarios.
Unit Testing has proven invaluable to me in my attempts to write patches for WordPress Core. One time I ran some code I thought would work, and it broke WordPress in 9 other places. Luckily I was able to test it before I submitted it for review, right?
Unit Testing can also reveal unexpected output. Recently, automated testing revealed to me something I didn’t expect to see: when typical date formats were passed into a function, it executed perfectly, but when just one other date format was sent through, it spit out data I hadn’t accounted for. I’m not sure I would have caught that error on my own. It would have required running my code by hand many times and many ways.
Typically, testing looks like this:
- Edit code.
- Switch to browser.
- Input data (e.g., create a new post/comment, ativate a plugin, change admin settings, etc).
- Cross fingers.
- Check 80,987,096 places for error messages.
- Hope you didn’t miss anything.
- Repeat 9,464,643,943 times, just to be sure.
- Spend the rest of your day answering support questions about errors caused by your last Plugin.
The automated testing process looks more like this:
- Edit code.
- Switch to terminal.
- Start unit testing (
- Make a glass of ice tea.
- Wait for Test Results.
How to Test your WordPress App, Theme, Plugin, or Patch
Tom McFarlin wrote a great series on this topic, so I won’t waste time and space trying to explain it all over again (and probably in an inferior way). Check his article out on WPTuts+: The Beginner’s Guide to Unit Testing: What Is Unit Testing? Specifically, check out the section titled “Preparing the Environment.” It will help get you started.
Unit testing is very important. It is way faster and way more thorough than you are. It is powerful and will save you a lot of headaches and embarassment when your code goes live. You need to get into Unit Testing.
So, what has been your experience with Unit Testing? What tips do you have for those getting started? Share your thoughts in the comments!
If you’re ready to get to the meat and potatoes, check out the next post, “Tutorial: Writing a Unit Test for WordPress.”