Activating BuddyPress for PHPUnit WordPress-tests

I have found myself writing PHPUnit tests for WordPress plugins lately (using wordpress-tests) and the code that I’m working with has a dependency on BuddyPress. ¬†Enabling BuddyPress for WordPress unit testing requires a bit of hoop jumping because you can’t use the wp_tests_options global variable as you would any other plugin in your local bootstrap file (which is configured in phpunit.xml):

$GLOBALS['wp_tests_options'] = array(
  'active_plugins' => array(

Instead, run the following within your bootstrap file after you’ve included the wordpress-tests bootstrap:

  // activate the buddypress plugin for your network (if you aren't running MULTISITE, the third parameter should be FALSE)
  activate_plugin( WP_CONTENT_DIR . '/plugins/buddypress/bp-loader.php', '', TRUE, TRUE );

  // set the components that you want to activate
  $active_components = array(
    'activity' => TRUE,
    'friends' => FALSE,
    'groups' => FALSE,
    'messages' => TRUE,
    'xprofile' => TRUE,
    'blogs' => TRUE,

  // update your site's bp-active-components option with your configured settings
  update_option( 'bp-active-components', $active_components );

  // include the file that contains the logic you need to create BuddyPress tables
  require_once WP_CONTENT_DIR . '/plugins/buddypress/bp-core/admin/bp-core-schema.php';

  // run the installer

With that in place, your tests should have BuddyPress classes & functions available to you – complete with BP tables in your database!