BDD, Behat, & Drupal 
by Bozhidar Boshnakov
About me 
• Bozhidar Boshnakov 
• Team leader @ ProPeople 
• bboshnakov91@gmail.com 
• Skype: bo6nakov 
• Drupal.org – bboshnakov 
• linkedin.com/in/bboshnakov
Table of contents 
• What is Behavior-Driven Development? 
• Why Behavior-Driven Development? 
• How to BDD? 
• How to automate BDD? 
• How to do it in Drupal?
INTELLIGENCE 
IS 
A LIABILITY!
SMART PEOPLE 
Can make progress … 
… without process 
This is not a good thing !!!
Professionals 
• Design, plan and prepare first 
• …then do the work 
• This produces better results FASTER !!!
Process is the difference 
between 
Software 
Engineering &Programming
Test-Driven Development 
…is an iterative design process 
• Write a test 
• Ensure the new test fails 
• Write code to satisfy the test 
• Ensure all tests pass 
• Refactor 
• Repeat
“BDD is about implementing an 
application by describing its 
behavior from the perspective of 
its stakeholders.” 
Dan North
Behavior-Driven Development 
…builds upon TDD 
• Write test cases in a natural language 
– Understood by developers and business folks alike 
– Helps relate domain language of requirements to 
the code 
• Do this with user stories and scenarios 
– User stories describe a feature’s benefit in context 
– Scenarios are executable acceptance criteria
START TALKING 
Using the language business can understand
“When browsing an article 
user should be able to see 
related articles”
Start asking 
• Why would anyone want this feature? 
• Who will use this feature? 
• What does he/she need to use it?
Story narrative 
In order to have that cool feature everyone else has
Story narrative 
In order to read more interesting articles 
As a user
Story narrative 
In order to read more interesting articles 
As a reader 
I need to see related articles to the one I’m reading
Story narrative 
In order to read more interesting articles 
As a reader 
I need to see related articles to the one I’m reading
Stories 
In order to read more interesting articles 
As a reader 
I need to see related articles to the one I’m reading 
In order to find most interesting articles faster 
As a reader 
I need to be presented with high-rating articles first 
In order to always provide high-quality content 
As a moderator 
I need to be able to review articles before publishing 
In order to protect reads from spam 
As an administrator 
I need to differentiate humans from robots
Stories 
In order to read more interesting articles 
As a reader 
I need to see related articles to the one I’m reading 
In order to find most interesting articles faster 
As a reader 
I need to be presented with high-rating articles first 
In order to always provide high-quality content 
As a moderator 
I need to be able to review articles before publishing 
In order to protect reads from spam 
As an administrator 
I need to differentiate humans from robots 
1 
4 
3 
2
Tips & tricks 
“Can you give me an example?” 
“Is there a case in which this example wouldn’t be true?” 
“Is that the only outcome?” 
“Does the user has full control?”
Feature 
Feature: Related articles 
in order to read more interesting articles 
as a reader 
I need to see related articles to the one I’m reading 
Scenario: Seeing an article with the same word in the title 
Given there is a “DrupalCamp is awesome” article 
And there is a “DrupalCamp sessions” article 
When I read the “DrupalCamp is awesome” article 
Then the “DrupalCamp sessions” should be in the list of related articles 
Scenario: Seeing an article with same tag 
Given there is a “DrupalCamp is awesome” article tagged “DrupalCampSofia2014” 
And there is a “Drupal 8 is great” article tagged “DrupalCampSofia2014” 
When I read the “DrupalCamp is awesome” article 
Then the “Drupal 8 is great” should be in the list of related articles
Gherkin 
Gherkin is a Business Readable, Domain 
Specific Language created especially for 
behavior descriptions. It gives you the 
ability to remove logic details from 
behavior tests.
Gherkin Syntax
Gherkin Syntax 
Feature: Banana Calculator 
As Bob the Banana merchant, 
I want a calculator that can add the amount of 
bananas so that I can know how many bananas I 
currently have 
Scenario: Will add 2 banana amounts 
Given I have 3 Bananas 
When I add 5 Bananas 
Then I should have 8 Banana
In order to verify application behavior 
As a software developer 
I need tests 
Preferably automated tests
This is where 
and come in
Behat is an open source behavior-driven 
development framework for 
PHP 5.3 and 5.4. 
Created by Konstantin Kudryashov
Before you begin, ensure that you have at least PHP 5.3.1 installed. 
The simplest way to 
install Behat is through 
Composer.
Composer.json 
{ 
"require": { 
"behat/mink": "*", 
"behat/mink-goutte-driver": "*", 
"behat/behat": "*", 
"behat/mink-extension": "*", 
"drupal/drupal-extension": "*" 
}, 
"minimum-stability": "dev", 
"config": { 
"bin-dir": "bin/" 
} 
}
Composer.phar 
Then download composer.phar and run install 
command: 
$ curl http://getcomposer.org/installer | php 
$ php composer.phar install
Behat.yml 
default: 
extensions: 
BehatMinkExtensionExtension: 
base_url: http://wearepropeople.com 
goutte: ~ 
selenium2: ~ 
paths: 
features: features 
bootstrap: features/bootstrap
And the last step… 
php bin/behat --init 
And now you have your Features folder 
where you can create the magic!!!
What are the features?
Let's Have Behat Analyze Our Feature
Behat Creates the Glue 
...but the rest is up to you
Not so fast. What about Mink?
Understanding Mink 
One of the most important parts in the 
web is a browser. A browser is the window 
through which web application users 
interact with the application and other 
users.
Headless 
browser 
emulators VS Browser 
controllers
Selenium2Driver 
• Control the Browser – Session 
• Cookies and Headers management 
• HTTP Authentication 
• Traverse the Page - Selectors 
• Manipulate the Page
Mink Context Defines Steps 
...for making requests
Mink Context Defines Steps 
...for interacting with forms
Mink Context Defines Steps 
...for querying the DOM
Mink Context Defines Steps 
...for examining responses
Now… 
Let’s get away from the 
theory and show some 
action!
Tags 
Tags are a great way to organize your 
features and scenarios: 
A Scenario or Feature can have as many 
tags as you like, just separate them with 
spaces:
Tags 
How to execute the tags? 
If a tag exists on a Feature, Behat 
will assign that tag to all child 
Scenarios and Scenario Outlines 
too!
Behat.yml 
All configuration happens inside a 
single configuration file in the 
YAML format.
Imports 
…or share your feature configurations
Profiles 
$bin/behat –p google features/feature1
The Drupal extension 
• Overrides default behaviors: 
– Snippets follow Drupal coding standards 
– Mink Extension steps get synonyms for readability 
• Provides drivers to facilitate data setup 
– Blackbox 
– Drush 
– Drupal API 
• Adds support for Drupal: 
– Regions 
– Node types 
– Users and Roles 
– Taxonomy 
– Subcontexts for Contributed Modules
Provides drivers for data set up 
• Works with Drupal 6, 7, and 8 
• Connect with no privileges (local or remote) 
• Connect with Drush (local or remote) 
• Connect with the Drupal API (local only)
Drush: alias file 
http://drush.ws/examples/example.aliases.drushrc.php
Drush: behat.yml
Drupal API: behat.yml
Region map: behat.yml
Regions 
Requires a map in the behat.yml file 
• label => css selector 
• This means regions don’t have to map directly to 
Drupal theme regions
Structure > Blocks > (Theme) > Demonstrate block regions
Nodes
Users, roles and taxonomies
Relational data
Relational data
If you have any questions or you want 
access for the full installation guideline feel 
free to contact me on my email address: 
bboshnakov91@gmail.com
Resources 
• http://dspeak.com/pnwds/6-drupalextension.html 
• http://docs.behat.org/guides/7.config.html 
• http://behat.org/ 
• http://mink.behat.org/ 
• https://amsterdam2014.drupal.org/session/doing-behaviour- 
driven-development-behat
Thank you!!!
BDD, Behat & Drupal

BDD, Behat & Drupal

  • 1.
    BDD, Behat, &Drupal by Bozhidar Boshnakov
  • 2.
    About me •Bozhidar Boshnakov • Team leader @ ProPeople • bboshnakov91@gmail.com • Skype: bo6nakov • Drupal.org – bboshnakov • linkedin.com/in/bboshnakov
  • 3.
    Table of contents • What is Behavior-Driven Development? • Why Behavior-Driven Development? • How to BDD? • How to automate BDD? • How to do it in Drupal?
  • 4.
  • 5.
    SMART PEOPLE Canmake progress … … without process This is not a good thing !!!
  • 6.
    Professionals • Design,plan and prepare first • …then do the work • This produces better results FASTER !!!
  • 7.
    Process is thedifference between Software Engineering &Programming
  • 8.
    Test-Driven Development …isan iterative design process • Write a test • Ensure the new test fails • Write code to satisfy the test • Ensure all tests pass • Refactor • Repeat
  • 9.
    “BDD is aboutimplementing an application by describing its behavior from the perspective of its stakeholders.” Dan North
  • 10.
    Behavior-Driven Development …buildsupon TDD • Write test cases in a natural language – Understood by developers and business folks alike – Helps relate domain language of requirements to the code • Do this with user stories and scenarios – User stories describe a feature’s benefit in context – Scenarios are executable acceptance criteria
  • 11.
    START TALKING Usingthe language business can understand
  • 12.
    “When browsing anarticle user should be able to see related articles”
  • 13.
    Start asking •Why would anyone want this feature? • Who will use this feature? • What does he/she need to use it?
  • 14.
    Story narrative Inorder to have that cool feature everyone else has
  • 15.
    Story narrative Inorder to read more interesting articles As a user
  • 16.
    Story narrative Inorder to read more interesting articles As a reader I need to see related articles to the one I’m reading
  • 17.
    Story narrative Inorder to read more interesting articles As a reader I need to see related articles to the one I’m reading
  • 18.
    Stories In orderto read more interesting articles As a reader I need to see related articles to the one I’m reading In order to find most interesting articles faster As a reader I need to be presented with high-rating articles first In order to always provide high-quality content As a moderator I need to be able to review articles before publishing In order to protect reads from spam As an administrator I need to differentiate humans from robots
  • 19.
    Stories In orderto read more interesting articles As a reader I need to see related articles to the one I’m reading In order to find most interesting articles faster As a reader I need to be presented with high-rating articles first In order to always provide high-quality content As a moderator I need to be able to review articles before publishing In order to protect reads from spam As an administrator I need to differentiate humans from robots 1 4 3 2
  • 20.
    Tips & tricks “Can you give me an example?” “Is there a case in which this example wouldn’t be true?” “Is that the only outcome?” “Does the user has full control?”
  • 21.
    Feature Feature: Relatedarticles in order to read more interesting articles as a reader I need to see related articles to the one I’m reading Scenario: Seeing an article with the same word in the title Given there is a “DrupalCamp is awesome” article And there is a “DrupalCamp sessions” article When I read the “DrupalCamp is awesome” article Then the “DrupalCamp sessions” should be in the list of related articles Scenario: Seeing an article with same tag Given there is a “DrupalCamp is awesome” article tagged “DrupalCampSofia2014” And there is a “Drupal 8 is great” article tagged “DrupalCampSofia2014” When I read the “DrupalCamp is awesome” article Then the “Drupal 8 is great” should be in the list of related articles
  • 22.
    Gherkin Gherkin isa Business Readable, Domain Specific Language created especially for behavior descriptions. It gives you the ability to remove logic details from behavior tests.
  • 23.
  • 24.
    Gherkin Syntax Feature:Banana Calculator As Bob the Banana merchant, I want a calculator that can add the amount of bananas so that I can know how many bananas I currently have Scenario: Will add 2 banana amounts Given I have 3 Bananas When I add 5 Bananas Then I should have 8 Banana
  • 26.
    In order toverify application behavior As a software developer I need tests Preferably automated tests
  • 27.
    This is where and come in
  • 28.
    Behat is anopen source behavior-driven development framework for PHP 5.3 and 5.4. Created by Konstantin Kudryashov
  • 29.
    Before you begin,ensure that you have at least PHP 5.3.1 installed. The simplest way to install Behat is through Composer.
  • 30.
    Composer.json { "require":{ "behat/mink": "*", "behat/mink-goutte-driver": "*", "behat/behat": "*", "behat/mink-extension": "*", "drupal/drupal-extension": "*" }, "minimum-stability": "dev", "config": { "bin-dir": "bin/" } }
  • 31.
    Composer.phar Then downloadcomposer.phar and run install command: $ curl http://getcomposer.org/installer | php $ php composer.phar install
  • 32.
    Behat.yml default: extensions: BehatMinkExtensionExtension: base_url: http://wearepropeople.com goutte: ~ selenium2: ~ paths: features: features bootstrap: features/bootstrap
  • 33.
    And the laststep… php bin/behat --init And now you have your Features folder where you can create the magic!!!
  • 34.
    What are thefeatures?
  • 35.
    Let's Have BehatAnalyze Our Feature
  • 36.
    Behat Creates theGlue ...but the rest is up to you
  • 37.
    Not so fast.What about Mink?
  • 38.
    Understanding Mink Oneof the most important parts in the web is a browser. A browser is the window through which web application users interact with the application and other users.
  • 39.
    Headless browser emulatorsVS Browser controllers
  • 40.
    Selenium2Driver • Controlthe Browser – Session • Cookies and Headers management • HTTP Authentication • Traverse the Page - Selectors • Manipulate the Page
  • 41.
    Mink Context DefinesSteps ...for making requests
  • 42.
    Mink Context DefinesSteps ...for interacting with forms
  • 43.
    Mink Context DefinesSteps ...for querying the DOM
  • 44.
    Mink Context DefinesSteps ...for examining responses
  • 45.
    Now… Let’s getaway from the theory and show some action!
  • 46.
    Tags Tags area great way to organize your features and scenarios: A Scenario or Feature can have as many tags as you like, just separate them with spaces:
  • 47.
    Tags How toexecute the tags? If a tag exists on a Feature, Behat will assign that tag to all child Scenarios and Scenario Outlines too!
  • 48.
    Behat.yml All configurationhappens inside a single configuration file in the YAML format.
  • 49.
    Imports …or shareyour feature configurations
  • 50.
    Profiles $bin/behat –pgoogle features/feature1
  • 51.
    The Drupal extension • Overrides default behaviors: – Snippets follow Drupal coding standards – Mink Extension steps get synonyms for readability • Provides drivers to facilitate data setup – Blackbox – Drush – Drupal API • Adds support for Drupal: – Regions – Node types – Users and Roles – Taxonomy – Subcontexts for Contributed Modules
  • 52.
    Provides drivers fordata set up • Works with Drupal 6, 7, and 8 • Connect with no privileges (local or remote) • Connect with Drush (local or remote) • Connect with the Drupal API (local only)
  • 53.
    Drush: alias file http://drush.ws/examples/example.aliases.drushrc.php
  • 54.
  • 55.
  • 56.
  • 57.
    Regions Requires amap in the behat.yml file • label => css selector • This means regions don’t have to map directly to Drupal theme regions
  • 58.
    Structure > Blocks> (Theme) > Demonstrate block regions
  • 59.
  • 60.
    Users, roles andtaxonomies
  • 61.
  • 62.
  • 63.
    If you haveany questions or you want access for the full installation guideline feel free to contact me on my email address: bboshnakov91@gmail.com
  • 64.
    Resources • http://dspeak.com/pnwds/6-drupalextension.html • http://docs.behat.org/guides/7.config.html • http://behat.org/ • http://mink.behat.org/ • https://amsterdam2014.drupal.org/session/doing-behaviour- driven-development-behat
  • 65.

Editor's Notes

  • #5 Most of the programmers are smart and it is very important when building software. What I mean…
  • #6 This is not good for big projects
  • #7 This is not good for big projects
  • #8 This is not good for big projects
  • #9 В ТДД тестовете реално не са тестове а примери как функционалностите трябва работят! Но оставям на вас да прецените кои са силните и слабите страни на този метод на работа. TDD Defined: - Decide what the code will do Write a test that will pass if the code does that thing Run the test, see it fail Write the code Run the test see it pass TDD Provides: Design and plan before you code Document your design Proof that code implements design Encourages design of testable code
  • #10 “BDD is about implementing an application by describing its behavior from the perspective of its stakeholders.” – Dan North The original developer of BDD (Dan North) came up with the notion of BDD because he was dissatisfied with the lack of any specification within TDD of what should be tested and how. BDD – Second generation of agile methodology User stories Domain-Driven Development Extreme Programmin TDD Acceptance Driven Test Planning Continuous integration
  • #11 “BDD is about implementing an application by describing its behavior from the perspective of its stakeholders.” – Dan North The original developer of BDD (Dan North) came up with the notion of BDD because he was dissatisfied with the lack of any specification within TDD of what should be tested and how. BDD – Second generation of agile methodology User stories Domain-Driven Development Extreme Programmin TDD Acceptance Driven Test Planning Continuous integration
  • #12 Everzet Konstantin Kudryashov -> Въпроси за техният бизнес -> Започнете да доставяте софтуер който се ползва и наистина удовлетворява клиента. Не започвайте да с tools, а с разговори и въпроси!!!!
  • #25 Сега ще покажа прост пример как да опишем тези неща.
  • #26 Само бърз пример за да ви покажа нагледно как изразяваме тези стъпки в програмен код.
  • #29 И сега ще си поговорим как точно да сложим бехат на нашите локални машини.
  • #30 Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
  • #32 This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.
  • #33 Sometimes, Behat’s default configuration is not enough. Some day you’ll need some extended tools to configure your feature suite. For that day, Behat has a very powerful configuration system based on YAML configuration files and profiles.
  • #35 Every *.feature file conventionally consists of a single feature. Lines starting with the keyword Feature: (or its localized equivalent) followed by three indented lines starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts withScenario: (or localized equivalent) on a new line. You can use tags to group features and scenarios together, independent of your file and directory structure. Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And (or localized one). Behat treats them all the same, but you shouldn’t. Here is an example:
  • #39 There’s huge amount of browser emulators out there, like Goutte, Selenium, Sahi and others. They all do the same job, but do it very differently. They behave differently and have very different API’s. But, what’s more important - there’s actually 2 completely different types of browser emulators out there: Headless browser emulators Browser controllers
  • #40 First type browsers are simple pure HTTP specification implementations, likeGoutte. Those browser emulators send a real HTTP requests against an application and parse the response content. They are very simple to run and configure, because this type of emulators can be written in any available programming language and can be run through console on servers without GUI. Headless emulators have both, advantages and disadvantages. Advantages are simplicity, speed and ability to run it without the need in real browser. But this type of browsers have one big disadvantage - they have no JS/AJAX support. So, you can’t test your rich GUI web applications with headless browsers. Second browser emulators type are browser controllers. Those emulators aims to control the real browser. That’s right, a program to control another program. Browser controllers simulate user interactions on browser and are able to retrieve actual information from current browser page. Selenium and Sahi are two most famous browser controllers. The main advantage of browser controllers usage is the support for JS/AJAX interactions on page. Disadvantage is that such browser emulators require installed browser, extra configuration are usually much slower than headless counterparts.
  • #47  All of one team's tests: bin/behat features/folder A single feature: bin/behat features/folder/filename.feature A single scenario within a feature: bin/behat features/teamname/filename.feature:7 where 7 represents the first line of the scenario. Exclude the tagname if present. All tests with a certain tag: bin/behat --tags @tagname All tests except a certain tag: bin/behat --tags ~tagname Multiple tags: bin/behat --tags "@tagname1&&@tagname2&&~@tagname3" By adding an @javascript tag above the feature (for all the scenarios) or above a single scenario, the test will use Selenium and Firefox to run the test. These defaults can be changed in the behat.yml file.
  • #49 Behat tries to load behat.yml or config/behat.yml by default, or you can tell Behat where your config file is with the --config option: Paths Formatter Colors Context Imports Environment Variable
  • #50 The imports block allows you to share your feature suite configuration between projects and their test suites. All files from the imports block will be loaded by Behat and merged into yourbehat.yml config.
  • #51 Profiles Extensions
  • #52 Profiles Extensions
  • #53 Profiles Extensions
  • #54 Examples
  • #64 Profiles Extensions
  • #65 The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.
  • #66 The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.