Test Driven Development is one of the best strategies for building solid WordPress plugins that are easy to maintain and that is bootstrapped to live a long life in the WordPress ecosystem. There are many variations as to how to implement the approach, but they all have a common principle: Define tests first, then code.
So the first step before starting WordPress plugin development is to get a test environment up and running. This article will cover how to get ready for Unit testing on a local XAMPP development server using PHP Unit.
First, a short definition of unit test that I’ve paraphrased from the Wikipedia article on the topic
a software testing method by which individual units of source code, i.e. the smallest testable part of an application, are tested to determine whether they are fit for use.
In plugin development this usually would be methods or functions.
This article will cover
- Installing XAMPP
- Installing PHPUnit
- Setting up unit tests in a WordPress plugin directory
Installing XAMPP and PHPUnit
This is the easiest part of this small tutorial. Head on over to Apache Friends and download a copy of the XAMPP installer. PHPUnit ships with XAMPP, so once installed, you have both installed. Follow the installation Wizard to set it up on your computer. The rest of the article assumes that XAMPP has been installed in c:/xampp. Next, add the XAMPP PHP directory to the path variable. The directory would be c./xampp/php. To verify that phpunit is properly installed, open up the command line and type
phpunit –version
The expected result would be
PHPUnit X.Y.Z by Sebastian Bergman
Set up the files needed to run plugin unit test
The rest of this article covers how to get and set up the files needed to run WP unit test.
Final directory setup:
- phpunit.xml
- tests
- wordpress-tests-lib
- includes
- <Files from WP unit test>
- src
- wp-tests-config.php
- bootstap.php
- test-*.php
Create the directory structure needed
All tests and associated files will be located inside the plugin folder. To start with, create a directory within your plugin called tests, and then a subdirectory called wordpress-tests-lib. Tests will be the main folder where all test cases will be located. WordPress-tests-lib will contain the libraries that we depend on to do WordPress unit tests. Lets start with those.
Get the unit test scripts from the WordPress development repository
The first thing needed is the scripts that are used to do WordPress unit tests. These are located in the development repository in the tests folder. To download the files, use the following subversion command.
svn co https://develop.svn.wordpress.org/tags/4.4.2/tests/phpunit/includes/ tests/wordpress-tests-lib
Also, get the config sample file, and store it in tests/wordpress-tests-lib as wp-tests-config.php. Replace the database info at the bottom with your own. Make sure that the database you provide is empty.
Setup PHPUnit to run tests
The first ting you need is the phpunit XML file that tells PHP unit where your tests and your scripts are. I reccomend getting a copy of the config file used by WP-CLI and store it as phpunit.xml in your plugin directory. The file points to a file called tests/bootstrap.php. This is a link to the file that bootstraps your plugin for testing. This file should look something like this.
<?php
require_once 'wordpress-tests-lib/includes/functions.php';
function _manually_load_plugin() {
require dirname( dirname( __FILE__ ) ) . '/mainpluginfile.php';
}
tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );
require_once 'wordpress-tests-lib/includes/bootstrap.php';
?>
Basically, what it does is that it required the WordPress test scripts and plugs the plugin into a hook to make sure it is loaded.
Write a test, and you are ready to go
All tests should be kept in files named test-SOMENAME.php in the tests folder. An example test would look like:
<?php
class SampleTest extends WP_UnitTestCase {
function test_sample_true() {
// replace this with some actual testing code
$this->assertTrue( true );
}
function test_sample_false() {
// replace this with some actual testing code
$this->assertTrue( false );
}
}
?>
Run the test by executing running phpunit in your plugin folder.