Skip to:
Content
Pages
Categories
Search
Top
Bottom

Writing automated tests for BuddyPress-dependent plugins

The automated testing suite introduced into BuddyPress after 1.7 make it easy to write unit tests for your BP-dependent plugins. Our implementation assumes that you’ll be writing your plugin tests according to wordpress-plugin-tests method. If you’re new to writing WordPress plugin tests, it’s highly recommended that you use the wordpress-plugin-tests technique (or, even easier, the wp-cli unit-tests scaffold tool.

BuddyPress provides two useful kinds of utilities that your plugin’s tests can take advantage of. We’ll discuss the two utilities briefly, and follow it with a working example.

Note: In this tutorial, we’ll be extending BP’s test suite for use in a plugin. But a similar technique could be used for BP themes, as well as for setting up integration tests for an entire, highly-customized BuddyPress installation.

  1. BuddyPress installation and bootstrapping – If your plugin depends on BuddyPress, then you’ll need to make sure that BuddyPress is installed and running when running your tests. Establishing the proper dependencies manually can be very tricky, because BP’s setup routine requires the installation of custom tables and other important one-time configuration, and it all has to happen in a very specific order in order to have a functioning version of BuddyPress. Luckily, you don’t have to do it manually. Just require buddypress/tests/includes/loader.php in a callback function hooked to muplugins_loaded using tests_add_filter() – right before you manually load your own plugin – and BP will make sure that (1) BP gets installed correctly, and (2) BP is fully loaded.
  2. BP_UnitTestCase and data factories – When you use the BP_UnitTestCase class for your test cases, you automatically get access to some helpful utility methods (such as go_to(), which is sensitive to the requirements of BP URLs), as well as the BuddyPress data factories for generating groups, activity, and other BP test data. Does your plugin have additional data factories? Consider building your own _UnitTestCase that extends BP_UnitTestCase.

What follows is an annotated bootstrap.php that demonstrates the best way to take inherit BP’s unit testing tools in your own plugin’s test suite. The plugin in the example can be found at BP-CLI, and the code below is from the file bp-cli/tests/bootstrap.php. You should be able to copy and paste this code into your own tests/bootstrap.php file – just change the paths for your plugin loader file, and your optional _UnitTestCase class file.

<?php

// The BP_TESTS_DIR constant is a helpful shorthand for accessing assets later
// on. By defining in a constant, we allow for setups where the BuddyPress
// tests may be located in a non-standard location.
if ( ! defined( 'BP_TESTS_DIR' ) ) {
	define( 'BP_TESTS_DIR', __DIR__ . '/../../buddypress/tests' );
}

// Checking for the existence of tests/bootstrap.php ensures that your version
// of BuddyPress supports this kind of automated testing
if ( file_exists( BP_TESTS_DIR . '/bootstrap.php' ) ) {

	// The functions.php file from the WP test suite needs to be defined early,
	// because it gives us access to the tests_add_filter() function
	require_once getenv( 'WP_TESTS_DIR' ) . '/includes/functions.php';

	// Hooked to muplugins_loaded, this function is responsible for bootstrapping
	// BuddyPress, as well as your own plugin
	function _bootstrap_plugins() {

		// loader.php will ensure that BP gets installed at the right time, and
		// that BP is initialized before your own plugin
		require BP_TESTS_DIR . '/includes/loader.php';

		// Change this path to point to your plugin's loader file
		require __DIR__ . '/../bp-cli.php';
	}
	tests_add_filter( 'muplugins_loaded', '_bootstrap_plugins' );

	// Start up the WP testing environment
	require getenv( 'WP_TESTS_DIR' ) . '/includes/bootstrap.php';

	// Requiring this file gives you access to BP_UnitTestCase
	require BP_TESTS_DIR . '/includes/testcase.php';

	// Optional: If your plugin needs its own _UnitTestCase class, include it
	// here so that it's available when your testcases are loaded
	require __DIR__ . '/bp-cli-testcase.php';
}