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.
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
- 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/
andtools
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 thewp-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
.
- Create a Database to use only for your tests.
- Open
wp-tests-config.php
and write in the credentials of your database. Also changedefine( 'ABSPATH', dirname( __FILE__ ) . '/src/' );
todefine( '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'
.
- 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
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' );
}
}
$ 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:
- https://github.com/wp-cli/wp-cli/wiki/Plugin-Unit-Tests
- http://code.tutsplus.com/articles/the-beginners-guide-to-unit-testing-building-a-testable-plugin--wp-25741
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.