最合适的回答,由SO网友:J.D. 整理而成
我认为这里的主要问题是你加载WordPress的引导程序太早了。改成这样应该可以解决这个问题(我在评论中解释了为什么事情是按顺序进行的):
// This file contains tests_add_filter(), so we need to load it
// so that we can hook the _manually_load_plugin() function to
// muplugins_loaded. We can\'t use add_action() because WordPress
// isn\'t loaded yet, and we can\'t load it quite yet (see below).
require_once $wp_tests_dir . \'/includes/functions.php\';
// This function loads the plugin and dependencies. We need to
// define it and hook it to muplugins_loaded before including
// WordPress\'s bootstrap, since that will load WordPress. If
// we don\'t hook this up first, the hook will be fired when
// WordPress is loaded, and adding a hook to this after that
// will have no effect.
function _manually_load_plugin() {
/* Code in here is probably the same. */
}
tests_add_filter( \'muplugins_loaded\', \'_manually_load_plugin\' );
// Now that our function is hooked up, we can load WordPress, and
// the plugin and dependencies will be loaded when the hook gets
// called.
require_once $wp_tests_dir . \'/includes/bootstrap.php\';