BDD, Behat & Drupal

66
BDD, Behat, & Drupal by Bozhidar Boshnakov

Transcript of BDD, Behat & Drupal

BDD, Behat, & Drupalby Bozhidar Boshnakov

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?

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

• 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 TALKINGUsing 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 narrativeIn order to have that cool feature everyone else has

Story narrativeIn order to read more interesting articlesAs a user

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

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

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

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!!!

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

emulatorsVS 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!

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!

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

RegionsRequires 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:[email protected]

Thank you!!!