You appear to be a bot. Output may be restricted
Description
Function to invoke when loadedHaving looked at a number of unit test routines for WordPress plugins there appear to be a couple of approaches of getting their code loaded into the test suite.
Using mu_plugins loaded
- Load the wordpress-develop-tests functions.php
- This provides tests_add_filter() which enables them to register code to run in response to "muplugins_loaded"
- Load the wordpress-develop-tests boostrap.php
Using global $wp_tests_options
- Adds your plugin to the array of 'active_plugins' in $GLOBALS['wp_tests_options']
- Load the wordpress-develop-tests bootstrap.php
Summary of methods used by different plugins
plugin | method used | env var | plugin’s tests dir | multisite? |
---|---|---|---|---|
shortcake | muplugins_loaded | WP_TESTS_DIR | php-tests | ? |
jetpack | muplugins_loaded | WP_DEVELOP_DIR | tests/php | php.multisite.xml |
WP-cli generated | muplugins_loaded | WP_TESTS_DIR | tests | ? |
CMB2 | muplugins_loaded | WP_TESTS_DIR | tests | ? |
BuddyPress | muplugins_loaded | WP_TESTS_DIR | tests/phpunit | ? |
blobaugh/wordpress-plugin-tests | wp_tests_options | |||
bbPress | both? | WP_TESTS_DIR | tests/phpunit | multisite.xml |
Preliminary thoughts on generalizing the solution
- Most unit test routines hard code the name of the plugin file to load
- But we can probably do better than that.
- If we know the name of our bootstrap file then we can determine the plugin name from that; assumption is that the bootstrap file is in wp-content/plugins/$plugin/tests/bootstrap.php
- Another way of invoking the tests would be to respond to an action hook invoked by wordpress-develop-tests
Requirements for in situ testing
All of the above discusses how plugins cater for running the tests in a vanilla environment. But we want to run our tests in situ, which means our plugin is expected to be loaded already.- Therefore we don't need to manually load the plugin.
- And we certainly don't want to actually run the bootstrap.
- You might wonder why we looked for it in the first place.
- So all we need to do is locate and load the files we need
Usage
bootstrap_loaded();
Parameters
Returns
voidSource
File name: oik-batch/tests/bootstrap.phpLines:
1 to 11 of 11
function bootstrap_loaded() { $wordpress_develop_tests = locate_wordpress_develop_tests(); if ( $wordpress_develop_tests ) { load_bootstrap_functions( $wordpress_develop_tests ); //tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' ); //continue_loading_bootstrap( $wordpress_develop_tests ); } else { echo "What shall we do now then?" . PHP_EOL; die( "Tests cannot be run without the WordPress develop test suite." ); } }View on GitHub