How to Set Up Unit Testing for a WordPress Plugin

This is a quick rundown of the commands used and resources needed to start unit testing a WordPress plugin. It is a fairly straight forward guide and does not include much explanation for each step. If you want a more detailed description of what is going on under the hood, check out the link to Pippin’s tutorial in the credits at the bottom of this article.

Please note: This tutorial was written for use with the Linux operating system. While it may also work on a Mac, I haven’t tested it with any other systems.

Install WP-CLI

The documentation for installing WP-CLI is available here: http://wp-cli.org/#installing

Install PHPUnit

Until WordPress adds support for PHPUnit 6, we’ll need to use PHPUnit 5.7 (PHPUnit 6 support is being worked on here: https://core.trac.wordpress.org/ticket/39822)

The documentation for installing PHPUnit 5.7 is located here: https://phpunit.de/manual/5.7/en/installation.html

Prepare Unit Tests

One of the handy things about using WP-CLI in this process is that it can generate the files we need to run our unit tests. There are two parts to this process. First, we will create the files that will hold our tests. To generate these files, we’ll navigate to the root directory of our WordPress installation and run the following command in the terminal (remember to replace “your-plugin-slug” with the directory name of your plugin):

wp scaffold plugin-tests your-plugin-slug

Second, we will set up everything else we’ll need to run the unit tests. The following command will install a copy of WordPress in the /tmp directory, create a database for this new installation of WordPress to use, and install the unit testing tools.  There are a number different parts to the this command, so let me explain them before giving the command itself.

  • wordpress_test – This is the name of the database that will be created.
  • root – Our MySQL username.
  • password – Our MySQL password.
  • localhost – The MySQL server host.
  • lastest – The version of WordPress to install.

We can now navigate to our plugin directory and enter the following command in the terminal (replacing the details to match our server environment):

bash bin/install-wp-tests.sh wordpress_test root password localhost latest

Please note: this command requires subversion to be installed on your system in order to properly download certain files.

Run the Sample Unit Test

To run the tests associated with our plugin, we can navigate to our plugin folder in the terminal and run the following command:

phpunit

If everything is installed properly, the command line output should include this line:

OK (1 test, 1 assertion)

Credits

I want to thank Pippin for his excellent article, Unit Tests for WordPress Plugins – Setting Up Our Testing Suite. My tutorial was derived heavily from his work, with updated information to reflect some of the changes that have occurred since his article was published. If you want a more in-depth explanation of each step of the process, please read his article.