Testing Wordpress Themes and Plugins in Bitbucket Pipelines

Amine Benkeroum
HackerNoon.com
4 min readSep 29, 2017

--

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:

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

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

or from your vendor :

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:

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:

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:

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.

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:

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:

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.

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 😃

--

--

Amine Benkeroum
HackerNoon.com

A twenty-something developer from Morocco, love innovation, startups, new technologies and meet smart people to improve my skills and build new technologies.