Behave! - BDD
DPC 2014
Tobias Schlitt (@tobySen)
2014-06-26
How a Project Typically Works
Project… 0 articles 0.00 € Awesome Shop Smartphone 1337,-- € 5 items in stockLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Comments
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor.
☑ Unit Tests ☑ Selenium
No, no, no …!
Some time later…
Behavior Driven Development (BDD)
I
Executable specification
I
Combines
I
Test Driven Development
I
Business communication
ICommon language
IBusiness people
IAnalysts
IDevelopers
ITesters
Outline
Behat
Mink
Conclusion
Behat
I
BDD test framework for PHP
I
Inspired by Ruby’s Cucumber
I
Work with Gherkin language framework
Gherkin
I
Domain Specific Language (DSL) framework
I
Goal: Business readable
I
Create custom DSL on Gherkin
Gherkin in Action
1 Feature: DateTime proceeds t o end o f month. 2
3 Scenario: End o f February i s reached 4 Given i t i s date ”2013−02
−01”
5 When I proceed t o t h e end o f t h e month 6 Then t h e date i s ”2013
−02−28”
Gherkin keywords
IGiven
IPrecondition
ITest setup
IWhen
IBehavior trigger
ITest stimulus
IThen
IExpected behavior
I
Describe observable result
IAnd
/
But
Binding Code to Sentences
1 Feature: DateTime proceeds t o end o f month.
2
3 Scenario: End o f February i s reached 4 Given i t i s date ”2013−02−01”
5 When I proceed t o t h e end o f t h e month 6 Then t h e date i s ”2013−02−28”
1 class ExampleContext extends BehatContext
2 {
Binding Code to Sentences
1 Feature: DateTime proceeds t o end o f month.
2
3 Scenario: End o f February i s reached 4 Given i t i s date ”2013−02−01”
5 When I proceed t o t h e end o f t h e month 6 Then t h e date i s ”2013−02−28”
1 class ExampleContext extends BehatContext
2 { 3 protected $date; 4 5 /∗ ∗ 6 ∗ @Given / ˆ i t i s date ” ( [ ˆ ” ]∗) ” $ / 7 ∗/ 8 p u b l i c f u n c t i o n t h e r e I s T h e D a t e($date) 9 {
10 $ t h i s−>date = new \DateTime($date) ;
11 }
Binding Code to Sentences
1 Feature: DateTime proceeds t o end o f month.
2
3 Scenario: End o f February i s reached 4 Given i t i s date ”2013−02−01”
5 When I proceed t o t h e end o f t h e month 6 Then t h e date i s ”2013−02−28”
1 class ExampleContext extends BehatContext
2 {
3 /∗ ∗
4 ∗ @When / ˆ I proceed t o t h e end o f t h e month$ /
5 ∗/
6 p u b l i c f u n c t i o n iProceedToTheEndOfTheMonth( )
7 {
8 $ t h i s−>date−>m o d i f y(” l a s t day o f t h i s month ”) ;
9 }
Binding Code to Sentences
1 Feature: DateTime proceeds t o end o f month.
2
3 Scenario: End o f February i s reached 4 Given i t i s date ”2013−02−01”
5 When I proceed t o t h e end o f t h e month 6 Then t h e date i s ”2013−02−28”
1 class ExampleContext extends BehatContext
2 {
3 /∗ ∗
4 ∗ @Then / ˆ t h e date i s ” ( ? P<date>[ ˆ ” ]∗) ” $ /
5 ∗/
6 p u b l i c f u n c t i o n theDateIsNow($date)
7 {
8 \PHPUnit Framework Assert: :a s s e r t E q u a l s(
9 $date,
10 $ t h i s−>date−>f o r m a t(” Y−m−d ”)
11 ) ;
12 }
Using Behat
I
$ path/to/behat[.phar]
Command for Behat usage
I
$ behat --init
Initialize global test infrastructure
I
$ behat --init @SomeNiceBundle
Initialize bundle test infrastructure
I
$ behat -dl
Display known sentences
I
$ behat path/to/features
Execute feature tests in directory
I
$ behat @SomeNiceBundle
Behavior Driven Development
Model
Domain
DDD
Domain Driven Design
Ubiquitous Language
DSL
Domain Specific Language
Speci
fi
cation
BDD
Outline
Behat
Mink
Mink
I
Web acceptance test framework
I
Abstracts browser emulations / controllers
I
Goutte
I
Zombie.js
I
Selenium / Selenium 2
ISahi
I
Mink extension for Behat provides sentences
Behat Mink Example
1 Feature: Browse W i k i p e d i a 2
3 Scenario: Search f r o n t page 4 Given I am on ” / ”
5 When I f i l l i n ”s e a r c h I n p u t” w i t h ”Kore” 6 And I pr es s ”s e a r c h B u t t o n”
7 Then I s h o u l d see ”Kore may r e f e r t o: ” 8
9 Scenario: F o l l o w r e d i r e c t l i n k 10 Given I am on ” / ”
11 When I f i l l i n ”s e a r c h I n p u t” w i t h ”Kore” 12 And I pr es s ”s e a r c h B u t t o n”
13 And I f o l l o w ”Kore (energy d r i n k) ”
Mink Behat-extension
I
Mink integration for Behat
I
Pre-build sentences to browse pages
I
Extensible with custom sentences
Attention!
I
This is no more BDD!
I
Too technical
I
Not bound to the model
I
Fragile for view changes
IHowever
I
Useful for system tests
IAwesome for refactoring!
BDD through the Front-End
1 /∗ ∗
2∗
@Given / ˆ I am logged i n as ” ( [ ˆ ” ]∗
) ” $ / 3∗
/ 4 p u b l i c f u n c t i o n iAmLoggedInAs($user) 5{
6 $ t h i s−
>
currentUsername = $user; 7 8 r e t u r n a r r a y(9 new Step
\
Given(’ I am on ” / ” ’) ,10 new Step
\
When(’ I f i l l i n ” username ” w i t h ” ’ .$user . ’ ” ’) ,
11 new Step
\
When(’ I f i l l i n ” password ” w i t h ” t e s t ” ’) , 12 new Step\
When(’ I p re ss ” Log in ” ’) ,13 ) ;
Outline
Behat
Mink
Unit Testing?
I
The hype is over
I
Still important for
I
Complexity
I
High impact code
Double Feedback Test Cycle
Project Owner Failing Acceptance Test Developer Failing Unit-Test Make Test Pass Refactor Make Test PassTest Mix Aspects
Coverage
Stability
Unit Tests Functional Tests Acceptance Tests Integration TestsConclusion
I
BDD makes specification executable
I
Awesome for communication
I
Awesome to abuse for front-end testing (Mink)
INo low hanging fruit
I