Quantcast
Channel: #! code - SimpleTest
Viewing all articles
Browse latest Browse all 6

Mimicking Data Provider Functionality In Drupal SimpleTest

$
0
0

Although Drupal SimpleTest is an extremeley 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.

1
2
3
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.

1
2
3
publicfunction 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.

1
2
3
4
5
6
7
publicfunction addingNumbersDataProvider(){  returnarray(    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.

1
2
3
4
5
publicfunction 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.

1
2
3
4
5
6
function my_module_add_numbers($number1,$number2){  if(!is_numeric($number1)&&!is_numeric($number2)){    return0;  }  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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
publicfunction calculationDataProvider(){  returnarray(    array(2,2,2,4),    array(1,1,1,2)  );} 
publicfunction 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);  }}
Category: 

Viewing all articles
Browse latest Browse all 6

Latest Images

Trending Articles





Latest Images