Drupal 8 ships with two testing suites. Previously Drupal only supported Simpletest
. Now there are PHPUnit tests as well. In the official change record, PHPUnit was added to provide testing without requiring a full Drupal bootstrap, which occurs with each Simpletest
test. Read the change record here: https://www.drupal.org/node/2012184.
Currently core comes with Composer dependencies prepackaged and no extra steps need to be taken to run PHPUnit. This recipe will demonstrate how to run tests the same way that the QA testbot on Drupal.org does.
The process of managing Composer dependencies may change, but is currently postponed due to Drupal.org's testing and packaging infrastructure. Read more here https://www.drupal.org/node/1475510.
Simpletest
module. Even though you might only want to run PHPUnit, this is a soft dependency for running the test runner script.php core/scripts/run-tests.sh PHPUnit
Simpletest
tests required executing the same script, however, instead of passing PHPUnit as the argument, you must define the url
option and tests
option:php core/scripts/run-tests.sh --url http://localhost --all
The run-tests.sh
script has been shipped with Drupal since 2008, then named run-functional-tests.php
. The command interacts with the other suites in Drupal to run all or specific tests and sets up other configuration items. We will highlight some of the useful options below:
http://localhost:80
Simpletest
without having to have Drupal installedThe run-tests.sh
isn't actually a shell script. It is a PHP script which is why you must execute it with PHP. In fact, within core/scripts each file is a PHP script file meant to be executed from the command line. These scripts are not intended to be run through a web server which is one of the reasons for the .sh
extension. There are issues with discovered PHP across platforms that prevent providing a shebang line to allow executing the file as a normal bash
or bat
script. For more info view this Drupal.org issue at https://www.drupal.org/node/655178.
With Drupal 8, Simpletest
can be run from SQLlite and no longer requires an installed database. This can be accomplished by passing the sqlite and dburl options to the run-tests.sh
script. This requires the PHP SQLite extension to be installed.
Here is an example adapted from the DrupalCI test runner for Drupal core:
php core/scripts/run-tests.sh --sqlite /tmp/.ht.sqlite --die-on-fail --dburl sqlite://tmp/.ht.sqlite --all
Combined with the built in PHP webserver for debugging you can run Simpletest
without a full-fledged environment.
Each example thus far has used the all
option to run every Simpletest
available. There are various ways to run specific tests:
With Drupal 8 came a new initiative to upgrade the testing infrastructure on Drupal.org. The outcome was DrupalCI. DrupalCI is open source and can be downloaded and run locally. The project page for DrupalCI is https://www.drupal.org/project/drupalci.
The test bot utilizes Docker and can be downloaded locally to run tests. The project ships with a Vagrant file to allow it to be run within a virtual machine or locally. Learn more on the testbot's project page: https://www.drupal.org/project/drupalci_testbot.
Simpletest
from the command line: https://www.drupal.org/node/645286