Skip to content

Instantly share code, notes, and snippets.

@dnaber-de
Last active November 9, 2016 13:55
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dnaber-de/a62ddc41be3fd512c76b to your computer and use it in GitHub Desktop.
Save dnaber-de/a62ddc41be3fd512c76b to your computer and use it in GitHub Desktop.
Short description on how I prepared sucessfully a unit test for wordpress plugin.

Plugin UnitTest – How To

Assuming you have a separate WordPress installation for your project (eg. /var/www/my-project ) with your plugin you want to test in /var/www/my-project/wp-content/plugins/my-plugin. PHPUnit and SVN needs also to be installed on your system.

1. Get the WordPress test environment

Get the WordPress test environment as described here in step 1 and 2. You may want to use a separate directory for those stuff like ( ~/tmp/wordpress-test).

The repository looks like:

src/
tests/
tools/
Gruntfile.js
.travis.yml
.jshintrc
.svn
.editorconfig
.gitignore
wp-cli.yml
package.json
wp-config-sample.php
phpunit.xml.dist
wp-tests-config-sample.php

2. Copy relevant Files

  • Move the wp-tests-config-sample.php to your projects root directory: /var/www/my-project/wp-tests-config.php (Note the file re-naming!)
  • Create a directory for the core test framework (eg: /var/www/my-project/wp-tests)
  • Copy the directories tests/ and tools to this directory: (/var/www/my-project/wp-tests/tests and /var/www/my-project/wp-tests/tests.
  • Copy the tests config file /var/www/my-project/wp-tests-config.php to the wp-tests directory: /var/www/my-project/wp-tests/wp-tests-config.php

From now, all relative paths refer to your project root /var/www/my-project.

3. Setup Core Test Config

  • Create a Database to use only for your tests.
  • Open wp-tests-config.php and write in the credentials of your database. Also change define( 'ABSPATH', dirname( __FILE__ ) . '/src/' ); to define( 'ABSPATH', __DIR__ . '/' );
  • Open wp-tests/wp-tests-config.php and delete everything, except the opening php tag, of course! Then write in: require '../wp-tests-config.php'.

4. Setup Plugin Test Config

  • Create a directory /tests in your plugin directory.
  • Create a file phpunit.xml in your plugin directory and write in the following content
<phpunit
	bootstrap="tests/bootstrap.php"
	backupGlobals="false"
	colors="true"
	convertErrorsToExceptions="true"
	convertNoticesToExceptions="true"
	convertWarningsToExceptions="true"
	>
	<testsuites>
		<testsuite>
			<directory prefix="test-" suffix=".php">./tests/</directory>
		</testsuite>
	</testsuites>
</phpunit>
  • Create a file tests/bootstrap.php in your plugin directory and write in the following:
<?php 
/**
 * Bootstrap process for plugin unit tests
 */
$GLOBALS['wp_tests_options'] = array(
	'active_plugins' => array( 'my-plugin/my-plugin.php' ),
);

//you can try to use relative path if you are a fearnought
$wp_tests_config = '/var/www/my-project/wp-tests/tests/phpunit/includes/bootstrap.php';
require $wp_tests_config;

Your plugin directory should now look like this:

tests/
    bootstrap.php
my-plugin.php
phpunit.xml

5. Write Tests

Write test in php files looking like tests/test-foo.php:

<?php

class TestFoo extends WP_UnitTestCase {

	public function testFooMethod() {
		$foo = new Foo();
		$this->assertEquals( $foo->method(), 'Bar' );
	}
}

6. Run the tests

$ cd /var/www/my-project/wp-content/plugins/my-plugin`
$ phpunit
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 4.1.0 by Sebastian Bergmann.

Configuration read from /var/www/projects/inpsyde/agd/wp-content/plugins/inpsyde-user-location/phpunit.xml

.

Time: 5 ms, Memory: 35.50Mb

OK (1 test, 1 assertion)

Alternative Resources:

Those didn't work for me either. Amongst others, I got in trouble with wrong file pathes. Even so, the wp-cli method sounds nice an a quick way to set up the testing environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment