Unit testing for plugin development

As an ex-software engineer building large business types who landed in an interactive agency let me give you a few thoughts on testing when developing for WordPress:

  1. Your Unit Testing should test the smallest amount of behavior that a class can perform. Each class should be able to be tested independently of WordPress. If you have a WordPress dependency consider creating mock objects and/or methods in your test to work around this. Ultimately, this will change how you write your code a bit — but it will be far more testable. As usual no free lunch.

  2. When you get up to the level of functional testing this is where you can test you code with WordPress dependencies. This is where you would actually test that your plugin actually performs a service it is supposed to.

  3. Depending on what your plugin does — consider using Selenium-based tests which test for the presence of data in the DOM by using IDs. Its reasonably robust and it is a lot less likely to break version to version. Again, the trick here is to think of testing as part of your software development lifecycle. Large changes in the code will cascade down to large changes in your tests. All part of the cost of doing business.

If you need any assistance with any of the tools to accomplish this — I would be happy to answer if you open up a new question. I make use of both PHPUnit and its selenium extension in the work I do.

Leave a Comment