Mimicking Data Provider Functionality In Drupal SimpleTest

Although Drupal SimpleTest is an extremely useful module it doesn't currently support data providers, which is a shame as I use that feature quite a bit in other testing frameworks. A data provider is a mechanism that allows you to call a single test case multiple times with different arguments so that you can ensure the correct output each time. This is useful because testing a single function once is fine, but testing it with a variety of different values can otherwise mean having multiple test cases.

To mimic this functionality in Drupal SimpleTest you can create a data provider method that returns an array, which is then used to test a particular function.

For example, let's say I have the following (trivial) function in a Drupal module.

function my_module_add_numbers($number1, $number2) {
  return $number1 + $number2;
}

I would normally test this in Drupal SimpleTest by creating a method in testing class and feed some parameters into the function. Here is the test method with the rest of the test class removed.

public function testAdding1And1Equals3() {
  $this->assertEqual(my_module_add_numbers(1, 2), 3);
}

Obviously this function just adds two numbers together, but what happens if the parameters are different? What if we pass two strings instead of integers? The function obviously doesn't degrade nicely if this happens and so we need to add some error checking to the function to make sure it can handle different forms of input. In order to unit test our work properly we need to pass multiple arguments to this function using a data provider. Here is the data provider we will use for this unit test. The first two values in each array are the parameters we will use, and the third is the expected output.

public function addingNumbersDataProvider() {
  return array(
    array(1, 2, 3),
    array('1', '2', 3),
    array('monkey', 'wrench', 0)
  );
}

The only change we make to the unit test is to loop through the data from the data provider, feed the parameters into the function and test the correct output is returned.

public function testAddingNumbers() {
  foreach ($this->addingNumbersDataProvider() as $data) {
    $this->assertEqual(my_module_add_numbers($data[0], $data[1]), $data[2]);
  }    
}

With unit test in hand we can see that our tests now fail. So we can ensure the correct output with some simple changes to the original function.

function my_module_add_numbers($number1, $number2) {
  if (!is_numeric($number1) && !is_numeric($number2)) {
    return 0;
  }
  return (int)$number1 + (int)$number2;
}

Using this method means that you can now call a single test case multiple times without having to have multiple test cases. We can also ensure that our function works no matter what sort of data we throw at it. This is especially useful for things like escaping strings as you will want to make sure that any security threats are dealt with safely. If any new security threat happens to crop up you can always add this to your data provider and ensure that your functions are able to deal with it.

One small downside to this approach is that you will almost certainly get lost if you have lots of elements in your data provider array. One way to get around this is to extract the variables in the array via a list() function. In the following example we take a data array consisting of 4 elements and create variables from these elements so we can better understand what is going on in the test.

public function calculationDataProvider() {
  return array(
    array(2, 2, 2, 4),
    array(1, 1, 1, 2)
  );
}

public function testCalculation() {
  foreach ($this->calculationDataProvider() as $data) {
    list($height, $length, $width, $total_devices_count) = $data;
    // run calculation
    $result = theCalculation($height, $length, $width);
    // assertions
    $this->assertEqual($result['total_devices_count'], $total_devices_count);
  }
}

Comments

Nice simple idea, done well. I have lots of @dataProvider tests in drupal 8.x and am having to expand/backport to 7.x. Your post gave me a good way to save time. Thanks

 

Permalink

You are very welcome Jonathan. I'm really pleased that an article I wrote over 8 years ago is still helping people \o/

Name
Philip Norton
Permalink

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
1 + 8 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.