Behavioral driven development with Behat

Preview:

Citation preview

Training Day DrupalGovCon 2015 July 21 & 23, 2015

Behavior Driven Development with Behat

About Promet

2

Open Source Applications – Focus on Drupal

DevOps

• Automated cloud infrastructure • Drupal deployments • Configuration management for

Drupal • Automated testing • Continuous integration • Performance and scaling • Open source tools (Chef, Git,

Jenkins)

➢ Founded in 2003 ➢ Open source technologists ➢ Drupal platform since 2008 ➢ Mobile apps since 2009

➢ Based in Chicago ➢ Distributed global team ➢ Agile development practices ➢ 24x7 support

Support

• 24x7 with SLA • Support for Drupal sites built by others • 24x7 monitoring • Drupal security and maintenance

updates • QA for custom modules • Code-level fixes • Help desk support • Ongoing development

• Drupal websites • Drupal-based web applications • Mobile apps integration • Systems integration • CMS migrations • Custom ecommerce • Prototyping and proof-of-

concepts

Custom Development

Meet Your Trainers

Allan Chappell

Senior Solutions Architect

3

Lisa Ridley

Solutions Architect

Housekeeping

Breaks

• 10:30 - Break

• 12:00 - Lunch (Boxed Lunches)

• 2:30 - Break

• 5:00 - Out-a-here

4

Rules of the Road

• Keep external distractions to a minimum

• Place your cellphones on silent

• If you must take a call, please exit the room so you do not distract the learning process for other participants

• Please check and return emails during one of the breaks

• We will have regularly scheduled breaks; however if you must take a nature break, please feel free to exit at any point; however we will proceed without you.

• Feel free to ask questions at any time; this training is intended to be interactive and by interacting with us, you will be more likely to retain what we cover

• Please help us keep discussions on topic; if you need to engage with other class participants, please make arrangements to meet after the session for extracurricular discussion

5

VM Setup

• Grab a USB Stick

• Copy the contents to a folder on your machine where you would like to work

• In a command line prompt, type

cd path/you/copied/to

vagrant box add promet_behat ./promet_behat.box

• Add promet.corp.dev your hosts file

• In your command line run

vagrant up

» When finished, navigate to http://promet.corp.dev

6

7

What is Behavior Driven Development?

Software application development using a continuous example-based communication between business customers and software developers.

Behavior Driven Development

• Rooted in agile software development

• Used to develop a solution “works” for the target user

• Ideally involves the business customer in an intimate and integrated fashion, from development to validation

8

How is this Done?

• By developing specifications with the business customer

• By requiring the customer drive the priority of product feature development

• By communicating with the customer during all phases of the project

• By writing specifications in the business speak of the customer (business domain language)

9

Business Domain Language?

• Written in the “business speak” of the client’s business world, or “domain”

• Geared toward being “business readable”

• Bridges the communications gap between product owners (clients) and developers (us) by expressing requirements in a common language that both sides understand

10

Why is this Important?

11

When the customer thinks he explained… Helps keep you from delivering… When what the customer really

needs is…

What’s the Benefit?

• Increases customer / developer mutual understanding of objectives

• Increases odds of the application you develop actually meeting the customer expectations

• Increases customer satisfaction

12

13

Goal of the BDD process

The outcome of this process, ultimately, is a well-crafted set of User Acceptance Tests that can be used to evaluate how well the application

being developed meets client expectations.

User Acceptance Tests

• Starts with a User Story

• Gets going with user story prioritization

• Then your Acceptance Criteria for your User Stories

• And Finally…write your software to comply with the Acceptance Criteria for the User Stories

14

What is a User Story?

• A User Story is the smallest piece of functionality that adds business value

• A Good User Story is a Feature of your project

15

Structure of a User Story

• As a user who…(target user role)

• I need…(business need)

• In order to….(obtain a benefit)

• Good user stories are written by the customer!

16

User Story Example

• As a student…

• I want to purchase a parking pass...

• So I can park on campus without getting a ticket or being towed

17

User Stories are Prioritized

• By the client

• Based on business importance

• With feedback from the development team

• Highest agreed priority items get worked on first

18

Next Come Acceptance Criteria

• Written before programming begins

• Defines the specific functional aspects of the user story (Feature set)

• When development starts, a Feature set is complete when all Acceptance Criteria are met (i.e, all Acceptance tests for that Feature are passed)

• Acceptance Criteria follow the “context-action-outcome” behavioral pattern

19

Structure of Acceptance Criteria

• Given that I am (context - user precondition)

• When I do this (action - performs activity)

• Then I should (outcome - see observable results)

• Think of this as a Scenario…

• …and each of the above as a Step in the Scenario

20

21

As a Drupal Developer, how do you get there?

You start with Behat!

What is Behat?

• A testing framework written in PHP

• Accepts tests written in a business domain language

• Executes those test on your application

• Designed to test the behavior of your application

22

Behat works with Mink

• Mink is an open source browser controller/emulator API for web applications

• Written in PHP

• Integrates with Behat via the Mink Extension

23

What does the Mink Extension Provide?

• Predefined “steps” that comprise the components of Acceptance Tests, or Scenarios

• Each “step” maps to a PHP callback that contains the code for the test

24

But Wait! You Said…

• Tests were written in “business speak”

• And they are….using a structured, highly readable language called Gherkin

25

What is Gherkin?

• Gherkin is a business readable, domain specific language created specifically for crafting behavior descriptions

• These behavior descriptions serve as both your product documentation and your acceptance tests

26

Behat does the Work

• Features (User Stories and Acceptance Tests) are written in Gherkin and are composed of Scenarios and their supporting Steps

• Steps are parsed by Behat using regular expressions, and mapped to PHP Callbacks

• PHP Callbacks execute test code that powers Mink

• Mink runs browser simulations to test applications

27

Behat Structure

• Behat is Pluggable, through extensions

• Mink Extension is a Behat Plugin

• Mink Extension, written in OO PHP, is extendable

28

• And the Drupal community has done so!

Behat Drupal Extension

• Integration layer between Behat, Mink Extension and Drupal

• Provides additional, Drupal specific “steps” and corresponding PHP Callbacks to the base step collection provided by the Mink Extension

29

Installing Drupal Extension

• Need to have Composer and Drush installed, and available through your environment path

• Create a composer.json file for your project in your project root

30

composer.json

{ "require": { "drupal/drupal-extension": “~3.0”, “knplabs/friendly-contexts”: “dev-master” }, "config": { "bin-dir": "bin/" } }

$> composer install

31

What does Composer Give you?

• bin/ -- contains Behat executable

• vendor/ -- contains dependencies, including Mink Extension, KNP Labs’ FriendlyContexts extension, the Drupal Extension and their dependencies

• Next you need to define your test environment parameters

32

behat.yml (bare minimum)

default: suites: default: paths: [%paths.base%/features/default] contexts: - FeatureContext - Drupal\DrupalExtension\Context\MinkContext - Drupal\DrupalExtension\Context\MessageContext - Drupal\DrupalExtension\Context\DrushContext extensions: Behat\MinkExtension\Extension: goutte: ~ selenium2: ~ base_url: http://promet.corp.dev Knp\FriendlyContexts\Extension: smartTag: smartStep Drupal\DrupalExtension\Extension: blackbox: ~

33

And you also get…

• features/ -- Directory that will house your features (user stories and scenarios) written in Gherkin

• features/bootstrap/FeatureContext.php -- Context extension that will house any of your application specific test callbacks

– Will extend DrupalExtension Context

34

FeatureContext.php

• Change Class Extension from:

class FeatureContext extends BehatContext

• to

class FeatureContext extends Drupal\ DrupalExtension\Context\DrupalContext (or whatever context you wish to extend)

35

Predefined Steps

• $bin/behat -di

or

• $bin/behat -dl

• -di provides detailed information about each step that includes all of the data included in the full doc comment

• -dl provides summary information about the structure of the step only, skipping the extraneous information in the full doc comment

36

Write your Feature

• Saved in the features/default directory in a file called <something>.feature

• Can have multiple .feature files in this directory

• Can also have multiple directories in the features folder (to house “suites”)

• All will get executed unless otherwise specified

37

Hands-On Time!

38

Hands On Behat

• Fire up your Virtual Machine, and shell in

- vagrant up

- vagrant ssh

• Change to project directory

- /var/www/sites/promet.corp.dev

• Pull up site in your browser

- http://promet.corp.dev

39

40

Hidden Location of Full Behat Test Suites

shell into the VM using “vagrant up”

cd /usr/local/promet/testing

You should see four folders:

✴ bootstrap ✴ promet_hr_role ✴ user_accounts ✴ working_at_promet

This folder can be copied to /var/www/sites/promet.corp.dev/testing to get all of the completed tests for the user requirements.

Useful Sites and Documentation

• http://docs.behat.org/en/latest/

• https://behat-drupal-extension.readthedocs.org/en/3.0/

• http://mink.behat.org/en/latest/

• https://getcomposer.org/

• http://phantomjs.org/

41

Thank you for coming!

42

Recommended