PHPNW16 Matt Brunt - BDD & Behat

Preview:

Citation preview

@TheMattBrunt

BEHAVIOUR DRIVEN DEVELOPMENT & BEHAT

TELLING STORIES THROUGH CODE

@TheMattBrunt

Senior Software Engineer Viva IT

@TheMattBrunt @PHPem

@TheMattBrunt

BEFORE WE TALK ABOUT BDD

@TheMattBrunt

LET’S QUICKLY TALK ABOUT TESTING

@TheMattBrunt

TDD VS BDD

@TheMattBrunt

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

@TheMattBrunt

TDD IS BUILDING THE THING RIGHT

@TheMattBrunt

BDD IS BUILDING THE RIGHT THING

@TheMattBrunt

BEHAVIOUR DRIVEN DEVELOPMENT

@TheMattBrunt

WE’LL BE LOOKING AT SCENARIO BDD

@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/

@TheMattBrunt

SECOND-GENERATIONBREAK DOWN

@TheMattBrunt

MULTIPLE-STAKEHOLDERBREAK DOWN

@TheMattBrunt

AGILE METHODOLOGYBREAK DOWN

@TheMattBrunt

MULTIPLE STAKEHOLDERS

@TheMattBrunt

CONVERSATIONS ARE KEY

@TheMattBrunt

HAVING CONVERSATIONS > CAPTURING

CONVERSATIONSAUTOMATING

CONVERSATIONS>

@TheMattBrunt

HAVE CONVERSATIONS BEFORE YOU START WRITING CODE

@TheMattBrunt

CAPTURE THE CONVERSATIONS SO YOU CAN USE THEM TO DRIVE DEVELOPMENT

@TheMattBrunt

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

LIZ KEOGH

@TheMattBrunt

EXAMPLES ARE ESSENTIAL

@TheMattBrunt

SO, HOW DO WE WORK WITH THIS?

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

GHERKIN DSL

@TheMattBrunt

HUMAN READABLEGHERKIN IS…

@TheMattBrunt

MULTI-LINGUAL SUPPORTTOOLS CAN INCLUDE…

@TheMattBrunt

EN-PIRATEINCLUDING

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

@TheMattBrunt

KEYWORD BASEDGHERKIN IS…

@TheMattBrunt

LINE ORIENTEDGHERKIN IS…

@TheMattBrunt

DOCUMENTATIONGHERKIN IS…

@TheMattBrunt

AUTOMATIONGHERKIN ALLOWS…

@TheMattBrunt

DEVELOPERS LOVE AUTOMATION

@TheMattBrunt

WRITING STORIES

@TheMattBrunt

BE DESCRIPTIVE

@TheMattBrunt

FEATURES ARE STORIES

@TheMattBrunt

SINGLE FEATURE PER FILE

@TheMattBrunt

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

@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/

@TheMattBrunt

SCENARIOS

@TheMattBrunt

SCENARIOS ARE EXAMPLES

@TheMattBrunt

EXAMPLES ARE ESSENTIAL

@TheMattBrunt

MULTIPLE SCENARIOSFEATURES CAN HAVE

@TheMattBrunt

STEPS

@TheMattBrunt

MULTIPLE STEPSSCENARIOS HAVE…

@TheMattBrunt

KEYWORDS

@TheMattBrunt

GIVENPUTS THE SYSTEM IN A KNOWN STATE

@TheMattBrunt

WHENDESCRIBE A KEY ACTION

@TheMattBrunt

THENDESCRIBE AN OBSERVABLE OUTCOME

@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

@TheMattBrunt

ANDTHERE’S ALSO…

@TheMattBrunt

BUTTHERE’S ALSO…

@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

@TheMattBrunt

STORY STRUCTURE: THE MAKEUP OF A FEATURE FILE

@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

@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

@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

@TheMattBrunt

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

@TheMattBrunt

STEP DEFINITIONS

@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

@TheMattBrunt

ARGUMENTS

@TheMattBrunt

@TheMattBrunt

TRANSFORMERS

@TheMattBrunt

@TheMattBrunt

@TheMattBrunt

TAGS

@TheMattBrunt

@TheMattBrunt

HOOKS

@TheMattBrunt

BEFORE & AFTER SUITES, FEATURES, SCENARIOS, STEPS

HOOKS

@TheMattBrunt

@TheMattBrunt

SUITES & PROFILES

@TheMattBrunt

TEST FEATURES WITH DIFFERENT CONFIGURATIONS

SUITES ALLOW YOU TO…

@TheMattBrunt

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

FOR EXAMPLE:

@TheMattBrunt

YOU CAN USE THE SAME FEATURE FILE TO TEST DIFFERENT IMPLEMENTATIONS

WITH SUITES AND TAGS…

@TheMattBrunt

@TheMattBrunt

@TheMattBrunt

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

FOR EXAMPLE:

@TheMattBrunt

@TheMattBrunt

@TheMattBrunt

YOU CAN CHANGE IMPLEMENTATION WITHOUT CHANGING YOUR DOCUMENTED BUSINESS RULES

WITH SUITES AND TAGS…

@TheMattBrunt

THIS IS WHERE THE POWER LIES

FOR ME…

@TheMattBrunt

IMPLEMENTATION CHANGES MORE FREQUENTLY THAN BUSINESS RULES

@TheMattBrunt

WRITING A GOOD STORY

@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

@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

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

@TheMattBrunt

DON’T WRITE IMPLEMENTATION IN FEATURES

@TheMattBrunt

IN SUMMARY

@TheMattBrunt

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

CONVERSATIONS.

@TheMattBrunt

STORIES TOLD BY REAL WORLD EXAMPLES.

USER STORIES AS REQUIREMENTS.

INVOLVE MULTIPLE STAKEHOLDERS.

WRITE FEATURES BEFORE CODE.

WRITE FEATURES WITHOUT IMPLEMENTATION.

@TheMattBrunt

BEGINNING: HAVE A CONVERSATION, CAPTURE THAT CONVERSATION

MIDDLE: AUTOMATE AND IMPLEMENT THAT CONVERSATION

END: HAPPY STAKEHOLDERS AND WELL BUILT SOFTWARE

@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

@TheMattBrunt

THANKS FOR LISTENING

@TheMattBrunt

@TheMattBrunt @PHPem matt@mfyu.co.uk

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