Top Banner
@TheMattBrunt BEHAVIOUR DRIVEN DEVELOPMENT & BEHAT TELLING STORIES THROUGH CODE
92

PHPNW16 Matt Brunt - BDD & Behat

Jan 11, 2017

Download

Technology

Matt Brunt
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: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BEHAVIOUR DRIVEN DEVELOPMENT & BEHAT

TELLING STORIES THROUGH CODE

Page 2: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Senior Software Engineer Viva IT

@TheMattBrunt @PHPem

Page 3: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BEFORE WE TALK ABOUT BDD

Page 4: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

LET’S QUICKLY TALK ABOUT TESTING

Page 5: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

TDD VS BDD

Page 6: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BDD IS TDD ‘DONE RIGHT’IT’S OFTEN SAID THAT…

Page 7: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

TDD IS BUILDING THE THING RIGHT

Page 8: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BDD IS BUILDING THE RIGHT THING

Page 9: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BEHAVIOUR DRIVEN DEVELOPMENT

Page 10: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

WE’LL BE LOOKING AT SCENARIO BDD

Page 11: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BDD IS A SECOND-GENERATION, OUTSIDE-IN, PULL-BASED, MULTIPLE-STAKEHOLDER, MULTIPLE-SCALE, HIGH-AUTOMATION, AGILE METHODOLOGY.

DAN NORTH

http://dannorth.net/whats-in-a-story/

Page 12: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SECOND-GENERATIONBREAK DOWN

Page 13: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

MULTIPLE-STAKEHOLDERBREAK DOWN

Page 14: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

AGILE METHODOLOGYBREAK DOWN

Page 15: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

MULTIPLE STAKEHOLDERS

Page 16: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

CONVERSATIONS ARE KEY

Page 17: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

HAVING CONVERSATIONS > CAPTURING

CONVERSATIONSAUTOMATING

CONVERSATIONS>

Page 18: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

HAVE CONVERSATIONS BEFORE YOU START WRITING CODE

Page 19: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

CAPTURE THE CONVERSATIONS SO YOU CAN USE THEM TO DRIVE DEVELOPMENT

Page 20: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BDD IS THE ART OF USING EXAMPLES IN CONVERSATIONS TO ILLUSTRATE BEHAVIOUR

LIZ KEOGH

Page 21: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

EXAMPLES ARE ESSENTIAL

Page 22: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SO, HOW DO WE WORK WITH THIS?

Page 23: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunthttps://github.com/cucumber/cucumber/wiki/Gherkin

GHERKIN DSL

Page 24: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

HUMAN READABLEGHERKIN IS…

Page 25: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

MULTI-LINGUAL SUPPORTTOOLS CAN INCLUDE…

Page 26: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

EN-PIRATEINCLUDING

https://github.com/Behat/Gherkin/blob/master/i18n.php#L313

Page 27: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

KEYWORD BASEDGHERKIN IS…

Page 28: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

LINE ORIENTEDGHERKIN IS…

Page 29: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

DOCUMENTATIONGHERKIN IS…

Page 30: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

AUTOMATIONGHERKIN ALLOWS…

Page 31: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

DEVELOPERS LOVE AUTOMATION

Page 32: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

WRITING STORIES

Page 33: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BE DESCRIPTIVE

Page 34: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

FEATURES ARE STORIES

Page 35: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SINGLE FEATURE PER FILE

Page 36: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Feature: As a [role] I want [feature] So that [benefit]

Page 37: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Feature: In order to [achieve value] As a [role] I want [feature]

https://lizkeogh.com/2008/05/14/rip-as-a-i-want-so-that/

Page 38: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SCENARIOS

Page 39: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SCENARIOS ARE EXAMPLES

Page 40: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

EXAMPLES ARE ESSENTIAL

Page 41: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

MULTIPLE SCENARIOSFEATURES CAN HAVE

Page 42: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

STEPS

Page 43: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

MULTIPLE STEPSSCENARIOS HAVE…

Page 44: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

KEYWORDS

Page 45: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

GIVENPUTS THE SYSTEM IN A KNOWN STATE

Page 46: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

WHENDESCRIBE A KEY ACTION

Page 47: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

THENDESCRIBE AN OBSERVABLE OUTCOME

Page 48: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Given I have a “Large T-Shirt” product

When I add a “Large T-Shirt” to my basket

Then I should have a “Large T-Shirt” in my basket

Page 49: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

ANDTHERE’S ALSO…

Page 50: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BUTTHERE’S ALSO…

Page 51: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Given I have a “Large T-Shirt” product that costs £9.99

And I have an empty basket

And I am a tax-exempt customer

When I add a “Large T-Shirt” to my basket

Then I should have a “Large T-Shirt” in my basket

And the basket total should be £9.99

But I should not have tax applied to my order

Page 52: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

STORY STRUCTURE: THE MAKEUP OF A FEATURE FILE

Page 53: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Feature: As a customer

I want to be able to add products to my basket

So that I can have a gift for my partner

Scenario: I can add a product to the basket

Given I have a "Large T-Shirt" product

When I add a “Large T-Shirt” to my basket

Then I should have a “Large T-Shirt” in my basket

FEATURE

Page 54: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SCENARIO

Feature: As a customer

I want to be able to add products to my basket

So that I can have a gift for my partner

Scenario: I can add a product to the basket

Given I have a "Large T-Shirt" product

When I add a “Large T-Shirt” to my basket

Then I should have a “Large T-Shirt” in my basket

Page 55: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

STEP

Feature: As a customer

I want to be able to add products to my basket

So that I can have a gift for my partner

Scenario: I can add a product to the basket

Given I have a "Large T-Shirt" product

When I add a “Large T-Shirt” to my basket

Then I should have a “Large T-Shirt” in my basket

Page 56: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

THIS IS GREAT, BUT HOW DO WE (DEVELOPERS) WORK WITH IT?

Page 57: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

STEP DEFINITIONS

Page 58: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Given I have a “Large T-Shirt” product

When I add that product to my basket

Then I should see that product in my basket

Page 59: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

ARGUMENTS

Page 60: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 61: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

TRANSFORMERS

Page 62: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 63: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 64: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

TAGS

Page 65: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 66: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

HOOKS

Page 67: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BEFORE & AFTER SUITES, FEATURES, SCENARIOS, STEPS

HOOKS

Page 68: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 69: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

SUITES & PROFILES

Page 70: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

TEST FEATURES WITH DIFFERENT CONFIGURATIONS

SUITES ALLOW YOU TO…

Page 71: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

THEY CAN USE THE SAME FEATURE FILES. OR THEY CAN USE DIFFERENT FEATURE FILES

FOR EXAMPLE:

Page 72: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

YOU CAN USE THE SAME FEATURE FILE TO TEST DIFFERENT IMPLEMENTATIONS

WITH SUITES AND TAGS…

Page 73: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 74: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 75: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

YOU CAN HAVE A UI SUITE THAT USES DIFFERENT STEP DEFINITIONS THAN A SERVICE / DOMAIN / LOWER LEVEL SUITE

FOR EXAMPLE:

Page 76: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 77: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Page 78: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

YOU CAN CHANGE IMPLEMENTATION WITHOUT CHANGING YOUR DOCUMENTED BUSINESS RULES

WITH SUITES AND TAGS…

Page 79: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

THIS IS WHERE THE POWER LIES

FOR ME…

Page 80: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

IMPLEMENTATION CHANGES MORE FREQUENTLY THAN BUSINESS RULES

Page 81: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

WRITING A GOOD STORY

Page 82: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Scenario: I can add a product to my basket

Given I am on the “/product/1” page

When I press “Add to basket”

Then I should see “Playstation 4”

And I should see £250

BAD STORY

Page 83: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Scenario: I can add a single product to my basket

Given I have a “Playstation 4” that costs £250

When I add the “Playstation 4” to my basket

Then I should have 1 product in my basket

And the basket total should be £250

BETTER STORY

Page 84: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

Scenario: I can add a product to my basket

Given I am on the “/product/1” page

When I press “Add to basket”

Then I should see “Playstation 4”

And I should see £250

WHAT HAPPENS WHEN THE UI CHANGES TO SAY “ADD TO CART”?

Page 85: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

DON’T WRITE IMPLEMENTATION IN FEATURES

Page 86: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

IN SUMMARY

Page 87: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

Page 88: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

STORIES TOLD BY REAL WORLD EXAMPLES.

USER STORIES AS REQUIREMENTS.

INVOLVE MULTIPLE STAKEHOLDERS.

WRITE FEATURES BEFORE CODE.

WRITE FEATURES WITHOUT IMPLEMENTATION.

Page 89: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

BEGINNING: HAVE A CONVERSATION, CAPTURE THAT CONVERSATION

MIDDLE: AUTOMATE AND IMPLEMENT THAT CONVERSATION

END: HAPPY STAKEHOLDERS AND WELL BUILT SOFTWARE

Page 90: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

LINKS & READINGhttps://cucumber.io/docs

https://adamcod.es/2014/05/15/test-doubles-mock-vs-stub.html

https://github.com/cucumber/cucumber/wiki/Gherkin

http://dannorth.net/whats-in-a-story/

http://dannorth.net/introducing-bdd/

http://lizkeogh.com/category/bdd/

http://lizkeogh.com/2014/01/22/using-bdd-with-legacy-systems/

http://inviqa.com/insights/bdd-guide

https://cucumber.io/blog/2015/03/24/single-source-of-truth

https://github.com/Behat/Gherkin/blob/master/i18n.php#L313

Page 91: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

THANKS FOR LISTENING

Page 92: PHPNW16 Matt Brunt - BDD & Behat

@TheMattBrunt

@TheMattBrunt @PHPem [email protected]

https://joind.in/talk/7c94c