How to Write Drupal Test Case with Drupal Testing Framework

Category: 

In Drupal 6 many of the Drupal developers used a Module called Simpletest to Unit Test the other modules. But it required a lot of core patches as well. So the good news for Drupal Developers is Drupal 7 has that module inbuilt. But, once it is inbuilt a question comes is how to use it and how to write simple unit tests. So today I am going to demonstrate how to write a simple test suite.
 

To make it in action I will be using or rather creating a Module that will list all the enabled modules in the drupal 7 website. So lets start with creating the Module. As drupal 7 convension, I will be using the following hooks to create the module. The Module is called "module_list"

  1. hook_help
  2. hook_permission
  3. hook_block_info
  4. hook_block_view

In this tutorial it is assumed that, you are well awere of the module development. So I will just be focusing to the test case and very little module development. If you are not enough familier to the module development you can google yourself or make a request to me so I can make another tutorial in the Module Development. Anyway, without further more delay lets proceed.

Actually, most of the tests written for Drupal are not unit tests. Instead, they are functional tests. That is, the tests are designed to verify that when a given piece of code is inserted into Drupal, it functions as expected within the context of the application. This is a broader category of testing than unit tests. Larger chunks of code (like, say, Drupal as a whole) are expected to function correctly already before the functional test can accurately measure the correctness of the code being tested. And rather than calling the functions-to-be-tested directly, often times a functional test will execute the entire application under conditions which make it easy to check, whether the code being tested is working. For example, Drupal's functional tests often start Drupal, add a user, enable some modules, then retrieve URLs over an HTTP connection and finally test the output.

So we will proceed to the test creation steps,

  1. In time of module creation we have defined a .info file. In my case it is module_list.info. To add the test page you have to add a line as the following code,
     
    ;$Id$
     
    name = Module List
    description = A list of Enabled Modules
    core = 7.x
    package = Module
    files[] = module_list.info
    files[] = module_list.module
    files[] = module_list.test

    Here files[] = module_list.test is the new addition. This will tell Drupal to add the test file and render it.

  2. Next we will create the test file. Before that we have to keep some informations in mind.
    1. Create a new class that extends DrupalWebTestCase or have to extend that class wgich extends DrupalWebTestCase.
    2. Add a getInfo() function. This function will let the drupal know about the test. I will describe it later in this tutorial.
    3. Use setUp() method for any necessary configuration. It is not actually required.
    4. Write one or more test methods, beginning each method with the word test.
    5. In each test method, use one or more assertions to test actual values. In this tutorial we will use only assertions.
  3. So, let us create the file and the class first. To do that open up your text editor and create a file with the modulename.test extension. Now create a class called ModuleListTestCase. Before that please keep in mind that drupal has some Naming Conventions here.
    1. Drupal functions are named in all lower-case, with words separated by underscore (_) Classes and methods are different. Classes should be named in upper-case "CamelCase" notation, with the first letter capitalized. Methods should be named in "camelCase" with the first letter in lowercase. Underscores should not be used in class or method names.
       
      class ModuleListTestCase extends DrupalWebTestCase {
      }
  4. Then we will write the getInfo() method. The getInfo() method will tell drupal about the test case. This method has the following parameters.
    1. name: The name of the test.
    2. description: A sentence describing what the tests do.
    3. group: The name of the group to which these tests belong.
    4. class ModuleListTestCase extends DrupalWebTestCase {
           public function getInfo() {
      		return array(
      			'name' => 'Test module_list Block Functionality',
      			'description' => 'Test the display Block in the module_list Module',
      			'group' => 'ModuleList',
      		);
           }
      }
  5. If you haven't enabled the Test Module, please enable it in the Modules section. Then Go to Configuration > Testing (admin/config/development/testing). You will see a big list of test cases. Now scroll down to see ModuleList in that big list. Please refer to the screen shot below,

    Test Module Listing Drupal
  6. Now, it is time to write the actual test suite. As I mentioned earlier that we will use assertions only here so we are writing two methods called assertTrue & assertEqual in both the test cases.

    Basically, a test assertion is a single check to verify the output of your code. For example, you can compare two variables or look for a certain text on the currently loaded page.
    public function testBlockInfo() {
    		$info = module_invoke('module_list', 'block_info');
     
    		$this->assertEqual(1, count($info),t('This Module Defines a block view'));
     
    		$this->assertTrue(isset($info['list_modules']), t('Module exists(module_list)'));
    	}	
     
    	public function testBlockView() {
    		$data = module_invoke('module_list', 'block_view', 'list_modules');
     
    		$this->assertTrue(is_array($data), t('This block view section has an array of data'));
     
    		$this->assertEqual(t('Enabled Module List'), $data['subject'], t('The module subject is present'));
     
    }

    The code above has two test methods as you can see,

    • testBlockInfo()
    • testBlockView()

    So as the name says we are going to test the hook_block_info and the hook_block_view. Now, in the both methods you can see that I have used, a method called module_invoke. This method takes 2 parameters and optional arguments.

    1. In the first test method I used module_invoke('module_list', 'block_info');, which is similar of calling the hook_block_info of the module_list Module. So the first parameter is the Module name and the second is the hook associated with it.
    2. In the second test method I used, module_invoke('module_list', 'block_view', 'list_modules');, which is again similar of calling the module_list module's hook_block_view with the hook's parameter as the block name.

    So finally we come to the test functions body.

    1. In testBlockInfo(), the method assertEqual will check if the module_list module is creating a block or not. If yes it will return a count of 1. Which will match in this case. Or else it will give a report as failed.
    2. In testBlockInfo(), the method assertTrue will return a boolean as if the Module exists. Or else it will return FALSE.
    3. In testBlockView(), the method assertTrue will check if the module is returning any output data as array. If yes the test passes or else it will be failed.
    4. In testBlockView(), the method assertEqual will check for the module's subject key is as same as the "Enabled Module List". If yes then the test will pass.
  7. So, after all this it is time to see it in action. So to do that click on the module test listing in the testing configuration page (Shown in the previous screenshot). Then click on Run test and you will get the following screens,

    Progress bar test case drupal

    test case results drupal
  8. So this is all about the testing a simple section in Drupal 7. Hope you will like it and it can help you. I will post more test cases in future. Please let me know your comments on this.

Rate this content: 

Average: 5 (4 votes)

Add new comment