SimpleTest is all the rage in Drupal 7, and it's even finding its way into quite a few contrib modules for Drupal 6, not to mention its use at some big Drupal 6 projects, like the Economist. You can also integrate SimpleTest's test results into a Hudson setup using a script like the one provided by ComputerMinds.co.uk. (Tree House Agency has recently updated this script to work with SimpleTest 6.x-2.9 and submitted it to Pressflow and d.o proper.)
What if you're building an install profile and want to run SimpleTests against it? The default DrupalWebTestCase will install the default profile and any extra modules that you specify when the setUp() method is called.
Here is the skeleton of a subclass of DrupalWebTestCase that will install an install profile of your choice, and then test functionality inside of that install profile. Note that if you make any changes to the default profile installation form, they will not be accounted for when setUp() is called, since it will not be submitted.
// Core: Id: drupal_web_test_case.php,v 1.96 2009/04/22 09:57:10 dries Exp
/**
* @file
* Test case for Drupal tests using a given install profile.
*
* Based on work that is copyright 2008-2009 by Jimmy Berry ("boombatower", <a href="http://drupal.org/user/214218" title="http://drupal.org/user/214218">http://drupal.org/user/214218</a>)
*/
class YOURPROFILENAMEWebTestCase extends DrupalWebTestCase {
protected function getProfileName() {
return 'YOURPROFILENAME';
}
/**
* Generates a random database prefix, runs the install scripts on the
* prefixed database and enable the specified modules. After installation
* many caches are flushed and the internal browser is setup so that the
* page requests will run on the new prefix. A temporary files directory
* is created with the same name as the database prefix.
*
* @param ...
* List of modules to enable for the duration of the test.
*/
protected function setUp() {
global $db_prefix, $user, $language; // $language (Drupal 6).
$profile_name = $this->getProfileName();
// Store necessary current values before switching to prefixed database.
$this->originalPrefix = $db_prefix;
$this->originalFileDirectory = file_directory_path();
$clean_url_original = variable_get('clean_url', 0);
// Must reset locale here, since schema calls t(). (Drupal 6)
if (module_exists('locale')) {
$language = (object) array('language' => 'en', 'name' => 'English', 'native' => 'English', 'direction' => 0, 'enabled' => 1, 'plurals' => 0, 'formula' => '', 'domain' => '', 'prefix' => '', 'weight' => 0, 'javascript' => '');
locale(NULL, NULL, TRUE);
}
// Generate temporary prefixed database to ensure that tests have a clean starting point.
$db_prefix = 'simpletest' . mt_rand(1000, 1000000);
include_once './includes/install.inc';
drupal_install_system();
// Add the specified modules to the list of modules in the default profile.
$args = func_get_args();
$modules = array_unique(array_merge(drupal_verify_profile($profile_name, 'en'), $args));
drupal_install_modules($modules);
// Because the schema is static cached, we need to flush
// it between each run. If we don't, then it will contain
// stale data for the previous run's database prefix and all
// calls to it will fail.
drupal_get_schema(NULL, TRUE);
// Run default profile tasks.
$task = 'profile';
$func = "{$profile_name}_profile_tasks";
$func($task, '');
// Rebuild caches.
actions_synchronize();
_drupal_flush_css_js();
$this->refreshVariables();
$this->checkPermissions(array(), TRUE);
user_access(NULL, NULL, TRUE); // Drupal 6.
// Log in with a clean $user.
$this->originalUser = $user;
session_save_session(FALSE);
$user = user_load(array('uid' => 1));
// Restore necessary variables.
variable_set('install_profile', $profile_name);
variable_set('install_task', 'profile-finished');
variable_set('clean_url', $clean_url_original);
variable_set('site_mail', 'simpletest@example.com');
// Use temporary files directory with the same prefix as database.
variable_set('file_directory_path', $this->originalFileDirectory . '/' . $db_prefix);
$directory = file_directory_path();
// Create the files directory.
file_check_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
set_time_limit($this->timeLimit);
}
}
To use, enter your profile name in the appropriate places, and then subclass this class to make a set of tests for your install profile.

Support for this has been integrated into the Pressflow mainline for quite some time.
Post new comment