You will need svn, PHPUnit and wp-cli (they are provided by VVV). Make sure you have them installed.
###1. Set up
Initialize your testing environment using install-wp-tests.sh
script.This script installs a copy of Wordpress in the /tmp directory (by default) as well as the Wordpress unit testing tools. Then it creates the specified database to be used while running tests.
vagrant@vvv:cd my-project/tests/
vagrant@vvv:my-project/tests$ install-wp-tests.sh name_of_test_db db_user 'db_pass' localhost wp_version
If the script fails or you get some errors requiring files like :
require_once $_tests_dir . '/includes/functions.php';
// or
require $_tests_dir . '/includes/bootstrap.php';
You may need to delete /tmp/wordpress-tests-lib/
or /tmp/wordpress
folder and run the script again (the bash script will skip the SVN checkout of the phpunit includes if the directory already exists)
Also,notice that this script can be run multiple times without errors, but it will not overwrite previously existing files. So if your DB credentials change, or you want to switch to a different instance of mysql, simply re-running the script won't be enough. You'll need to manually edit the wp-config.php that's installed in the /tmp.
When everything is installed you should be able to run the repository tests :
vagrant@vvv:my-project/tests$ phpunit
...
Time: 922 ms, Memory: 25.25Mb
OK (3 tests, 19 assertions)
Test files must be named starting with "test-" and test functions must start with "test".
// tests/phpunit/my-plugin/test-keywords.php
class Keywords_Test extends WP_UnitTestCase {
protected $sitemaps;
public function setUp() {
parent::setUp();
$this->sitemaps = new Sitemaps();
//custom setup
}
function testKeywords() {
global $post;
$post = $this->factory->post->create_and_get( array( 'post_title' => 'test ' . rand_str(), 'post_date' => date('Y-m-d H:i:s')));
$keywords = $this->sitemaps->get_post_sitemap_keywords();
$this->assertLessThanOrEqual($this->sitemaps->max_sitemap_keywords, count($keywords));
}
}
###3. Change your bootstrap if needed
There are some dependencies and basic config loaded for every test in the tests/bootstrap.php
file:
function _manually_load_environment() {
switch_theme('my-theme');
// Update array with plugins to include ...
$plugins_to_active = array(
'my-plugin/my-plugin.php',
'my-other-plugin/my-other-plugin.php'
);
update_option( 'active_plugins', $plugins_to_active );
}
tests_add_filter( 'muplugins_loaded', '_manually_load_environment' );
require $_tests_dir . '/includes/bootstrap.php';
etc
But you can create your own bootstrap file with dependencies needed only for your new test and include it in your files.
vagrant@vvv:my-project/tests$ phpunit
...
Time: 922 ms, Memory: 25.25Mb
OK (4 tests, 23 assertions)
Specify a handler for wp_die so we can test this result like an expected exception
function wp_die_handler( $message, $title = '', $args = array() ) {
throw new Exception($message);
}
tests_add_filter( 'wp_die_handler', 'wp_die_handler' );
And now..
/**
* @expectedException Exception
*/
public function testException()
{
this_function_dies_with_wp_die();
}
You can use wp scaffold plugin-tests my-plugin
to generate the structure and files needed for running PHPUnit tests in your plugin, including the install-wp-tests.sh
script.