如何在自动化测试中正确设置依赖项?

时间:2015-03-16 作者:Jonathan

我正在尝试为依赖WP和BP的插件设置自动测试,如中所述:

  • https://codex.buddypress.org/developer/automated-testing/
  • https://make.wordpress.org/core/handbook/automated-testing/
  • https://codex.buddypress.org/developer/automated-testing/writing-automated-tests-for-buddypress-dependent-plugins/

    正在运行wp scaffold plugin-testsbin/install-wp-tests.sh

  • 使用上述手册页面中描述的加载程序加载BP,但我似乎无法正确加载堆栈。它要么抱怨BP函数未定义,要么抱怨WP函数未定义,要么抱怨我的插件函数未定义。Here\'s my install script, 和here\'s my bootstrap file. 我做错了什么?为方便起见,下面是引导文件的粘贴:

    <?php
    
    // set a flag so that certain functions know we\'re running tests. 
    define( \'RUNNING_TESTS\', TRUE ); 
    
    $wp_tests_dir = getenv( \'WP_TESTS_DIR\' );
    if ( ! $wp_tests_dir ) $wp_tests_dir = \'/tmp/wordpress-tests-lib\';
    define( \'WP_TESTS_DIR\', $wp_tests_dir ); 
    
    $bp_tests_dir = getenv( \'BP_TESTS_DIR\' ); 
    if ( ! $bp_tests_dir ) $bp_tests_dir = \'/tmp/buddypress/tests/phpunit\';
    define( \'BP_TESTS_DIR\', $bp_tests_dir ); 
    
    echo \'Done defining stuff! \'; 
    
    require_once $wp_tests_dir . \'/includes/bootstrap.php\';
    
    require_once $wp_tests_dir . \'/includes/functions.php\';
    
    echo \'Done requiring test dirs! \' ; 
    
    // this is my debugging file, which ensures that tests won\'t fail 
    // if there\'s a call to `_log()`. 
    require dirname( __FILE__ ) . \'/debug.php\';
    
    function _manually_load_plugin() {
        require BP_TESTS_DIR . \'/includes/loader.php\';
    
    
        // override this class to load mock data
        require dirname( __FILE__ ) . \'/class-MockMLAAPI.php\'; 
    
        // don\'t get the whole plugin now, just a few classes, because 
        // to test them individually we feed them mock data above. 
        require dirname( __FILE__ ) . \'/../class-CustomAuthentication.php\';
    
        // Requiring this file gives you access to BP_UnitTestCase
        require $bp_tests_dir . \'/includes/testcase.php\';
    
    }
    tests_add_filter( \'muplugins_loaded\', \'_manually_load_plugin\' );
    

1 个回复
最合适的回答,由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\';

结束

相关推荐