Testing WordPress Plugins with WP-CLI and PHPUnit

Everyone agrees that extensive testing of WordPress plugins is necessary. The problem is that the tests we run as developers don’t take into consideration certain edge cases and rely on our experience to determine what needs to be tested. Testing is also time-consuming and quite frankly not very fun. Automated testing is recommended to ensure quality and with PHP we are lucky to have the PHPUnit framework at our disposal.

Using WP-CLI to setup and install test suite

That said, PHPUnit isn’t particularly easy to setup and requires cloning an environment and then destroying it. Again, there is a handy tool at our disposal. If you are running a local site with VVV then you are all set. Otherwise you will have to install PHPUnit. The version you install will depend on the version of PHP you have running on your server. PHPUnit 5.x can be installed here (download link). For setting it up WP-CLI once again comes to the rescue. If you need help setting up or installing WP-CLI I encourage you to start either with the documentation or with this short article. With one simple command you can set up your own PHPUnit test suite.

wp scaffold plugin-tests your-plugin-slug

Here, you will get two folders. The first one, bin, contains a shell script to create the testing infrastructure consisting of a separate temporary WordPress installation. The second, test, contains a bootstrap file that looks for the WP_TESTS_DIR environment variable. It also includes your first sample test. This folder is where you will store your unit tests. You will also get a phpunit.xml file and a .travis.yml file. Feel free to look at them to better understand what is going on… or don’t.

Installing the test environment on your server

In order to install your test suite you will need both wget and svn. You can install them on your Linux server if you don’t already have them available.

sudo apt-get install subversion 
sudo apt-get install wget

Followed by:

. bin/install-wp-tests.sh wordpress_test <user> <password> <host> <wordpress-version>

You should then be able to run phpunit and get

OK (1 test, 1 assertion)

Writing the tests

It is now up to you to write your own tests. Here the PHPUnit Documentation is your best friend. I won’t go into writing the tests in detail but here are a few concrete examples taken from a plugin I am testing.


class MyPluginTest extends WP_UnitTestCase {

    function setup() {
        require_once 'my-plugin.php';

    function test_truth() {
        $this->assertTrue( 1 >= 0 );

    function test_string_contains_word() {
        $string = mp_admin_notice();
        $this->assertContains( 'requires', $string);

    function test_is_string(){
        $string = mp_admin_notice();
        $this->assertInternalType('string', $string);


There are some good references (listed below) for writing unit tests for your plugin but as the tests you write are specific to the functionality of your plugin you will have to do some experimenting.

I hope this article was helpful and as always, feel free to ask questions or comment below.

Leave a Reply

Your email address will not be published.

Click to view allowed tags

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.