Testing helper library

cabbiepete - January 4, 2008 - 13:18

This is a cross post with a comment I made at http://groups.drupal.org/node/7785#comment-24109

Anyone already making helpers for testing in drupal let me know I would like to contribute this stuff into a common place if its useful to anyone else.

Post Starts Here

There are some plans for some similar stuff in the testing plans (I forget where but I read it the other day) and Angie Byron mentions the need for node creation helpers for DrupalTestCase in her excellent article A Drupal Module Developer's Guide to SimpleTest.

This sounds like a good approach so I am going to start writing a ITMDrupalTestCase that extends DrupalTestCase and can be used as an extension of custom TestCases. ITM because I work for Informa Telecoms and Media currently.

First version of ITMDrupalTestCase pasted below, currently has two protected variables which you override in your custom test. Using these overridden values the test function checks the module implements the hook functions.

It just checks the hook is implemented as a function not that its parameters are correctly defined or the return values come out correctly.

<?php
/**
* Helper class for testing drupal modules.
*
* Usage
*
*   class MyTestCase extends ITMDrupalTestCase
*/
class ITMDrupalTestCase extends DrupalTestCase {

  protected
$module_name = '';
  protected
$hooks = array();

  function
get_info() {
    return array(
     
'name' => t('helper test case'),
     
'desc' => t('A helper test case.'),
     
'group' => t('ITM Test'),
    );
  }


/**
   * Test cck hooks are implemented.
   *
   * Usage
   *
   *  Re-declare $module_name and $hooks in your test case to test a module
   *  implements those hooks as functions.
   *
   */
 
function test_hook_impl() {
   
$module_name = $this->module_name;
   
$hooks = $this->hooks;

    if (
$module_name and $hooks) {
      foreach (
$hooks as $hook) {
       
$function_exists = 0;
       
$function_name = $module_name. '_' .$hook;
       
$function_exists = function_exists($function_name);
       
$this->assertTrue($function_exists, 'Expected hook ' .$hook);
      }
    }
  }
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.