March 13, 2013

There is a module Testing for creating automated tests in Drupal. It is based on the SimpleTest PHP Library.

Presence of the php-curl library on the server is a necessary condition for the operation of the module. By means of it, the module performs parsing of pages on the site.


Testing module is included in the core of Drupal 7 version, so you just need to turn it on.

drush en testing

Then on the page admin/build/testing test of all modules will be available without activation.

Creating a test

All tests should be created in the folder modulename/tests/. Name of the test file should be in the format modulename.test. SimpleTest uses object-oriented programming. Tests are written as classes, so in the file .info module, add the following code:

files[] = tests/modulename.test

Enlarge the class

To start writing the test you need to create a class and inherit from the standard class DrupalWebTestCase.

The method getInfo()

The method getInfo() is the first method which you need to implement.

class NodeViewCountTestCase extends DrupalWebTestCase {  
  public static function getInfo() {
    return array(
      'name' => 'Node view count test',
      'description' => 'Node view count module test',
      'group' => 'Node view count',

The test will not be performed without this method. We'll describe our test in this method.

  • name – test name
  • description – test description
  • group – a group that will be available to test.

The method setUp()

This method is needed to describe the various options of the test site.

public function setUp() {
  $permissions = array('administer site configuration', 'administer modules', 'access content', 'create article content', 'delete own article content', 'edit own article content');
  $user = $this->drupalCreateUser($permissions);

Here you can create users, enable certain rights, include necessary modules and themes.


This code is required to make an independent test set working in any case.

$user = $this->drupalCreateUser($permissions);

Here we create user specified rules, and log in them on the site.

Write the test

Now we need to write the test itself.

We can create nodes for testing. We will create an associative array $settings with a pair of field name => value.

$settings = array(
  'type' => 'article',
  'title' => $this->randomName(10),
  'body' => array(LANGUAGE_NONE => array(array($this->randomName(64)))),

Next, we use the method drupalCreateNode, which is designed to create a node.

$node = $this->drupalCreateNode($settings);

We can also move to different forms to fill out and submit them.

$edit = array(
  'node_view_count_node_types_options[article]' => TRUE,  
  'node_view_count_user_roles_options[2]' => TRUE,  
$this->drupalPost('admin/config/content/count-views', $edit, t('Save configuration'));

In an associative array $edit fill in the fields you want. In this example we have 2 fields of checkbox type, to make certain options checked. Using the method of drupalPost we go the page by passing certain values by POST.

Also we can go to different pages using the method drupalGet



The method module_invoke is designed for the implementation of the hooks in the Testing module, which takes 2 parameters, the name of the module and the name of the hook.

$items = module_invoke('node_view_count', 'node_view', $node, $view_mode = 'full', $langcode = 'und');

We can display a variety of information to debug messages. The method verbose is designed for it.

$this->verbose('Node created: ' . var_export($node, TRUE));




Assertions are used to verify data with the corresponding values. The method assertText checks to see if there is some text on the current page.

$this->assertText(t("@title", array('@title' => $settings['title'])), "Found title");

This method has opposite method assertNoText. The method assertNotNull checks the transmitted values​​ if they are not equal NULL.

$this->assertNotNull($result, 'Node view count is working!');

A complete list of assertions can be found here:

Running the test

To run the test, it is necessary to select the desired tests on the page admin/config/development/testing and click on "Run test". Since each test creates a clean and independent installation of Drupal, it can take a certain amount of time, but the tests should be done fast.

After the tests running is complete, we can see the results of testing.


pass – means that the test is performed as expected
fail – means that the test is not passed
exception – usually means an error in the code of the test.

Was this article helpful? Click to rate: 
Average: 5 (1 vote)

You may also like

Table of contents Preface Security principles Key security objects...
Over the past few years, e-commerce has transformed how people buy...
A year ago Dries Buytaert told that those are the content authors and...