Mock modules

Last modified: July 14, 2009 - 23:19

Sometimes, hooks we want to test are not used in any of the core modules, or core modules do not cover all the use cases of these hooks. Hopefully, our testing framework allows mock modules to be created very easily.

Creating a mock module

A mock module is a really nothing more than a Drupal module, the only difference is that it is hidden from the normal user.

Mock modules should be put in the tests subdirectory of the module they are meant to test, or in the modules/simpletest/tests directory for hooks defined by core in one of the include files.

As an example, the XML-RPC Test module, that tests XML-RPC hooks, is in modules/simpletest/tests/xmlrpc_tests.module. It's info file is written this way:

; $Id$
name = "XML-RPC Test"
description = "Support module for XML-RPC tests according to the validator1 specification."
package = Testing
version = VERSION
core = 7.x
files[] = xmlrpc_test.module
hidden = TRUE

The new hidden = TRUE property instruct the administrative interface to hide this module to the user.

In modules/simpletest/test/xmlrpc.test, we simply have:

<?php
 
/**
   * Implementation of setUp().
   */
 
function setUp() {
   
parent::setUp('xmlrpc_test');
  }
?>

To load the test modules during the setUp phase.

 
 

Drupal is a registered trademark of Dries Buytaert.