WordCamp New York City 2009

November 14–15, 2009
...was awesome!

unit testing Tag archive

Can I Prove That This Works?

Photo of John Bintz

John Bintz

After hundreds, if not thousands, of times when my code has failed for any number of reasons, the first question that now comes out of my mouth before I sit down and craft a new block of code is:

Can I Prove That This Works?

If a user comes to me and says, “Your code is failing!” how can I prove that no, it’s not the code I wrote, it’s how it interacts with this other part, or that it’s user error, or that it’s some other reason? How can I quickly find the place to look for problems by ruling out chunks of code that I know are working? How do I know that, when I get something working, it will stay working?

When we all started programming, we proved that our code worked by writing the output to a console, to a browser, to some location where we had to visually verify that our inputs produced sane outputs. While this technique certainly does work, it doesn’t scale well at all. How do you prove that the core of your four thousand line plugin stays working, month after month, after you add new features, fix bugs, and re-factor code?

Lucky for us, we have the tools nowadays to be able to prove that our code does just what we intended it to do. WordPress is built upon PHP, and in PHP, we have multiple testing frameworks to choose from. These frameworks bring to us the ability to perform automated tests on our code, making sure it doesn’t break after we add new things, allowing us to re-factor safely, and letting us say, “The problem’s not here, because the tests just passed.

The trick is that, like any other habit, the idea of writing tests takes time to really sink in. When you start, you’ll find that, to make your code testable, you’ll have to completely rethink your approach to programming. You’ll become acutely aware of the problems of global variables. You’ll have to learn to write smaller functions and methods that do one — and only one — thing, since anything more becomes too hard to test. You’ll have to learn when a thing is a unit, a discrete component that does one thing, and when a thing becomes an integration, where you string a bunch of units together to make your code masterpiece work.

My session, Yes, Your Code Will Change, will walk you through what it’s like to unit test code that’s destined to be run on WordPress. It’s an introduction to the ideas of unit & regression testing, why these types of test are beneficial to you, and what it’s like to go through a simple testing situation by showing how you can use PHPUnit and MockPress to prove that your code works, even when — especially when — it has to change. If you write anything even remotely complex for WordPress, this is the session for you. Hope to see you there!

49ce41fdc95f9bfeb1673611775f042b6bd0acf3

Platinum Sponsors

Silver Sponsor

Bronze Sponsors

Small Business Sponsors



Mojofiti logo



Fusebox logo


Cacoo logo


Consultant Sponsor

Recent Posts

Post Categories

RSS #wcnyc

WordCampNYC has no relationship to public radio station WNYC, and we apologize if our abbreviation-based logo has caused any confusion.

Visit WordCamp Central


Code is Poetry.