Accueil / Blog / Métier / Archives / Running tests with Drupal

Running tests with Drupal

Par Benoit Bryon — publié 07/12/2011, édité le 25/01/2016

Here are some notes about running tests with Drupal 7, using bundled scripts/ or drush.

Prerequisites: system setup

To try this article, you need:

  • a Drupal 7.x installation;
  • drush.

System setup is not covered by this article.

In the following article, let's say:

  • we got a Drupal 7.x website running at
  • we got drush installed and "drush" command is in the shell's PATH
  • we execute commands from Drupal root.

Create Drupal tests: example with the predictable_tests module

The creation of Drupal tests is not covered by this article.

You can follow simpletest module documentation [1].

In this article, we are to use the predictable_tests [2] module. It registers tests that always or never pass. We will use these tests to:

  • try tests that passes and others that fails;
  • use "quick" tests. Standards tests bundled with Drupal or simpletest are quite slow to run. So predictable_tests will save us time.

Install simpletest

# Download and install simpletest
drush dl simpletest
drush en simpletest

Install modules to test (here: predictable_tests)

Predictable_tests is currently at "sandbox" stage on, so you can't (or I don't know how to) download it with drush. So let's fetch it manually from Github:

# Download and install predictable_tests manually
git clone git:// sites/all/modules/predictable_tests
drush en predictable_tests

Set English as default language

There currently are issues in simpletest: testrunner fails if you don't use Drupal's default builtin language (i.e. English).
In development or integration environments (i.e. *do not do this on production!*), you can use the drush_language module:

drush dl drush_language
drush en drush_language
drush language-default en

And don't forget to switch back to the former default language later...

Running tests with scripts/

Let's consider some examples using script bundled with Drupal... We will see how to reproduce these actions with Drush later.

Notice that:

  • is not a SH script, but a PHP one!
  • If --xml folder does not exist or is not writeable, you get no errors!
  • In my case, I had to specify the php interpreter, even if:
    • "which php" returns the good result
    • I execute the command with the php interpreter, so I'd like it recognized himself...
php scripts/ --php /usr/bin/php PredictableTests
# 1 fail, 2 pass

You can export results as XML files:

# Make sure the directory exists and you can write to it.
mkdir -p ~/var/drupal/tests
ls -al ~/var/drupal/tests
# If you provide a relative path to --xml option, it is considered relative
# to Drupal root.
# If you want the output to be outside Drupal root, use an absolute path.
php scripts/ --php /usr/bin/php --xml ~/var/drupal/tests PredictableTests

Running tests with drush

Drush is the recommended way to go.

I don't know wether it is possible to execute a single test within a test group with It is easy with Drush.


Drush's help tells that we must use the --uri option, but we don't get an error if we don't... With PredictableUnitTest, it seems to work without the option, so I don't know why this option is required, but let's use it because we are told to...

# Learn usage
drush help test-run

# See available test classes
drush test-run

# Execute "PredictableTests" group of tests
drush test-run --uri= PredictableTests

# Execute only "PredictableUnitTest"
drush test-run --uri= PredictableTests

# Execute only "PredictableUnitTest" with XML output as files
# As with, value of --xml is relative to Drupal root or make
# sure to provide an absolute path.
# Notice that the --xml syntax uses a "=" with drush and a space with
mkdir -p ~/var/drupal/tests
drush test-run --uri= --xml=~/var/drupal/tests PredictableUnitTest

About simpletest's web interface

Of course, you can use the simpletest's web admin interface at /admin/config/development/testing, as suggested in the simpletest documentation.

I'm personally used to command line tools, which I think are better for administrative tasks like running tests. So I recommend using the command line.

Another reason to prefer command line tools is that they can be used for automation. In the case of tests, you'd like building a continuous integration service with Jenkins [3].

Note about module development

I haven't yet figured out where Drupal stores the information, but when you add or edit some test classes, Drupal does not discover the changes. My current workaround is to disable the enable the module. But I guess it could be done with some cache clean.

Further readings


Voir aussi
Faut-il mettre Drupal ou Symfony dans son cahier des charges ? Aucun ! Décrivez nous votre besoin ! 20/02/2017

Cela fait des années que nous développons des sites web sur la base d'un CMS ou d'un framework, ...

10 modules Drupal que vous n'utilisez (peut-être) pas assez 10 modules Drupal que vous n'utilisez (peut-être) pas assez 10/12/2014

Dans l'équipe Drupal de Makina Corpus, nous avons l'opportunité de travailler sur de nombreux ...

Gérer ses dépendances Drupal 7 avec Composer Gérer ses dépendances Drupal 7 avec Composer 08/12/2016 distribue maintenant les modules et thèmes dans son propre dépôt Packagist, voyons ...

Utiliser Migrate en Drupal 8 Utiliser Migrate en Drupal 8 20/12/2016

Trucs, astuces et points d'attention pour l'import de données avec Migrate en Drupal 8.

Utiliser des bundles Symfony dans Drupal 7 Utiliser des bundles Symfony dans Drupal 7 19/12/2016

Notre module Drupal Symfony DIC permet d'apporter la puissance de Symfony, dans Drupal, ...