Skip to main content
Drupal Behat Course - Behavior-driven development (BDD)
Course

Drupal Behat Course - Behavior-driven development (BDD)

Metadrop's Drupal Behat course teaches Behavior-Driven Development — user story testing, Drupal integration, and CI pipeline setup for professional development teams.
Duration

During this course, the foundations are provided for integration with Behat (testing software on websites in an automated way), acquisition of knowledge in the definition of functionality-oriented tests through user stories, as well as their integration in a Drupal environment.

This course is aimed at professionals in the online world who are looking for an intuitive way to test their application in a language understandable by non-technical personnel, allowing it to be read and conceptualized based on a clear business model defined according to the client's specifications.

The training is distributed over two days and can be complemented with practical cases in workshop mode in subsequent days on projects indicated by the applicant.

What you will learn

The objective of this course covers the first steps to adopt Behat as a testing tool in Drupal projects. Students will have the necessary knowledge to be able to create and adapt tests in Drupal.
Basis of testing approaches for online applications
Good practices when developing a test suite
Behat test execution
Writing and adaptation of Behat's tests
Knowledge of tools and plugins related to Behat

Course modules

Testing: introduction and types

Each type of test covers a different level of the product: unit tests check the smallest parts of the code, functional tests that a specific feature does what it should, and acceptance tests that the product meets the business requirements. The latter are the ones the client can read, understand and validate. We also look at when automating pays off and when it does not.

  1. Course description
  2. Test: introduction and objectives
  3. Types of tests
    1. Unit tests
    2. Functional tests
    3. Behavior test
    4. Selection of the type of test
    5. Manual vs automatic testing

BDD: behavior-driven development

In BDD, tests are derived directly from business requirements written in plain language, so that business and technical teams start from a shared understanding of how the product should behave. Scenarios work both as automated tests and as living documentation: when a requirement changes the scenario changes, and whatever is not updated does not pass the tests.

  1. Description
  2. Methodology
  3. Description of system characteristics

User stories

We turn abstract requirements into verifiable behaviors. Starting from the "as a… I want… so that…" format, we add what makes a scenario checkable: preconditions, post-conditions, alternative scenarios and error cases. We close with the difference between verifying (the system does what the specification says) and validating (that this is what the business actually needed).

  1. Use cases
  2. As a ___ I want to ___ so that ___
  3. Preconditions
  4. Post-conditions
  5. Alternative scenarios
  6. Error cases
  7. Verification vs. validation

Introduction to Behat

Behat is the tool that runs the scenarios: BDD is the methodology, Gherkin the language the scenarios are written in, and Behat the engine that reads them and checks them against the real system. It is a natural fit for PHP environments and widely used in Drupal projects, because there are ready-made steps for nodes, users, permissions, forms and views. Mink is what decides whether a scenario needs JavaScript or not.

  1. What is Behat?
  2. Syntax
  3. Extensions - Mink
    1. Description
    2. Drivers
  4. Javascript vs. without JavaScript

Anatomy of a feature

We take a .feature file apart piece by piece: the feature describes the functionality being validated, the scenario a specific case within it, and the Given/When/Then steps the actions and expected results. From there, the tools that keep you from repeating yourself: backgrounds for shared context, scenario outlines to run the same scenario with several sets of data, and variants for error cases.

  1. File structure
  2. Features
  3. Steps
  4. Scenarios
  5. Variants
    1. Error
  6. Scenario Outlines
  7. Backgrounds
  8. Advanced steps
  9. Complete example

Practical part

First hands-on block: we set up the testing machine and write scenarios against real cases, from basic access and error checks to content visualization and navigation and forms, including those that depend on JavaScript. The rule throughout is to keep only the steps that check a real behavior and drop the decorative ones.

  1. Testing machine installation
  2. Practical cases
    1. Basics
      1. Homepage
      2. Access check
      3. Errors in tests
    2. Content
      1. Visualization
      2. Navigation
    3. Forms
      1. Search
      2. Create content
      3. Forms with JavaScript

Installation and integration in Drupal

We install Behat with Composer on a DDEV environment and go through the packages that make it work on Drupal: behat/behat as the runner, behat/mink and its extension to interact with the browser, the matching drivers, and drupal/drupal-extension, which provides the Drupal-specific steps. With that in place, the practical decision is when a headless emulator is enough and when you need to drive a real browser.

  1. Introduction
  2. Component installation
    1. Composer
    2. Behat (Behat Drupal Extension)
    3. Selenium / ChromeDriver
  3. Behat integration in Drupal
    1. Navigation emulators
    2. Types
      1. Headless browser emulators
      2. Browser controllers
    3. Drivers
      1. GoutteDriver
      2. Selenium (Javascript)
  4. Behat Drupal Extension

Configuration: behat.yml and contexts

behat.yml is where everything is declared: the suites, the contexts that provide the available steps, the extensions and the environment base URL, with separate profiles so you do not have to touch the main configuration when switching environments. We also look at where features and contexts live inside the project, and how to load the DrupalExtension contexts alongside Metadrop own library (metadrop/behat-contexts), which adds debugging and screenshot utilities.

  1. behat.yml
    1. Basic parameters
    2. Contexts «Suite»
    3. Regions
  2. Virtual host
  3. Contexts
    1. Metadrop Library (GitHub)
    2. Context integration

Execution and custom steps

We run the suite and learn to read the output: steps that pass, that fail, that stay pending because no definition matches them, and that are skipped because an earlier step failed. From there you can filter by tags or check the setup with a dry run. When no existing step fits, you write your own in the project context — with parameters, leaning on the Drupal API and on XPath — keeping the reusable ones in shared libraries instead of piling them into FeatureContext.

  1. Test execution
    1. Guzzle
    2. Selenium (Javascript)
  2. Development of custom steps
    1. Simple steps
    2. Steps with parameters
    3. Drupal API
    4. Introduction to XPath
    5. Practical cases
Answers are generated automatically by AI and may not be accurate.