Running tests with Drupal
Here are some notes about running tests with Drupal 7, using bundled scripts/run-tests.sh or drush.
Prerequisites: system setup
To try this article, you need:
- a Drupal 7.x installation;
System setup is not covered by this article.
In the following article, let's say:
- we got a Drupal 7.x website running at http://example.com:80/
- 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.
- 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.
# 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 drupal.org, 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://github.com/makinacorpus/drupal-predictable_tests.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/run-tests.sh
Let's consider some examples using run-tests.sh script bundled with Drupal... We will see how to reproduce these actions with Drush later.
- run-tests.sh 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/run-tests.sh --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/run-tests.sh --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 run-tests.sh. 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=http://example.com/ PredictableTests # Execute only "PredictableUnitTest" drush test-run --uri=http://example.com/ PredictableTests # Execute only "PredictableUnitTest" with XML output as files # As with run-tests.sh, 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 # run-tests.sh. mkdir -p ~/var/drupal/tests drush test-run --uri=http://example.com/ --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 .
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.