bdd, behat & drupal
TRANSCRIPT
About me
• Bozhidar Boshnakov• Team leader @ ProPeople• [email protected]• 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?
Professionals• Design, plan and prepare first• …then do the work
• This produces better results FASTER !!!
Test-Driven Development
• Write a test• Ensure the new test fails• Write code to satisfy the test• Ensure all tests pass• Refactor• Repeat
…is an iterative design process
“BDD is about implementing an application by describing its
behavior from the perspective of its stakeholders.”
Dan North
Behavior-Driven Development
• 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
…builds upon TDD
Start asking
• Why would anyone want this feature?• Who will use this feature?• What does he/she need to use it?
Story narrativeIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading
Story narrativeIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading
StoriesIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading
In order to find most interesting articles fasterAs a readerI need to be presented with high-rating articles first
In order to always provide high-quality contentAs a moderatorI need to be able to review articles before publishing
In order to protect reads from spamAs an administratorI need to differentiate humans from robots
StoriesIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading
In order to find most interesting articles fasterAs a readerI need to be presented with high-rating articles first
In order to always provide high-quality contentAs a moderatorI need to be able to review articles before publishing
In order to protect reads from spamAs an administratorI 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?”
FeatureFeature: 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
GherkinGherkin 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
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 behaviorAs a software developer
I need tests
Preferably automated tests
Behat is an open source behavior-driven development framework for
PHP 5.3 and 5.4.Created by Konstantin Kudryashov
The simplest way to install Behat is through
Composer.
Before you begin, ensure that you have at least PHP 5.3.1 installed.
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.ymldefault: extensions: Behat\MinkExtension\Extension: 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!!!
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.
Selenium2Driver
• Control the Browser – Session• Cookies and Headers management• HTTP Authentication• Traverse the Page - Selectors• Manipulate the Page
TagsTags 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:
TagsHow to execute the tags?
If a tag exists on a Feature, Behat will assign that tag to all child
Scenarios and Scenario Outlines too!
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)
RegionsRequires a map in the behat.yml file
• label => css selector• This means regions don’t have to map directly to
Drupal theme regions
If you have any questions or you want access for the full installation guideline feel
free to contact me on my email address:[email protected]
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