Community Documentation

SimpleTest Overview

Last updated December 13, 2011. Created by boombatower on March 8, 2009.
Edited by christefano, sillygwailo, dman, bekasu. Log in to edit this page.

Testing methodology

Drupal testing focuses on functional testing instead of unit testing. What this means is that the tests are written in such a way that they test the interface as a whole instead of testing individual functions or finite pieces of code. Drupal focuses on functional testing because it is more effective for the way Drupal is written. Functional testing ends up being just as effective if not more effective than unit testing and especially so due to the code style used in Drupal. The testing framework is geared towards this type of testing and the documentation focuses on writing functional tests.

Separate testing environment

Overview of SimpleTest flow

The testing framework automatically creates a separate environment for each test to run in. This means an entirely separate set of database tables is created along with a separate files directory. Drupal is installed into that environment and the tests are performed. What this means is that the tests always start from the same environment and there is no chance for contamination from other tests. When you write your test it means that you do not have to clean up the environment at the end of each test.

Having a separate environment is necessary to ensure that each test can count on the same "starting position." It also means that changes made to the local development environment will not be present in the testing environment and cannot be tested. Tests should perform all the necessary setup (eg. creating content, creating users, etc).

Test case layout

Each test case can only have one setUp() and tearDown() implementation. This means that all tests that require the same set of modules to be enabled and test similar functionality should be grouped in the same test case. Tests that have different setup needs should be moved to a different test case, but kept in the same test file.

Test file location

All tests for a module are to be placed in a central test file placed in the root of a module directory. The file name should be in the form modulename.test. For example the test file for the node module would be modules/node/node.test. If additional test files are needed, like test modules, they should be placed in a tests directory that is also placed in the module root.

Run tests

The testing system provides two methods for running tests: a web interface and a command line script. To use the web interface navigate to admin/build/testing under site building in the menu (Drupal6), admin/config/development/testing under "configuration" (Drupal7). You can then select the tests you wish to run and click the Run tests button. You should see a nice progress bar that shows you what percentage of the tests are complete and what the results are so far.

If you prefer to use the command line script it is located in the scripts directory with the name run-tests.sh. Please read the documentation included in the file to see how to use it or from a console in the Drupal installation root type: php ./scripts/run-tests.sh

Comments

Location for tests in D8

In D8 the tests are placed in the module directory in the subfolder lib/Drupal/modulename/Tests and each class lives in a separate file. For example the tests of the block module are in core/modules/block/lib/Drupal/block/Tests/ and each test case has its own file: BlockAdminThemeTest.php, BlockCacheTest.php etc.

nobody click here