Test Driven Development (TDD/BDD) with Drupal 8

Submitted by nmeegama on Mon, 06/13/2016 - 09:28

Test Driven Development also known as Behaviour driven development is one of the most fast developing trends for test automation. Drupal Extension is Drupal's step towards TDD. Its a lot of big words but its something very simple.

Drupal Extension

Drupal Extension is basically a library that can be used to tell Drupal in a easy way to execute commands like "login as user with administrator role". So What is Drupal Extension, It is an Extension that provides Drupal the functionality to communicate with the testing frameworks of Behat (is an open source BDD framework for PHP) and Mink (is a framework written for web applications to communicate with modern day browsers).

So lets start and see how this works. First we need to install the required software.

Installing DrupalExtension

We need to do a systemwide installation so first select a shared folder (Ex: /usr/local/share). it is recommended to create a seperate folder so that you can keep you libraries separated. Change you directory to the new folder. Now add a "composer.json" file and add the following code

  "require": {
    "drupal/drupal-extension": "~3.0"
  "config": {
    "bin-dir": "bin/"

Now run the install command

composer install

NOTE: sometimes you might get an issue as follows when you actually run tests with Drupal 8 because drupal-extension and Drupal 8 uses different versions of Symfony components. If so change the composer.json file as follows

  "require": {
    "drupal/drupal-extension": "~3.0",
    "guzzlehttp/guzzle": "^6.0@dev",
    "symfony/dependency-injection": "2.8.2",
    "symfony/event-dispatcher": "2.8.2"
  "config": {
    "bin-dir": "bin/"

Now run "composer update" to apply the changes.

Now composer should install all necessary modules and dependencies. We can check if everything went well by trying out the following command. The command should 

bin/behat --help

 Now make it accessible from anywhere by adding it to a global bin location

ln -s /<your location>/drupalextension/bin/behat /usr/local/bin/behat

Now you are good to write your automation tests

Setting up Automation scripts 

Go to your Drupal 8 installation location. Then go to sites/default folder create a folder to add the test scripts. 


cd <drupal 8 installation location>

mkdir -p sites/default/behat-tests

cd sites/default/behat-tests


This is the file where we add the Behat configuration. Basically this is how you tell Behat where to look for certain things. Add the following to the behat.yml file


vi behat.yml

# now add the following code

        - FeatureContext
        - Drupal\DrupalExtension\Context\DrupalContext
        - Drupal\DrupalExtension\Context\MinkContext
        - Drupal\DrupalExtension\Context\MessageContext
        - Drupal\DrupalExtension\Context\DrushContext
      goutte: ~
      selenium2: ~
      base_url: http://seven.l
      blackbox: ~

#save the file

#now run the following command

behat --init

The command you ran last should create a folder structure as below inside  your test folder

behat-test folder structure

 To make sure everything went well run the command to list all definition expression you can use

behat -dl

If everything is setup correctly you should get  a list similar to the next screen. This output will tell all the syntaxes you can use when writing your tests/features for a more detailed version you can use the command behat -di


Telling Behat about our web application

We need to now tell Behat about our web application. Basically we can inform this using 2 parameters in the behat.yml file as below. We need to set the base_url and the drupal_root parameters in the behat.yml file. This is no different for website running on SSL protocol

  base_url: "http://d8.local"
  goutte: ~
  selenium2: ~
    drupal_root: "/Users/nmeegama/work_repo/d8/"
  blackbox: ~

Writing Automation scripts

Now we are all ready for business. So lets get down to writing our first test. Create sample.feature and add the following code. This file should go inside the "features" folder of your test directory


Feature: Login Feature
  Scenario: Login and click on My account link
    Given I am on "/user/login"
    And I fill in "Username" with "admin"
    And I fill in "Password" with "admin"
    And I press "op"
    Then I should see the link "My account"

 Running the selenium server

As a pre requisite we also need to download and run the the Selenium server from here . We can then run it using the following command. This server needs to be up and running when we are running the tests/features


java -jar selenium-server-standalone-2.53.0.jar &

Running the tests 

Running the tests is the easiest part of it we just need to be at the root location of the test folder (in this case sites/default/behat-tests). Just run behat , this should execute all your feature by opening up your default browser. According to the sample.feature we just added the output should be something similar to below

behat command output


I have used the following references to create this blog

  • https://www.drupal.org/project/drupalextension
  • http://docs.behat.org/
  • http://mink.behat.org/
  • http://behat-drupal-extension.readthedocs.org/

I hope to continue this blog post with more advanced features like how to write your own behat commands, but this should be sufficient to get you started. So I hope you do take something away