Moshe Weitzman, Senior Architect and Project Lead, gave a talk at DrupalCon Global 2020 to introduce Drupal Test Traits -- a new open source project started by the Commonwealth of Massachusetts. Drupal Test Traits (DTT) are designed for use with Drupal websites that have existing content, where part of your testing includes checking your content, instead of tearing your website down and reinstalling Drupal.

Why Test Traits instead of Behat?

When testing populated websites, many developers use Behat. Behat is based on use cases, to ensure that development matches the initial expectation. However, Behat doesn’t work as well for regression testing. For example, it won’t ensure that a feature keeps working after initial development. DTT exists to help fill that hole.

How do you use DTT?

Adding Drupal Test Traits to your installation takes only a few steps:

  1. To download the software, add the composer require line to your composer.json file:

    composer require weitzman/drupal-test-traits

  2. Define the variable: DTT_BASE_URL=http://example.com where example.com is your website’s base URL. This variable may be defined as you run the tests, in your Docker file, or your phpunit.xml file.

  3. To run the tests, execute the following command at the command lineac, run from the base directory of your code repository:

    phpunit --bootstrap=/bootstrap-fast.php web/modules/custom

    This runs PHPUnit, with the bootstrap option. bootstrap-fast is built into DTT, and is the bootstrap file for the phpunit test runner. You can also create a custom bootstrap-fast,if, for example, you wanted to take an action based on a test. After the bootstrap option, pass the path to test. Our example tests all of the custom modules and phpunit tests in those modules. DTT runs all of your phpunit test suites, so all of the typical command options apply.

Sample test cases

This talk breaks out three example test cases:

  • SimpleExampleTest - tests an Article type by creating a user, a vocabulary, a term for that vocabulary, and a node using the created items. Further, tests that the node returns and that the user can log in and browse appropriate pages.
  • ExampleMailTest - tests the ability of your website to send emails and their correctness.
  • ExampleSelenium2DriverTest - tests the front-end webpage. This example tests whether or not a page’s autocomplete has worked.

You can use these as a base, or create your own extensions to the test base. You can use Drupal’s API directly inside of DTT. The SimpleExampleTest case uses the API when it uses $node->setPublished()->save();.

The following additional traits are available:

  • MailCollectionTrait
  • ScreenShotTrait
  • LoginTrait (contrib)
  • QueueRunnerTrait (contrib)

For full documentation, or to support the project with your own contributions, see the Drupal Test Traits repository.


For more of Tag1's content for and about DrupalCon, see DrupalCons!


Photo by John Moeses Bauan on Unsplash