Top Banner
BDD, Behat, & Drupal by Bozhidar Boshnakov
66

BDD, Behat & Drupal

Aug 20, 2015

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: BDD, Behat & Drupal

BDD, Behat, & Drupalby Bozhidar Boshnakov

Page 2: BDD, Behat & Drupal

About me

• Bozhidar Boshnakov• Team leader @ ProPeople• [email protected]• Skype: bo6nakov• Drupal.org – bboshnakov• linkedin.com/in/bboshnakov

Page 3: BDD, Behat & Drupal

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?

Page 4: BDD, Behat & Drupal

INTELLIGENCE IS

A LIABILITY!

Page 5: BDD, Behat & Drupal

SMART PEOPLE

Can make progress …… without process

This is not a good thing !!!

Page 6: BDD, Behat & Drupal

Professionals• Design, plan and prepare first• …then do the work

• This produces better results FASTER !!!

Page 7: BDD, Behat & Drupal

Process is the difference between

Software Engineering & Programming

Page 8: BDD, Behat & Drupal

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

Page 9: BDD, Behat & Drupal

“BDD is about implementing an application by describing its

behavior from the perspective of its stakeholders.”

Dan North

Page 10: BDD, Behat & Drupal

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

Page 11: BDD, Behat & Drupal

START TALKINGUsing the language business can understand

Page 12: BDD, Behat & Drupal

“When browsing an article user should be able to see

related articles”

Page 13: BDD, Behat & Drupal

Start asking

• Why would anyone want this feature?• Who will use this feature?• What does he/she need to use it?

Page 14: BDD, Behat & Drupal

Story narrativeIn order to have that cool feature everyone else has

Page 15: BDD, Behat & Drupal

Story narrativeIn order to read more interesting articlesAs a user

Page 16: BDD, Behat & Drupal

Story narrativeIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading

Page 17: BDD, Behat & Drupal

Story narrativeIn order to read more interesting articlesAs a readerI need to see related articles to the one I’m reading

Page 18: BDD, Behat & Drupal

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

Page 19: BDD, Behat & Drupal

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

Page 20: BDD, Behat & Drupal

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?”

Page 21: BDD, Behat & Drupal

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

Page 22: BDD, Behat & Drupal

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.

Page 23: BDD, Behat & Drupal

Gherkin Syntax

Page 24: BDD, Behat & Drupal

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

Page 25: BDD, Behat & Drupal
Page 26: BDD, Behat & Drupal

In order to verify application behaviorAs a software developer

I need tests

Preferably automated tests

Page 27: BDD, Behat & Drupal

This is where

and come in

Page 28: BDD, Behat & Drupal

Behat is an open source behavior-driven development framework for

PHP 5.3 and 5.4.Created by Konstantin Kudryashov

Page 29: BDD, Behat & Drupal

The simplest way to install Behat is through

Composer.

Before you begin, ensure that you have at least PHP 5.3.1 installed.

Page 30: BDD, Behat & Drupal

Composer.json{ "require": { "behat/mink": "*", "behat/mink-goutte-driver": "*", "behat/behat": "*", "behat/mink-extension": "*", "drupal/drupal-extension": "*" }, "minimum-stability": "dev", "config": { "bin-dir": "bin/" }}

Page 31: BDD, Behat & Drupal

Composer.phar

Then download composer.phar and run install command:

$ curl http://getcomposer.org/installer | php$ php composer.phar install

Page 32: BDD, Behat & Drupal

Behat.ymldefault: extensions: Behat\MinkExtension\Extension: base_url: http://wearepropeople.com goutte: ~ selenium2: ~ paths: features: features bootstrap: features/bootstrap

Page 33: BDD, Behat & Drupal

And the last step…

php bin/behat --init

And now you have your Features folder where you can create the magic!!!

Page 34: BDD, Behat & Drupal

What are the features?

Page 35: BDD, Behat & Drupal

Let's Have Behat Analyze Our Feature

Page 36: BDD, Behat & Drupal

Behat Creates the Glue...but the rest is up to you

Page 37: BDD, Behat & Drupal

Not so fast. What about Mink?

Page 38: BDD, Behat & Drupal

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.

Page 39: BDD, Behat & Drupal

Headless browser

emulatorsVS Browser

controllers

Page 40: BDD, Behat & Drupal

Selenium2Driver

• Control the Browser – Session• Cookies and Headers management• HTTP Authentication• Traverse the Page - Selectors• Manipulate the Page

Page 41: BDD, Behat & Drupal

Mink Context Defines Steps...for making requests

Page 42: BDD, Behat & Drupal

Mink Context Defines Steps...for interacting with forms

Page 43: BDD, Behat & Drupal

Mink Context Defines Steps...for querying the DOM

Page 44: BDD, Behat & Drupal

Mink Context Defines Steps...for examining responses

Page 45: BDD, Behat & Drupal

Now…Let’s get away from the theory and show some

action!

Page 46: BDD, Behat & Drupal

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:

Page 47: BDD, Behat & Drupal

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!

Page 48: BDD, Behat & Drupal

Behat.yml

All configuration happens inside a single configuration file in the

YAML format.

Page 49: BDD, Behat & Drupal

Imports…or share your feature configurations

Page 50: BDD, Behat & Drupal

Profiles

$bin/behat –p google features/feature1

Page 51: BDD, Behat & Drupal

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

Page 52: BDD, Behat & Drupal

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)

Page 53: BDD, Behat & Drupal

Drush: alias file

http://drush.ws/examples/example.aliases.drushrc.php

Page 54: BDD, Behat & Drupal

Drush: behat.yml

Page 55: BDD, Behat & Drupal

Drupal API: behat.yml

Page 56: BDD, Behat & Drupal

Region map: behat.yml

Page 57: BDD, Behat & Drupal

RegionsRequires a map in the behat.yml file

• label => css selector• This means regions don’t have to map directly to

Drupal theme regions

Page 58: BDD, Behat & Drupal

Structure > Blocks > (Theme) > Demonstrate block regions

Page 59: BDD, Behat & Drupal

Nodes

Page 60: BDD, Behat & Drupal

Users, roles and taxonomies

Page 61: BDD, Behat & Drupal

Relational data

Page 62: BDD, Behat & Drupal

Relational data

Page 63: BDD, Behat & Drupal

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]

Page 65: BDD, Behat & Drupal

Thank you!!!

Page 66: BDD, Behat & Drupal