Test Driven Development
Categories
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Downloads
Component created
Component changed
Component body
This module provides various utilities for helping drupal module developers do Test-driven development. Currently it provides two general utilities for making test-driven-development easier.
devel_tdd_get_test
The first is the function devel_tdd_get_test that will give your test-object that can be run outside of any test harness. This means that you can very easily invoke your test in your development environment without running SimpleTest. This lets you iterate quickly building tests and writing your module at the same time.
// Load the test using devel_tdd_get_test
$test = devel_tdd_get_test('mymodule','myModuleTestClass');
// Run the tests.
// I'm not running this using a SimpleTest harness. This is running "in the nude".
$test->run();
// Since we don't have a harness, there is no reporting mechanism. Inspect the object to see the results
dpm($test);
devel_tdd_create_mock_function
This module also provides another handy utility devel_tdd_create_mock_function. This is useful for testing functions that have complex input arguments. For example a ctools-content-type might have a complex context-object, a $conf array, $args etc. Putting devel_tdd_create_mock_function at the beginning of a function will output the PHP code for a new wrapping function, with the input data already mocked. It lets you capture the state of the function as it was being called.
function function_to_test($arg1, $context, $object, $conf_array) {
// When this function if called, for example by loading a page. This will
// now print, using dpm(), a wrapping function with $arg1, $context,
// $object, and $conf_array already filled out. This is an easy way to
// capture "known good" arguments for a function and test on them.
devel_tdd_create_mock_function();
// Main body of the function goes here...
if ($arg1 > 12345 && $object->bar->baz['bob'] == 'banana') {
return TRUE;
}
// etc...
}
This will produce a function that is ready to be included in your test, with all the input arguments properly mocked.
// Mock function automatically created using the devel_tdd module
// This function calls highwire_search_results_render using known working arguments
function mock_function_to_test() {
require_once(drupal_get_path('module','ctools').'/includes/content.inc');
require_once(drupal_get_path('module','panels').'/panels.module');
require_once(drupal_get_path('module','ctools').'/includes/context-handler.inc');
$mocked_args = 'a:5:{i:0;s:23";i:1;a:22:{s:18:"asd";}';
return call_user_func_array('function_to_test', unserialize($mocked_args));
}
