#323477: run some test class using a single-environment method. From: Damien Tournoud --- simpletest/drupal_web_test_case.php | 80 ++++++++++++++++++++++++++--------- simpletest/tests/database_test.test | 35 +++++++++++++++ 2 files changed, 94 insertions(+), 21 deletions(-) diff --git modules/simpletest/drupal_web_test_case.php modules/simpletest/drupal_web_test_case.php index ed437b7..dda90d2 100644 --- modules/simpletest/drupal_web_test_case.php +++ modules/simpletest/drupal_web_test_case.php @@ -63,6 +63,15 @@ abstract class DrupalTestCase { protected $skipClasses = array(__CLASS__ => TRUE); /** + * Indicate that the test methods in this class should run in a single environment. + * + * If this flag is set to TRUE, all the test methods in this class will run + * in the same test environment. If set to FALSE (the default), a new test + * environment is created before running each method. + */ + protected $singleEnvironmentMode = FALSE; + + /** * Constructor for DrupalTestCase. * * @param $test_id @@ -424,38 +433,69 @@ abstract class DrupalTestCase { set_error_handler(array($this, 'errorHandler')); $class = get_class($this); + // Iterate through all the methods in this class. + $methods = array(); foreach (get_class_methods($class) as $method) { // If the current method starts with "test", run it - it's a test. if (strtolower(substr($method, 0, 4)) == 'test') { - // Insert a fail record. This will be deleted on completion to ensure - // that testing completed. - $method_info = new ReflectionMethod($class, $method); - $caller = array( - 'file' => $method_info->getFileName(), - 'line' => $method_info->getStartLine(), - 'function' => $class . '->' . $method . '()', - ); - $completion_check_id = DrupalTestCase::insertAssert($this->testId, $class, FALSE, t('The test did not complete due to a fatal error.'), 'Completion check', $caller); - $this->setUp(); - try { - $this->$method(); - // Finish up. - } - catch (Exception $e) { - $this->exceptionHandler($e); - } - $this->tearDown(); - // Remove the completion check record. - DrupalTestCase::deleteAssert($completion_check_id); + $methods[] = $method; } } + + if ($this->singleEnvironmentMode) { + // Run all the methods in a single environment. + $this->runMethods($methods); + } + else { + // Run each method in its own test environment. + foreach ($methods as $method) { + $this->runMethods(array($method)); + } + } + // Clear out the error messages and restore error handler. drupal_get_messages(); restore_error_handler(); } /** + * Run a set of methods in the same test environment. + * + * @param $methods + * An array of method names to run. + */ + protected function runMethods($methods) { + $class = get_class($this); + + // Insert a fail record. This will be deleted on completion to ensure + // that testing completed. + $method_info = new ReflectionMethod($class, 'setUp'); + $caller = array( + 'file' => $method_info->getFileName(), + 'line' => $method_info->getStartLine(), + 'function' => $class . '->setUp()', + ); + $completion_check_id = DrupalTestCase::insertAssert($this->testId, $class, FALSE, t('The test did not complete due to a fatal error.'), 'Completion check', $caller); + + $this->setUp(); + + foreach ($methods as $method) { + try { + $this->$method(); + // Finish up. + } + catch (Exception $e) { + $this->exceptionHandler($e); + } + } + + $this->tearDown(); + // Remove the completion check record. + DrupalTestCase::deleteAssert($completion_check_id); + } + + /** * Handle errors during test runs. * * Because this is registered in set_error_handler(), it has to be public. diff --git modules/simpletest/tests/database_test.test modules/simpletest/tests/database_test.test index e97435b..71db349 100644 --- modules/simpletest/tests/database_test.test +++ modules/simpletest/tests/database_test.test @@ -18,6 +18,11 @@ class FakeRecord { } */ class DatabaseTestCase extends DrupalWebTestCase { + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + function setUp() { parent::setUp('database_test'); @@ -2623,6 +2628,12 @@ class DatabaseLoggingTestCase extends DatabaseTestCase { * Range query tests. */ class DatabaseRangeQueryTestCase extends DrupalWebTestCase { + + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + public static function getInfo() { return array( 'name' => 'Range query test', @@ -2654,6 +2665,12 @@ class DatabaseRangeQueryTestCase extends DrupalWebTestCase { * Temporary query tests. */ class DatabaseTemporaryQueryTestCase extends DrupalWebTestCase { + + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + public static function getInfo() { return array( 'name' => 'Temporary query test', @@ -3101,6 +3118,11 @@ class DatabaseTransactionTestCase extends DatabaseTestCase { */ class DatabaseExtraTypesTestCase extends DrupalWebTestCase { + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + public static function getInfo() { return array( 'name' => 'Extra Types tests', @@ -3109,7 +3131,6 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase { ); } - /** * Test the date data type. */ @@ -3203,6 +3224,12 @@ class DatabaseExtraTypesTestCase extends DrupalWebTestCase { * Check the sequences API. */ class DatabaseNextIdCase extends DrupalWebTestCase { + + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + public static function getInfo() { return array( 'name' => t('Sequences API'), @@ -3230,6 +3257,12 @@ class DatabaseNextIdCase extends DrupalWebTestCase { * Tests the empty pseudo-statement class. */ class DatabaseEmptyStatementTestCase extends DrupalWebTestCase { + + /** + * Run database test cases in the same environment. + */ + protected $singleEnvironmentMode = TRUE; + public static function getInfo() { return array( 'name' => t('Empty statement'),