webinar bdd behat

28
BDD with Behat Webinar in cooperation of Qafoo & Zend Tobias Schlitt (@tobySen) May 8th 2013 BDD with Behat 1 / 28

Upload: shambalic

Post on 14-May-2017

238 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: webinar bdd behat

BDD with BehatWebinar in cooperation of Qafoo & Zend

Tobias Schlitt (@tobySen)

May 8th 2013

BDD with Behat 1 / 28

Page 2: webinar bdd behat

Co-founder of

Qafoopassion for software quality

Helping teams to create high quality web applications.http://qafoo.com

I Expert consultingI Individual training

Get your team bootstrapped with BDD effectively!

BDD with Behat 2 / 28

Page 3: webinar bdd behat

How to do a project

Project…0 articles

0.00 €Awesome Shop

Smartphone

1337,-- €5 items in stock

Lorem ipsum dolor sit amet,consectetur adipisicing elit,sed do eiusmod temporincididunt ut labore et doloremagna aliqua.

Comments

Lorem ipsum dolor sit amet, consecteturadipisicing elit, sed do eiusmod tempor.

☑ Unit Tests ☑ Selenium

No, no, no …!

Some time later…

Mess

BDD with Behat 3 / 28

Page 4: webinar bdd behat

Behavior Driven Development (BDD)

I Make specification executableI Combines:

I Test Driven DevelopmentI Business focus

I Uses common language forI Business peopleI AnalystsI DevelopersI Testers

BDD with Behat 4 / 28

Page 5: webinar bdd behat

Double Feedback Test Cycle

Project Owner

FailingAcceptance

Test

DeveloperFailing

Unit-Test

Make TestPass

Refactor

Make TestPass

BDD with Behat 5 / 28

Page 6: webinar bdd behat

Unit Testing

BDD with Behat 6 / 28

Page 7: webinar bdd behat

Unit tests?

I Loosing importanceI Still valid for

I LibrariesI Complex business logicI High impact code

BDD with Behat 7 / 28

Page 8: webinar bdd behat

Instability vs. Coverage

Coverage

StabilityUnitTests

FunctionalTests

AcceptanceTests

IntegrationTests

BDD with Behat 8 / 28

Page 9: webinar bdd behat

Outline

Behat

Mink

Tool Stack Overview

BDD with Behat 9 / 28

Page 10: webinar bdd behat

Behat

I BDD test framework for PHPI Inspired by Ruby’s CucumberI Work with Gherkin language frameworkI http://behat.org

BDD with Behat 10 / 28

Page 11: webinar bdd behat

Gherkin

I Domain Specific Language (DSL) frameworkI Goal: Business readableI Create custom DSL on GherkinI Tie test code to DSL sentences

BDD with Behat 11 / 28

Page 12: webinar bdd behat

Gherkin in Action

1 Feature : DateTime proceeds to end of month .2

3 Scenario : End of February i s reached4 Given i t i s date ”2013−02−01”5 When I proceed to the end of the month6 Then the date i s ”2013−02−28”

BDD with Behat 12 / 28

Page 13: webinar bdd behat

Gherkin keywords

I GivenI PreconditionI Test setup

I WhenI Behavior triggerI Test stimulus

I ThenI Expected behaviorI Describe observable result

BDD with Behat 13 / 28

Page 14: webinar bdd behat

Binding Code to Sentences

1 Feature : DateTime proceeds to end of month .2

3 Scenario : End of February i s reached4 Given i t i s date ”2013−02−01”5 When I proceed to the end of the month6 Then the date i s ”2013−02−28”

1 class ExampleContext extends BehatContext2 {

3 }

BDD with Behat 14 / 28

Page 15: webinar bdd behat

Binding Code to Sentences

1 Feature : DateTime proceeds to end of month .2

3 Scenario : End of February i s reached4 Given i t i s date ”2013−02−01”5 When I proceed to the end of the month6 Then the date i s ”2013−02−28”

1 class ExampleContext extends BehatContext2 {

3 protected $date ;4

5 /∗ ∗6 ∗ @Given / ˆ i t i s date ” ( [ ˆ ” ] ∗ ) ” $ /7 ∗ /8 public function thereIsTheDate ( $date )9 {

10 $ th is −>date = new \DateTime ( $date ) ;11 }

12 }

BDD with Behat 15 / 28

Page 16: webinar bdd behat

Binding Code to Sentences

1 Feature : DateTime proceeds to end of month .2

3 Scenario : End of February i s reached4 Given i t i s date ”2013−02−01”5 When I proceed to the end of the month6 Then the date i s ”2013−02−28”

1 class ExampleContext extends BehatContext2 {

3 /∗ ∗4 ∗ @When / ˆ I proceed to the end of the month$ /5 ∗ /6 public function iProceedToTheEndOfTheMonth ( )7 {

8 $ th is −>date−>modify ( ” l a s t day o f t h i s month ” ) ;9 }

10 }

BDD with Behat 16 / 28

Page 17: webinar bdd behat

Binding Code to Sentences

1 Feature : DateTime proceeds to end of month .2

3 Scenario : End of February i s reached4 Given i t i s date ”2013−02−01”5 When I proceed to the end of the month6 Then the date i s ”2013−02−28”

1 class ExampleContext extends BehatContext2 {

3 /∗ ∗4 ∗ @Then / ˆ the date i s ” ( ?P<date > [ ˆ ” ] ∗ ) ” $ /5 ∗ /6 public function theDateIsNow ( $date )7 {

8 \PHPUnit Framework Assert : : asser tEquals (9 $date ,

10 $ th is −>date−>format ( ”Y−m−d ” )11 ) ;12 }

13 }BDD with Behat 17 / 28

Page 18: webinar bdd behat

Behavior Driven Development

ModelDomain

DDDDomain Driven Design

Ubiquitous Language

DSLDomain Specific Language

Specification

BDD

Test

BDD with Behat 18 / 28

Page 19: webinar bdd behat

Outline

Behat

Mink

Tool Stack Overview

BDD with Behat 19 / 28

Page 20: webinar bdd behat

Mink

I Web acceptance test frameworkI Abstracts browser emulations / controllers

I GoutteI Zombie.jsI Selenium / Selenium 2I Sahi

I Mink extension for Behat provides sentencesI http://mink.behat.org

BDD with Behat 20 / 28

Page 21: webinar bdd behat

Behat Mink Example

1 Feature : Browse Wikipedia2

3 Scenario : Search f r o n t page4 Given I am on ” / ”5 When I f i l l i n ” searchInput ” w i th ” Kore ”6 And I press ” searchButton ”7 Then I should see ” Kore may r e f e r to : ”8

9 Scenario : Fol low r e d i r e c t l i n k10 Given I am on ” / ”11 When I f i l l i n ” searchInput ” w i th ” Kore ”12 And I press ” searchButton ”13 And I f o l l o w ” Kore ( energy d r i nk ) ”14 Then the response s ta tus code should be 200

BDD with Behat 21 / 28

Page 22: webinar bdd behat

Mink Behat-extension

I Mink integration for BehatI Pre-build sentences to browse pagesI Extensible with custom sentencesI http://extensions.behat.org/mink

BDD with Behat 22 / 28

Page 23: webinar bdd behat

Attention!

I This is no more BDD!I Too technicalI Not bound to the modelI Fragile for view changes

I HoweverI Useful for system testsI Awesome for refactoring!http://bit.ly/behat_cc

BDD with Behat 23 / 28

Page 24: webinar bdd behat

BDD through the Front-End

1 /∗ ∗2 ∗ @Given / ˆ I am logged i n as ” ( [ ˆ ” ] ∗ ) ” $ /3 ∗ /4 public function iAmLoggedInAs ( $user )5 {

6 $ th is −>currentUsername = $user ;7

8 return ar ray (9 new Step\Given ( ’ I am on ” / ” ’ ) ,

10 new Step\When( ’ I f i l l i n ” username ” w i th ” ’ .$user . ’ ” ’ ) ,

11 new Step\When( ’ I f i l l i n ” password ” w i th ” t e s t ” ’ ) ,12 new Step\When( ’ I press ” Login ” ’ ) ,13 ) ;14 }

BDD with Behat 24 / 28

Page 25: webinar bdd behat

Outline

Behat

Mink

Tool Stack Overview

BDD with Behat 25 / 28

Page 26: webinar bdd behat

Behat tool stack

Behat

Feature-Tests

Mink-Extension

Custom Extension

Mink

Goutte

Sahi

Application

Parse / Execute

Access

Access Frontend

BDD with Behat 26 / 28

Page 27: webinar bdd behat

Conclusion

I BDD tries to make specification executableI DDD contextI No low hanging fruitI Easy to fail

I Behat is a tool for BDD in PHPI Behat + Mink = Awesome for refactoring

BDD with Behat 27 / 28

Page 28: webinar bdd behat

Thanks for Listening

Stay in touch

I Tobias SchlittI [email protected] @tobySen

I Code Coverage with Behat:http://bit.ly/behat_cc

I Another Behat intro:http://bit.ly/behat_intro

I Slides later:http://talks.qafoo.com

Rent a web quality expert:http://qafoo.com

BDD with Behat 28 / 28