Entity view (Content)

Using the Drupal testing API

By Jerome
Mar. 30, 2015


As Drupal developers, knowing how to write tests could be useful because it can help to increase the quality of the code and save hours of time clicking around a site making sure things still work after making any changes.

What does Drupal offer?

Drupal has the core module "Simpletest" which offers powerful functionality through its DrupalWebTestCase, but there may be times when you want to write a traditional, simple unit test. Simpletest offers that functionality too. This module creates a complete Drupal installation and a virtual web browser and then uses the virtual web browser to walk the Drupal install through a series of tests, just like you would do if you were doing it by hand. Each test runs in a completely new Drupal instance, which is created from scratch for the test (Kind of sandbox). You have to activate the "testing" drupal core module.

Test example overview

To build a test you have to follow these steps. Create a custom module "my_module" with a .info, .module and .test files. In order to be detected by Drupal, the .test file have to be declared on the .info file with the instruction

files[]= my_module.test

Once it's done you have to create the class that will extend DrupalUnitTestCase with these instructions.

class MymoduleTestCase extends DrupalWebTestCase {

Here we'll create a sample test for an insert function wich exists on a .inc file. On the .test we must declare to Drupal the name and the purpose of our TestCase with the function getInfo().

public static function getInfo() {
    return array(
      'name' => 'Name of the test',
      'description' => 'Test description',
      'group' => 'group test',

'name' => The name of the test

'description' => the test description

'group' => The group of test

This last information is important when there is severals tests on our module.

After giving informations about our test we must write the setUp function to enable and/or includes any files required for the test.

public function setUp() {
    module_load_include('inc', 'my_module', 'my_module.crud');

We include the CRUD file and inside it there is the create function that we want to test.

function _my_module_create_publication($entry = array()) {
  try {
    $return_value = db_insert('publication')

    return $return_value;
  catch (Exception $e) {
    watchdog(t('db_insert failed. Message = %message, query= %query',
    array('%message' => $e->getMessage(), '%query' => $e->query_string)), 'error');
  return FALSE;
This is the create function from our CRUD file.

And now we can create the test function.

public function testMyModuleCreate() {
    $entry = array(
      'publication_id' => 1,
      'author_uid' => 1,
      'country' => 'CANADA',
    $insert = _my_module_create_publication($entry);
    $this->assertTrue($insert, $insert ? t('Entry inserted.') : t('Impossible to insert entry'), 'my_module CRUD');

'$entry' is data to be insert on the custom table (3 fields).

'$insert' we call the create publication function which is included on the .crud file and pass in parameter the $entry.

'assertTrue' Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).

Run the testTo run the test you have to go to /admin/config/development/testing

There will be a list of all availables tests on the site. Our test will be available on the list and just have to select him and click on "Run test". Don't forget to clear the cache between the running of tests.

Post Tags: