Menu

Unit test WordPress plugins like a ninja (in progress)

Unit testing a plugin can be easy, but if the plugin needs dashboard configuration or has dependencies on other plugins, it can quickly go off the tracks. And if you haven’t setup Travis integration, you’re missing out.

Activate Travis CI

To start with, go sign in to Travis now and activate your repos for testing. If you’re not already using Github to host the plugin, please start there.

Set the configuration

If your plugin needs options to be set that are typically set in the WP dashboard, do so in tests/bootstrap.php.

In bCMS, I’m doing a simple update_option( 'bcms_searchsmart', '1' ) just before loading the plugin code. For that plugin, that option is checked when loading the components. That’s not ideal, but it’s works for this plugin (until I refactor the plugin to solve the shortcomings this exposes).

Download and activate dependencies

Some plugins depend on others. An example is bStat, which depends on libraries from GO-UI. The dependency in that case is appropriate, but it can add frustration to unit testing. To solve that problem, I’ve made some changes to download the plugins in the Travis environment and activate them in all.

It starts with the tests/dependencies-array.php, where I’ve specified the plugins and their repo paths. That file is used both by bin/install-dependencies.php, which downloads the plugin in Travis, and tests/bootstrap.php, where the plugins are included at runtime.

Of course, if those additional plugins need configuration settings, then do that in the tests/bootstrap.php as in the section above.