Uses ubiquitous language that everyone understands
My name is Valery, and I write terrible code
Milestones
Prehistoric (before 1950s)
First high level languages
Object-oriented programming (1960s-1980s)
World Wide Web (1982)
Visual programming and low code (2000s)
AI will make it even easier
How BDD can help here?
Use common language
Driven by examples
Tests as a living documentation
Focus on what and why
Introducing Behat
PHP framework for BDD
Uses Gherkin syntax
Integration with Mink for web testing
Extensible with contexts
Gherkin Example
Feature: User authentication
In order to protect accounts
As a website administrator
I need to ensure users can login securely
Scenario: Successful login
Given I am on the login page
When I fill in "username" with "admin"
And I fill in "password" with "password"
And I press "Log in"
Then I should see "Welcome back"
<?php
use Behat\Behat\Context\Context;
use Behat\Mink\Exception\ExpectationException;
class FeatureContext implements Context
{
/**
* @Then I should see the heading :heading
*/
public function iShouldSeeTheHeading($heading) {
$page = $this->getSession()->getPage();
$found = $page->findAll('css', 'h1, h2, h3, h4, h5, h6');
foreach ($found as $element) {
if ($element->getText() === $heading) {
return;
}
}
throw new ExpectationException(
"The heading '$heading' was not found",
$this->getSession()->getDriver()
);
}
}
BDD Workflow
Write feature description and scenarios
Run Behat to generate step definitions
Implement step definitions
Run tests (they should fail initially)
Implement functionality to make tests pass
Refactor and repeat
Drupal-specific Testing
Feature: Content management
As a content editor
I need to be able to create and edit content
So that I can manage the website effectively
Scenario: Create an article
Given I am logged in as a user with the "editor" role
When I go to "node/add/article"
And I fill in "Title" with "Test Article"
And I fill in "Body" with "This is a test article"
And I press "Save"
Then I should see "Article Test Article has been created"