diff options
author | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
---|---|---|
committer | Dries Buytaert <dries@buytaert.net> | 2008-04-20 18:34:43 +0000 |
commit | ffc0e93c4eb0555a08f0b58bed0735416e6ba41f (patch) | |
tree | 7f741d0b40124633c5b337dc7210711bfcd4b876 /modules/simpletest/drupal_test_suite.php | |
parent | af474609e3e80db9ba1d16b9ad2eae89775f51c8 (diff) | |
download | brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.gz brdo-ffc0e93c4eb0555a08f0b58bed0735416e6ba41f.tar.bz2 |
- Added a test framework to Drupal along with a first batch of tests for
Drupal core! This is an important milestone for the project so enable
the module and check it out ... :)
Thanks to Rok Žlender, Károly Négyesi, Jimmy Berry, Kevin Bridges, Charlie
Gordon, Douglas Hubler, Miglius Alaburda, Andy Kirkham, Dimitri13, Kieran
Lal, Moshe Weitzman, and the many other people that helped with testing
over the past years and that drove this home.
It all works but it is still rough around the edges (i.e. documentation
is still being written, the coding style is not 100% yet, a number of
tests still fail) but we spent the entire weekend working on it in Paris
and made a ton of progress. The best way to help and to get up to speed,
is to start writing and contributing some tests ... as well as fixing
some of the failures.
For those willing to help with improving the test framework, here are
some next steps and issues to resolve:
- How to best approach unit tests and mock functions?
- How to test drupal_mail() and drupal_http_request()?
- How to improve the admin UI so we have a nice progress bar?
- How best to do code coverage?
- See http://g.d.o/node/10099 for more ...
Diffstat (limited to 'modules/simpletest/drupal_test_suite.php')
-rw-r--r-- | modules/simpletest/drupal_test_suite.php | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/modules/simpletest/drupal_test_suite.php b/modules/simpletest/drupal_test_suite.php new file mode 100644 index 000000000..bbc33e220 --- /dev/null +++ b/modules/simpletest/drupal_test_suite.php @@ -0,0 +1,121 @@ +<?php +// $Id$ + +/** + * Implementes getTestInstances to allow access to the test objects from outside + */ +class DrupalTestSuite extends TestSuite { + var $_cleanupModules = array(); + + function DrupalTestSuite($label) { + $this->TestSuite($label); + } + + /** + * @return array of instantiated tests that this GroupTests holds + */ + function getTestInstances() { + for ($i = 0, $count = count($this->_test_cases); $i < $count; $i++) { + if (is_string($this->_test_cases[$i])) { + $class = $this->_test_cases[$i]; + $this->_test_cases[$i] = &new $class(); + } + } + return $this->_test_cases; + } +} + +class DrupalTests extends DrupalTestSuite { + /** + * Constructor + * @param array $class_list list containing the classes of tests to be processed + * default: NULL - run all tests + */ + function DrupalTests($class_list = NULL) { + static $classes; + $this->DrupalTestSuite('Drupal Unit Tests'); + + /* Tricky part to avoid double inclusion */ + if (!$classes) { + + $files = array(); + foreach (array_keys(module_rebuild_cache()) as $module) { + $module_path = drupal_get_path('module', $module); + $test = $module_path . "/$module.test"; + if (file_exists($test)) { + $files[] = $test; + } + } + + $existing_classes = get_declared_classes(); + foreach ($files as $file) { + include_once($file); + } + $classes = array_diff(get_declared_classes(), $existing_classes); + } + if (!is_null($class_list)) { + $classes = $class_list; + } + if (count($classes) == 0) { + drupal_set_message('No test cases found.', 'error'); + return; + } + $groups = array(); + foreach ($classes as $class) { + if (!is_subclass_of($class, 'DrupalWebTestCase') && !is_subclass_of($class, 'DrupalUnitTestCase')) { + continue; + } + $this->_addClassToGroups($groups, $class); + } + foreach ($groups as $group_name => $group) { + $group_test = &new DrupalTestSuite($group_name); + foreach ($group as $key => $v) { + $group_test->addTestCase($group[$key]); + } + $this->addTestCase($group_test); + } + } + + /** + * Adds a class to a groups array specified by the getInfo of the group + * @param array $groups Group of categorized tests + * @param string $class Name of a class + */ + function _addClassToGroups(&$groups, $class) { + $test = &new $class(); + if (method_exists($test, 'getInfo')) { + $info = $test->getInfo(); + $groups[$info['group']][] = $test; + } + } + + /** + * Invokes run() on all of the held test cases, instantiating + * them if necessary. + * The Drupal version uses paintHeader instead of paintGroupStart + * to avoid collapsing of the very top level. + * + * @param SimpleReporter $reporter Current test reporter. + * @access public + */ + function run(&$reporter) { + cache_clear_all(); + @set_time_limit(0); + ignore_user_abort(TRUE); + + // Disable devel output, check simpletest settings page + if (!variable_get('simpletest_devel', FALSE)) { + $GLOBALS['devel_shutdown'] = FALSE; + } + + $result = parent::run($reporter); + + // Restores modules + foreach ($this->_cleanupModules as $name => $status) { + db_query("UPDATE {system} SET status = %d WHERE name = '%s' AND type = 'module'", $status, $name); + } + $this->_cleanupModules = array(); + + return $result; + } +} |