Quickly setup WP Plugin Unit Tests

I am finding wp-cli increasingly useful. For those who haven’t come across it before—as the name suggests—it is a command line interface for WordPress. This in itself is fairly useful as a standalone tool for blog management (just run wp to see the possibilities!). For developers it also has a handy feature for bootstrapping plugin unit tests.

Writing tests often feels like hard work. wp-cli aims to aid the situation for WordPress developers by trying to remove as many obstacles as possible.

Assuming you have wp-cli all setup and you’re in your WordPress project directory creating an initial testing framework is as easy as running a few commands:

  • wp scaffold plugin-tests your-plugin-name-here – this creates a skeleton phpunit.xml file, a sample test file in tests/test-sample.php, a bootstrap file to load WordPress and the plugin for testing plus a useful shell script for setting up the test environment in bin/install-wp-tests.sh. That’s a lot of useful helping-hands in getting setup.
  • Next up we need to run the install-wp-tests.sh script. This requires MySQL credentials to create a copy of your database, which will be used for testing.
  • Write an actual test case for your plugin.
Get an error about SVN when you run the install script? You’ll need subversion to be installed. sudo apt-get install subversion if you’re using ubuntu.
Getting errors about the tagged versions of WordPress when setting up the testing framework? Check you’re running v 0.20.2 or newer.
Dev WordPress
Back to posts