Integration Automated Testing Tool for WordPress Plugin/Theme using PHPUnit

Automated Testing Tool for WordPress Plugin/Theme using PHPUnit

Creating WordPress plugins could be a challenging task as it needs constant monitoring of the performance in terms of how it functional with the WordPress. A plugin needs proper testing to ensure that it works fine as an individual unit in the WordPress ecosystem. This means that a plugin may perform well on its own, but a small error may backfire its functionality on the website. This small loophole may result in a havoc situation where an incorrect piece of code may run into a snowball effect and bring down the entire website.

There are multiple testing frameworks, but PHPUnit is considered to be the best of the lot and is often touted as the official, defacto standard for WordPress. As WordPress is open source, the developer community is widespread and comes up with new themes and plugins on a daily basis. They may not go through unit testing and this may result in a faulty WordPress plugin. To avoid this, a genuine practice of unit testing through PHPUnit is advisable.

Unit testing is done to check a block of code for an expected functionality. It weeds out errors in the beginning stages to prevent issues cropping up during the production stages. A well-tested plugin will pose fewer errors and would be easy to integrate and even customize. There are plenty of benefits of debugging especially automated testing and unit testing.

Major Benefits of Automated Testing:

1) Spotting bugs at the initial stages

By detecting errors during development stages, it becomes easy to correct them compared to when they are deployed. Also, correcting errors in an entire website with the incorporated plugin, would be tough as developers need to work on the implementation level issues. Therefore, rule them out in budding stages to save time and energy in the future.

2) Optimum level of software development

Automate testing is efficient at correcting the loopholes presented by manual tests. These written scripts can be run to detect any coding errors and weed them out from the extension. It improves accuracy of the entire software and ensures that the plugin is bug free.

3) Logging errors for quick solutions

Every test script prepared by WordPress developers can be easily checked through automated testing. The visual logs can be checked to spot the status and error in the script. Automated testing also hints how to fix a certain error which helps the developers to fix the problem.

When components are fixed in isolation, they need to go through integrated automatic testing to check if they work well in the entire system, which could be a website, CMS, eCommerce site or more.

PHPUnit contains library to test chunks or blocks of code.

To start, WordPress developers require 3 things.

Step 1 – To install PHPUnit you require composer. It can be download on this official site

1) The following command helps to install PHPUnit.

composer global require phpunit/phpunit:5.*

Please note that we have run this command to install 5.x version as it is recommended by the PHP7.0 and above.

2) Run phpunit –version to verify that it’s been installed.

Step 2 -WP-CLI is required to perform PHPUnit test with the WordPress plugin/theme.

The following are the steps help to install WP-CLI.

1) – Use the below commands to install WP-CLI:

curl -O

chmod +x wp-cli.phar

sudo mv wp-cli.phar /usr/local/bin/wp

2) – Run wp –info to verify to verify that it has been installed.

As both the pre-requisites are acquired, we will start the unit test for the plugin.

Step 3 -The following are the steps help to run or install PHPUnit demo.

1. Change your terminal’s directory to the root of your WordPress installation, and run the command below to generate the plugin test files.

wp scaffold plugin-tests php-unit-demo

Below is what the structure of the plugin will look like after the command above generates the test files.


Note: By default, the wp scaffold plugin-tests command generates a Travis CI configuration file. You can specify a –ci flag to generate a configuration file for the CI service you use, like so: wp scaffold plugin-tests –c gitlab. As at the time of writing, only Travis CI, CircleCI and GitLab CI are supported.

2. Change your terminal’s directory to your plugin’s directory, and run the installation script:

cd /var/www/html/php-unit-demo
bin/ wordpress_test root “” localhost latest

If you’re like me, then your MySQL username is not root, and the password is not empty. For example, suppose the username is homestead and the password is secret. You would run the installation script like:

bin/ wordpress_test homestead ‘secret’ localhost latest

3. Run the phpunit command to run the default test in tests/test-sample.php


Create a test-php-unit-demo.php file in the tests folder. It will contain our plugin tests with the following setUp class.


class WP_Demo_Test extends WP_UnitTestCase {

public function setUp() {


$this->class_instance = new WP_Demo_Admin();


public function test_email_validation() {}

public function test_email_exists() {}


It is worth noting that in order for a method to be considered a unit test, it must be prefixed with test. A best practice is to add a Test suffix to every test class, although it is not required. See WP_Demo_Test .

Confused about what setUp() does? Just know that PHPUnit runs it once before each test method (and on fresh instances) of the test case class. There is also tearDown(), but it is run after each test method. There are also setUpBeforeClass() and tearDownAfterClass(), which run before and after each test case, respectively. A test case is basically a class that contains a number of test methods. See the WordPress Handbook and the PHPUnit documentation for more information.

From the class above, it is pretty obvious we are going to be writing tests for the  test_email_validation and  test_email_exists methods of our plugin class.

public function test_email_validation() {

$is_email_valid = $this→class_instance→is_email_valid( ‘[email protected]’ );

$this→assertTrue( $is_email_valid, ‘Message here…’ );


public function test_email_exists() {

$is_email_ exists = $this→class_instance→email_exists( ‘[email protected]’ );

$this→assertFalse( $is_email_exists, ‘Message here…’ );


Basically, the tests will ensure that both methods return the correct meta tag when Google and Bing webmaster verification codes are passed to them as arguments.

Run phpunit, and you should see an output in terminal screen.

Recent Articles

Browse some of our latest articles...