This session will cover Behavior-Driven Development (BDD) using Behat and the Drupal Extension to test Drupal sites. We will start with the basics behind BDD, cover what is possible now using the Drupal Extension, and finish with a roadmap for the future of the extension, as well as efforts to move much of the core testing framework to Behat in Drupal 9.
With BDD, you write human-readable stories that describe the behavior of your application. These stories can then be automated to test
Automated testing is an enticing motivator to practice Behavior-Driven Development. The most important value, however, is learning a process to help ensure that you're building (and testing) the right thing: Site features with high business or organziational values.
Testing Drupal sites, what is currently possible
The Drupal Extension extends Behat to provide step-definitions for common tasks that are specific to Drupal. Examples of this include:
- Creating nodes
- Creating users
- Logging into a site as a user with a given role or permission
- Creating taxonomy terms
Scenario: Target links within table rows
Given I am logged in as a user with the "administrator" role
When I am at "admin/structure/types"
And I click "manage fields" in the "Article" row
Then I should be on "admin/structure/types/manage/article/fields"
And I should see text matching "Add new field"
The current code to test if node form buttons are working or not can be seen here. It is long, complex, and understood only by the people that wrote the test...barely.
The following is a start at converting the above test to Behat:
Given I am logged in as a user with the "administer nodes, bypass node access" permissions
When I visit "node/add/article"
And fill in "Title" with "A sample node title"
And press "Save and publish"
Then I should see the heading "A sample node title"
Slides from the presentation are available.