BDD with Behat

Webinar in cooperation of Qafoo & Zend

Tobias Schlitt (@tobySen)

May 8th 2013

BDD with Behat

1 / 28

Co-founder of

Qafoo
passion for software quality

Helping teams to create high quality web applications.

http://qafoo.com
Expert consulting Individual training Get your team bootstrapped with BDD effectively!

BDD with Behat

2 / 28

0 articles 0.€ 5 items in stock Comments Lorem ipsum dolor sit amet. consectetur adipisicing elit.00 € 1337. sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. no.-. consectetur adipisicing elit.How to do a project Some time later… Project… Awesome Shop Smartphone Lorem ipsum dolor sit amet. no …! BDD with Behat 3 / 28 . Mess ☑ Unit Tests ☑ Selenium No. sed do eiusmod tempor.

Behavior Driven Development (BDD) Make specification executable Combines: Test Driven Development Business focus Uses common language for Business people Analysts Developers Testers BDD with Behat 4 / 28 .

Double Feedback Test Cycle Failing Acceptance Test Failing Unit-Test Developer Refactor Project Owner Make Test Pass Make Test Pass BDD with Behat 5 / 28 .

Unit Testing BDD with Behat 6 / 28 .

Unit tests? Loosing importance Still valid for Libraries Complex business logic High impact code BDD with Behat 7 / 28 .

Instability vs. Coverage Stability Unit Tests Integration Tests Functional Tests Acceptance Tests Coverage BDD with Behat 8 / 28 .

Outline Behat Mink Tool Stack Overview BDD with Behat 9 / 28 .

org BDD with Behat 10 / 28 .Behat BDD test framework for PHP Inspired by Ruby’s Cucumber Work with Gherkin language framework http://behat.

Gherkin Domain Specific Language (DSL) framework Goal: Business readable Create custom DSL on Gherkin Tie test code to DSL sentences BDD with Behat 11 / 28 .

Gherkin in Action 1 2 3 4 5 6 Feature : DateTime proceeds t o end o f month . Scenario : Given When Then End o f February i s reached i t i s date ”2013 − 02 − 01” I proceed t o t h e end o f t h e month t h e date i s ”2013 − 02 − 28” BDD with Behat 12 / 28 .

Gherkin keywords Given Precondition Test setup When Behavior trigger Test stimulus Then Expected behavior Describe observable result BDD with Behat 13 / 28 .

Binding Code to Sentences 1 2 3 4 5 6 Feature : DateTime proceeds t o end o f month . Scenario : Given When Then End o f February i s reached i t i s date ”2013 − 02 − 01” I proceed t o t h e end o f t h e month t h e date i s ”2013 − 02 − 28” 1 2 3 class ExampleContext extends BehatContext { } BDD with Behat 14 / 28 .

/∗ ∗ ∗ @Given / ˆ i t i s date ” ( [ ˆ ” ] ∗ ) ” $ / ∗/ public function t he re Is Th eD a te ( $date ) { $ t h i s −> date = new \ DateTime ( $date ) . Scenario : Given When Then End o f February i s reached i t i s date ”2013 − 02 − 01” I proceed t o t h e end o f t h e month t h e date i s ”2013 − 02 − 28” 1 2 3 4 5 6 7 8 9 10 11 12 class ExampleContext extends BehatContext { protected $date . } } 15 / 28 BDD with Behat .Binding Code to Sentences 1 2 3 4 5 6 Feature : DateTime proceeds t o end o f month .

Binding Code to Sentences 1 2 3 4 5 6 Feature : DateTime proceeds t o end o f month . Scenario : Given When Then End o f February i s reached i t i s date ”2013 − 02 − 01” I proceed t o t h e end o f t h e month t h e date i s ”2013 − 02 − 28” 1 2 3 4 5 6 7 8 9 10 class ExampleContext extends BehatContext { /∗ ∗ ∗ @When / ˆ I proceed t o t h e end o f t h e month$ / ∗/ public function iProceedToTheEndOfTheMonth ( ) { $ t h i s −> date −> modify ( ” l a s t day o f t h i s month ” ) . } } BDD with Behat 16 / 28 .

$ t h i s −> date −> f o r m a t ( ” Y−m−d ” ) ).Binding Code to Sentences 1 2 3 4 5 6 Feature : DateTime proceeds t o end o f month . Scenario : Given When Then End o f February i s reached i t i s date ”2013 − 02 − 01” I proceed t o t h e end o f t h e month t h e date i s ”2013 − 02 − 28” 1 2 3 4 5 6 7 8 9 10 11 12 class ExampleContext extends BehatContext { /∗ ∗ ∗ @Then / ˆ t h e date i s ” ( ? P< date > [ ˆ ” ] ∗ ) ” $ / ∗/ public function theDateIsNow ( $date ) { \ PHPUnit Framework Assert : : a s s e r t E q u a l s ( $date . } 17 / 28 13 with } Behat BDD .

Behavior Driven Development Domain Driven Design DDD Domain Model Ubiquitous Language DSL Domain Specific Language Specification BDD BDD with Behat Test 18 / 28 .

Outline Behat Mink Tool Stack Overview BDD with Behat 19 / 28 .

behat.org BDD with Behat 20 / 28 .js Selenium / Selenium 2 Sahi Mink extension for Behat provides sentences http://mink.Mink Web acceptance test framework Abstracts browser emulations / controllers Goutte Zombie.

Behat Mink Example 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Feature : Browse W i k i p e d i a Scenario : Given When And Then Scenario : Given When And And Then Search f r o n t page I am on ” / ” I f i l l i n ” s e a r c h I n p u t ” w i t h ” Kore ” I press ” se a rc hB u tt o n ” I should see ” Kore may r e f e r t o : ” Follow r e d i r e c t l i n k I am on ” / ” I f i l l i n ” s e a r c h I n p u t ” w i t h ” Kore ” I press ” se a rc hB u tt o n ” I f o l l o w ” Kore ( energy d r i n k ) ” t h e response s t a t u s code should be 200 BDD with Behat 21 / 28 .

Mink Behat-extension Mink integration for Behat Pre-build sentences to browse pages Extensible with custom sentences http://extensions.behat.org/mink BDD with Behat 22 / 28 .

ly/behat_cc BDD with Behat 23 / 28 .Attention! This is no more BDD! Too technical Not bound to the model Fragile for view changes However Useful for system tests Awesome for refactoring! http://bit.

new Step \When( ’ I f i l l i n ” password ” w i t h ” t e s t ” ’ ) . new Step \When( ’ I f i l l i n ” username ” w i t h ” ’ . 11 12 13 14 } BDD with Behat 24 / 28 .BDD through the Front-End 1 2 3 4 5 6 7 8 9 10 /∗ ∗ ∗ @Given / ˆ I am logged i n as ” ( [ ˆ ” ] ∗ ) ” $ / ∗/ public function iAmLoggedInAs ( $user ) { $ t h i s −> currentUsername = $user . return array ( new Step \ Given ( ’ I am on ” / ” ’ ) . ). $user . new Step \When( ’ I press ” Login ” ’ ) . ’ ” ’ ) .

Outline Behat Mink Tool Stack Overview BDD with Behat 25 / 28 .

Behat tool stack Access Frontend Behat Mink-Extension Mink Goutte Sahi Application Custom Extension Access Parse / Execute Feature-Tests BDD with Behat 26 / 28 .

Conclusion BDD tries to make specification executable DDD context No low hanging fruit Easy to fail Behat is a tool for BDD in PHP Behat + Mink = Awesome for refactoring BDD with Behat 27 / 28 .

qafoo.ly/behat_intro Slides later: http://talks.com @tobySen http://bit.com BDD with Behat 28 / 28 .Thanks for Listening Stay in touch Code Coverage with Behat: Tobias Schlitt toby@qafoo.ly/behat_cc Another Behat intro: http://bit.com Rent a web quality expert: http://qafoo.