PHPUnit testing WordPress Plugin

The reason the WordPress test bootstrap is loaded at the end is precisely because it loads WordPress:

// Load WordPress
require_once ABSPATH . '/wp-settings.php';

If you don’t hook your function to load your plugin to 'muplugins_loaded' before including the bootstrap, your plugin won’t be loaded with WordPress. In most cases that will mean that your plugin won’t get set up properly (e.g., 'init' will have already been fired before your plugin’s functions get hooked up).

As far as checking for dependencies, you could probably do that by hooking into the 'plugins_loaded' action:

function _check_for_dependencies() {
    if ( ! is_plugin_active( 'some-plugin/some-plugin.php' ) ) {
        exit( 'Some Plugin must be active to run the tests.' . PHP_EOL );
    }
}
tests_add_filter( 'plugins_loaded', '_check_for_dependencies' );

Or, you can just let WordPress’ bootstrap load completely and check for your dependencies after it.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)