SimpleTest - functional and unit testing Drupal


SimpleTest (http://drupal.org/project/simpletest) - Drupal CMS module for unit testing and functional testing of Drupal-projects. The project is partly based on the principles of the same name the library for testing PHP, but should not be confused out.

SimpleTest is ideal for testing Drupal, because it offers the possibility of using Drupal API in tests. Thus, in order to obtain, for example, the data of any user we can use a standard feature user_load (), whereas without the use SimpleTest we would have to take data directly from the database.
SimpleTest for Drupal 6 is implemented as a module. In Drupal 7 - built into the kernel.

In addition to unit testing, SimpleTest, using a library cURL, allows for basic functional testing, using methods such as drupalGet (), drupalPost (), drupalPostAJAX (), drupalLogin (), drupalGetContent (), etc. It is understood that SimpleTest - not Selenium and complex functional tests do not get it.

SimpleTest is divided into two classes: DrupalUnitTestCase and DrupalWebTestCase. As the name suggests, the first class to be used for unit testing, and the second - for functional testing. Their principal difference lies in the fact that DrupalUnitTestCase requires you to connect the tested files to gain access to its functions (ie, works like a normal PHPUnit). Much more interesting is the case with DrupalWebTestCase, which connects the test initialization necessary modules Drupal, unfolds in a DB scheme with the necessary prefiksomsimpletest_ that the tests do not spoil in the database. When initializing the module, it also performs the corresponding mounting hooks module.

To create a test we need to create a directory with the module direktoriyutests / which will be stored and tests. For example:

sites / all / modules / myModule

myModule.install

myModule.module

tests /

myModule.test

For Drupal 6 it is enough. In the case of Drupal 7, we must clearly specify the path to the tests in the file. Info:

files [] = tests / myModule.test
After creating a test, do not forget to clear the cache.
The usual test is the following:

<? Php

class MyModuleTest extends DrupalWebTestCase {

  / / Description of the test

  public static function getInfo () {
    return array (
      ‘Name’ => ‘My Module Test’,

      ‘Description’ => ‘Description of test’,

      ‘Group’ => ‘My Module’

    );

  }

  / / Connection modules

  / / SetUp () is called whenever

  / / Before each test

  public function setUp () {

    parent:: setUp (’myModule’, ‘views’);

  }

  / / The actual test

  public function testMyModule () {

    $ Expected = ‘Everything is good’;

    $ Actual = my_module_function ();

    $ This-> assertEqual ($ actual, $ expected,

      t (’Function returns correct result’));

  }

}

?>

On the page admin / build / testing / we will see the appropriate group of our tests. Select it and click Run tests. Then look at the results.
Instead of printing. If you want to use TDD or BDD for your Drupal project, the SimpleTest - your clear choice.
Additional information on SimpleTest can be found here:
http://drupal.org/node/291740
http://drupal.org/node/395012
http://drupal.org/simpletest-tutorial-drupal7

Description of classes and methods:

http://contribs.rocky-shore.net/simpletest/doxy/html/classDrupalWebTestCase.html

See Also

Advertising

Archives