Testing Wordpress Themes and Plugins in Bitbucket Pipelines

Written by aminebenkeroum | Published 2017/09/29
Tech Story Tags: php | wordpress | testing | docker | deployment-pipelines

TLDRvia the TL;DR App

The past few days, me and my colleagues were figuring out the best way to test Wordpress plugins and themes, these tests should be able to run on our CI environment, and make sure everything fit the box before deploying somewhere, this is mainly why I decided to share this, it may help a lot of developers looking for shortcuts and don’t want to waste time.

WordPress ships and supports a unit testing framework, but only for core development. However with a bit of curiosity, you can use it for Themes and Plugins testing.

Step 1: Install PHPUnit

If you use composer, you can simply install PHPUnit using the commands:

curl -sS https://getcomposer.org/installer | php — — install-dir=/usr/local/bin — filename=composer

or you can install the binary in your local environment by using:

wget https://phar.phpunit.de/phpunit.pharchmod +x phpunit.pharmv phpunit.phar /usr/local/bin/phpunit

Once you have PHPUnit installed, you should be able to run the command from the terminal directly :

phpunit — configuration=path/phpunit.xml

or from your vendor :

./vendor/bin/phpunit — configuration=path/phpunit.xml

Step 2: Get the WordPress framework and configure your test database

Wordpress bundles its core Framework in an svn repo. We can download just the tools we want by checking out the includes directory.

It’s up to you where you want to organize your testing library. For our case, we will assume you clone it into a “tests” folder inside your root WordPress directory.

Make sure you pick the files matching your Wordpress Core version, for our case it was Wordpress 4.3, go to terminal and type:

svn co develop.svn.wordpress.org/branches/4.3/tests/phpunit/includes/

You will need a second Wordpress Config file wp-tests-config.php, where you should configure your database and Wordpress site informations, here is a sample:

https://develop.svn.wordpress.org/trunk/wp-tests-config-sample.php

Note : Make sure you use another database, because Wordpress drop and recreate tables as a part of the testing process.

In Mysql Host, we have 2 different hosts, “mysql” and “127.0.0.1”, so we made a little change to the Config file like this:

$remote = “mysql”;

if(getenv(‘IS_PIPELINES’))

$remote = “127.0.0.1”;

if we are running tests from our Docker container, we should be able to connect using “mysql” host, but it’s not the case in Pipelines as we define a mysql service and the rules are different, please see https://confluence.atlassian.com/bitbucket/test-with-databases-in-bitbucket-pipelines-856697462.html

Step 3: Configure your tests

to do so, we will create a file named “phpunit.xml” where we will put our tests setup, no matter where you put it because you can always specify that in your command.

<phpunitbootstrap=“./tests/includes/bootstrap.php"backupGlobals="false"colors="true"convertErrorsToExceptions="true"convertNoticesToExceptions="true"convertWarningsToExceptions="true"><testsuites><testsuite><directory prefix="test-" suffix=".php">./tests/</directory></testsuite></testsuites></phpunit>

We will be running every tests files with “test-” prefix inside the “./tests/” folder.

Now with this config, you will be running tests on Wordpress Core only, but if you need to run tests on your own Plugins and Themes, we are going to create our own bootstrap file:

<?php

require_once dirname( dirname( __FILE__ ) ) . ‘/tests/includes/functions.php’;

function _manually_load_environment() {

// Add your theme …

switch_theme(‘my_theme’);

// Update array with plugins to include …

$plugins_to_active = array(

‘your-plugin/your-plugin.php’

);

update_option( ‘active_plugins’, $plugins_to_active );

}

tests_add_filter( ‘muplugins_loaded’, ‘_manually_load_environment’ );

require dirname( dirname( __FILE__ ) ) . ‘/tests/includes/bootstrap.php’;

Now, I’m loading my main theme, and the plugins I need to get my Wordpress working.

and don’t forget to point on our new bootstrap file on phpunit.xml, like this:

bootstrap="./bootstrap.php"

backupGlobals="false"

...

Step four: Writing tests

You are now ready to go, write your first unit test like the example bellow, checking if the main theme is loaded correctly.

<?php

class VerificationTest extends WP_UnitTestCase {

function testTheme() {

   $this->assertTrue( ‘my\_theme’ == wp\_get\_theme() );

}

}

Running phpunit command, you should see the following informations about your tests:

Step 5: It’s all about CI ( Bitbucket Pipelines )

Well, I think we figured out the business, we just have to adjust this to runnable builds inside Bitbucket reading documentation, I used a “mysql” service for our test database.

Note that you should use the same credentials you mentioned in your wp-tests-config.php file.

I hope this will be useful to you, your comments are welcomed if there is anything that can be discussed or corrected.

Thank you 😃


Published by HackerNoon on 2017/09/29