Unit testing WordPress plugins

We’ve been unit testing some of our plugins using the old WordPress-tests framework and tips from this 2012 blog post. The good news is that the framework has since been incorporated into core WP, the bad news is that it was changed along the way, and it wasn’t exactly easy to get the test environment setup correctly for the old WordPress-tests.

I’ve had a feeling there must be a better way, and today I discovered there is. WP-CLI has plugin unit test scaffolding that’s easy to install. Pippin’s Plugins’ guide to the scaffold is helpful as well. My experience was pretty smooth, with the following caveats:

  • cd $(wp plugin path --dir my-plugin) is just another way of saying “cd into the plugin’s directory.” It’s good to see the example of how wpcli can be used that way, but way easier for me to type the path.
  • bin/install-wp-tests.sh came out with some unexpected permissions. I did a chmod 550 bin/install-wp-tests.sh and was a lot happier. It’s possible (perhaps likely) that I’m missing a sexy unix permissions trick there, and the permissions are intentionally non-executable for non-root users, but there’s no obvious documentation for that.
  • The bin/install-wp-tests.sh needs to run with a user that can create databases (probably root for many people). I’m usually pretty particular about this permission, but the convenience factor here depends on it.
  • The old framework expected -test.php to be the file suffix, the new approach expects the files to be prefixed with test-

All those are pretty minor, however. I think this approach will make it far easier to make tests distributable. The support for Travis (and from there to Github) is super sexy. All together, this should make tests easier to write and use.

Followup

I’ve added the scaffold to some of my most popular plugins:

Only bCMS has a meaningful test, written by Will Luo, but we’ll see where it goes from here. I’m still working out issues getting the test environment setup both locally and in Travis. Plugin dependencies, configuration, and git submodules are among the problems.