Behavior-driven Development

Using Behat in Drupal

BDD

In software engineering, behavior-driven development (abbreviated BDD) is a software development process based on test-driven development (TDD). Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software development and management teams with shared tools and a shared process to collaborate on software development.

Wikipedia

Tools

  • Behat – BDD framework for PHP.
  • Mink – Library for integrating Behat with browser emulators such as Goutte and Selenium.
  • Drupal Extension – Provides integration between Behat, Drupal, and Mink.

Behat

It's similar to writing human-readable sentences that describe how a feature should work, what context should be given, the actions an end user should do, and finally the expected output.

Writing Behat tests

Gherkin is the syntax used when producing tests. Statements are ended by using line endings. A test is usually separated into three:

  • Feature
  • Scenario
  • Steps

Feature

Features are separated into files and are named as *.feature. The beginning of the file starts with the keyword Feature:. The next three lines after defining a feature is more like a description. These next 3 lines are not usually parsed by Behat. It's usually just a description to give readers a background of the whole feature. A feature then contains different scenarios which will be further broken down into steps.

Scenario

A scenario describes the actual tests. It starts with the keyword Scenario:. Think of it as how your application would react in a certain situation. When running Behat, you can specify which scenario it would just run. A scenario is further broken down into steps.

Steps

Steps are the way you tell the test about what context it has, the actions an end user would do, and the expected outcome. Keywords used in steps are Given, When, Then, And, and But.

Example:


@api @javascript
Feature: Calculate tax.
    As a customer,
    I need to provide valid address
    So that tax can be calculated.

    Scenario: Calculate tax.
        Given I am an anonymous user
          And I have a product with sku "FS1000" in my cart
          And I am on the commerce checkout page
        When I fill in "7732456890" for "Phone Number" in the "new_account" region
        And I fill in "bob@thejones.net" for "User ID" in the "new_account" region
        And I fill in "bob@otherjones.net" for "Contact Email" in the "new_account" region
        And I fill in "social" for "Security Question" in the "new_account" region
        And I fill in "1234" for "Security Question Answer" in the "new_account" region
        And I fill in "Test" for "First name" in the "billing" region
        And I fill in "Test" for "Last name" in the "billing" region
        And I fill in "108 5th Ave NW" for "Address 1" in the "billing" region
        And I fill in "Altoona" for "City" in the "billing" region
        And I select "IA" from "State"
        And I fill in "50009" for "ZIP Code" in the "billing" region
        And I select the ajax radio button "Manual (Cash or Check)"
        And I press the "Review Order" button
        Then I should be on the order review page
        And I should see the text "Sales tax"
        

Mink

Library for Behat which has support for various browser emulators.

  • Goutte
  • Selenium2
  • Zombie

Drupal Extension

The Drupal Extension is an integration layer between Behat, Mink Extension, and Drupal.

Drivers

  • Blackbox
  • Drush (api)
  • Drupal (api)
Feature Blackbox Drush Drupal API
Map Regions Yes Yes Yes
Create users No Yes Yes
Create nodes No No Yes
Create vocabularies No No Yes
Create taxonomy terms No No Yes
Run tests and site on different servers Yes Yes No

It's all about the DEMO!