Unit testing plugins with different versions of WordPress

The nasty scenario: Your plugin depends on some WordPress functionality that doesn’t work as expected in another WP version. It’s possible to run unit tests under different versions of WordPress.

Ollie Armstrong has published a great post about getting started with unit tests, Unit Testing WordPress Plugins: The Right Way. The post covers installing PHPUnit, getting the WordPress test suite, and setting up the testing framework for your plugin.

I wrote in an earlier post about how to install different versions of WordPress on your system.

Now, to get started, we need to modify the wp-tests-config.php file a little bit:

Replace:

define( 'ABSPATH', dirname( __FILE__ ) . '/src/' );

with:

if ( ! defined( 'ABSPATH' ) )
	define( 'ABSPATH', dirname( __FILE__ ) . '/src/' );

This allows us to define our own path before the test runner reads this config.

Now, in the directory where you want to run your tests, you should already have a file called phpunit.xml, the configuration file for PHPUnit for your plugin’s unit tests. You also should have a bootstrap.php file, probably in a tests subdirectory.

Let’s suppose you installed a copy of WordPress 3.6.1 and that it is located in C:\wamp\www\wordpress-3.6. (I think for a real life case it’s better to use the “.1” maintenance releases as I would expect my plugin’s end-users to download them.)

We need to create a PHPUnit configuration file to override the default source directory. Copy the contents of phpunit.xml to a new file called phpunit-wp36.xml, and modify:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
        [...]
        bootstrap="tests/bootstrap-wp36.php"
        [...]
</phpunit>

Create a file called bootstrap-wp36.php in the tests subdirectory with the following contents:

<?php
define( 'ABSPATH', "C:\\wamp\\www\\wordpress-3.6\\" );
require( dirname(__FILE__) . '/bootstrap.php' );

Place the path to your alternative WP installation in the ABSPATH definition.

Now, let’s get ready to test if the correct WordPress version is loaded. Let’s do that by creating a test file, called WP_Version_Test.php under the tests subdirectory:

<?php
class WP_Version_Test extends WP_UnitTestCase {
	public function test_version() {
		global $wp_version;
		echo "\n\nThe WordPress version is $wp_version.";
	}
}

Now, to run the your test(s) using the desired WordPress version, you need to tell PHPUnit to use the appropriate configuration file. Let’s test the WordPress version using the (rather silly) test case created above:

C:\wamp\www\wp-content\plugins\your-plugin-dir>phpunit -c phpunit-wp36.xml ./tests/WP_Version_Test.php

You should get output like this:

Installing...
Running as single site... To run multisite, use -c tests/phpunit/multisite.xml
Not running ajax tests... To execute these, use --group ajax.
PHPUnit 3.7.28 by Sebastian Bergmann.

Configuration read from C:\wamp\www\wp-content\plugins\your-plugin-dir\phpun
it-wp36.xml

.

The WordPress version is 3.6.1.

Time: 1.01 seconds, Memory: 39.50Mb

OK (1 test, 0 assertions)

Now you can just go and run your own unit tests under that other WordPress version using the -c parameter to load the custom configuration file:

C:\wamp\www\wp-content\plugins\your-plugin-dir>phpunit -c phpunit-wp36.xml ./tests/Your_Plugin_Test.php

With one xml configuration file pointing to one php bootstrap file per installation, you can switch the WordPress version easily.

Advertisements
Tagged , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: