Sei sulla pagina 1di 44

Practical BDD with Behat and Mink

Jeremy Mikola (@jmikola)


In order to verify application behavior
A a oftware developer
I need tests
In order to verify application behavior
A a oftware developer
I need tests
Preferably automated tests
!et"Driven Development
###i an iterative dei$n proce

%rite a tet
!et"Driven Development
###i an iterative dei$n proce

%rite a tet

&n're the new tet fails


!et"Driven Development
###i an iterative dei$n proce

%rite a tet

&n're the new tet fails

%rite code to atify the tet


!et"Driven Development
###i an iterative dei$n proce

%rite a tet

&n're the new tet fails

%rite code to atify the tet

&n're all tet pass


!et"Driven Development
###i an iterative dei$n proce

%rite a tet

&n're the new tet fails

%rite code to atify the tet

&n're all tet pass

(efactor
!et"Driven Development
###i an iterative dei$n proce

%rite a tet

&n're the new tet fails

%rite code to atify the tet

&n're all tet pass

(efactor

(epeat
Dan )orth Introd'ce BDD
I had a problem# %hile 'in$ and teachin$ a$ile
practice like tet"driven development (!DD) on
project in different environment* I kept comin$
acro the ame conf'ion and mi'ndertandin$#
Pro$rammer wanted to know+

%here to tart

%hat to tet and what not to tet

,ow m'ch to tet in one $o

%hat to call their tet

,ow to 'ndertand why a tet fail

http://dannorth.net/introducing-bdd/
I tarted 'in$ the word -behavior. in place of -tet. in
my dealin$ with !DD and/ I now had anwer to
ome of thoe !DD 0'etion+

%hat to call yo'r tet i eay 1 it2 a entence


decribin$ the ne3t behavior in which yo' are
intereted#

,ow m'ch to tet become moot 1 yo' can only


decribe o m'ch behavior in a in$le entence#

%hen a tet fail* imply work thro'$h the proce


decribed above 1 either yo' introd'ced a b'$* the
behavior moved* or the tet i no lon$er relevant#

http://dannorth.net/introducing-bdd/
Dan )orth Introd'ce BDD
Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike

,elp relate domain lan$'a$e of re0'irement to the code


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike

,elp relate domain lan$'a$e of re0'irement to the code

Do thi with 'er torie and cenario


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike

,elp relate domain lan$'a$e of re0'irement to the code

Do thi with 'er torie and cenario

4er torie decribe a feat're5 benefit in conte3t


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike

,elp relate domain lan$'a$e of re0'irement to the code

Do thi with 'er torie and cenario

4er torie decribe a feat're5 benefit in conte3t

6cenario are e3ec'table acceptance criteria


Behavior"Driven Development
###b'ild 'pon !DD

%rite tet cae in a nat'ral lan$'a$e

4ndertood by developer and b'ine folk alike

,elp relate domain lan$'a$e of re0'irement to the code

Do thi with 'er torie and cenario

4er torie decribe a feat're5 benefit in conte3t

6cenario are e3ec'table acceptance criteria


A tory2 behavior i imply it acceptance
criteria 1 if the ytem f'lfill all the
acceptance criteria* it2 behavin$ correctly7
if it doen2t* it in2t#
http://dannorth.net/introducing-bdd/

6o what doe thi look like8


&3ample+ A 9ontact :orm
contact#feat're
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form
Given I am on "/demo/contact"
When I fill in "Email" with "usere!am"le#com"
And I fill in "$essa%e" with "&ello there'"
And I "ress "Send"
(hen I should see "$essa%e sent'"
&3ample+ A 9ontact :orm
contact#feat're
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form
Given I am on "/demo/contact"
When I fill in "Email" with "usere!am"le#com"
And I fill in "$essa%e" with "&ello there'"
And I "ress "Send"
(hen I should see "$essa%e sent'"
Benefit
Role
Feature
Context
Events
Outcome
!hi i where Behat and Mink come in#
!hi i where Behat and Mink come in#
Acceptance testing (any tests)
Tests a feature by executing its scenarios'
steps in a context.
%eb acceptance tetin$ (f'nctional tet)
Driver for ;o'tte* 6ahi and 6ymfony<5
tet client#
Initiali=e >'r B'ndle %ith Behat
) a""/console behat **init Acme+emo,undle
-d src/Acme/+emo,undle/Features
* "lace your .#feature files here
-f src/Acme/+emo,undle/Features/Conte!t/FeatureConte!t#"h"
* "lace your feature related code here

%e now have a directory to hold AcmeDemoB'ndle5 feat're

Behat alo create an empty :eat're9onte3t cla* which


e3tend BehatB'ndle5 Behat9onte3t

:eat're decribe o'r behavior* b't the conte3t tell Behat


how to eval'ate o'r feat're a an e3ec'table tet
?et5 ,ave Behat Analy=e >'r :eat're
) a""/console behat src/Acme/+emo,undle/Features/contact#feature
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form / contact#feature:0
Given I am on "/demo/contact"
When I fill in "Email" with "usere!am"le#com"
And I fill in "$essa%e" with "&ello there'"
And I "ress "Send"
(hen I should see "$essa%e sent'"
1 scenario 21 undefined3
4 ste"s 24 undefined3
Behat 9reate the ;l'e
###b't the ret i 'p to yo'
/..
. Given /5I am on "265"7.3")/
./
"ublic function iAm8n2)ar%ument13
9
throw new :endin%E!ce"tion23;
<
/..
. When /5I fill in "265"7.3" with "265"7.3")/
./
"ublic function iFillInWith2)ar%ument1= )ar%ument>3
9
throw new :endin%E!ce"tion23;
<
/..
. Given /5I "ress "265"7.3")/
./
"ublic function i:ress2)ar%ument13
9
throw new :endin%E!ce"tion23;
<
/..
. (hen /5I should see "265"7.3")/
./
"ublic function iShouldSee2)ar%ument13
9
throw new :endin%E!ce"tion23;
<
?ou can im"lement ste" definitions for undefined ste"s with these sni""ets:
)ot o fat# %hat abo't Mink8
Mink9onte3t Define 6tep
###for makin$ re0'et
Pattern Description
;iven @AI am on B(8PCpa$eDEABFG)BH@ >pen pecified pa$e
%hen @AI $o to B(8PCpa$eDEABFG)BH@ >pen pecified pa$e
%hen @AI reload the pa$eH@ (eload c'rrent pa$e
%hen @AI move backward one pa$eH@ Move backward one pa$e in hitory
%hen @AI move forward one pa$eH@ Move forward one pa$e in hitory
%hen @AI pre B(8PCb'ttonD(8+EABFIJJB)K)BH@ Pree b'tton with pecified idInameItitleIaltIval'e
%hen @AI follow B(8PClinkD(8+EABFIJJB)K)BH@ 9lick link with pecified idItitleIaltIte3t
Mink9onte3t Define 6tep
###for interactin$ with form
Pattern Description
%hen @AI fill in B(8PCfieldD(8+EABFIJJB)K)B with B(8
PCval'eD(8+EABFIJJB)K)BH@
:ill in form field with pecified idInameIlabelIval'e
%hen @AI fill in B(8PCval'eD(8+EABFIJJB)K)B for B(8
PCfieldD(8+EABFIJJB)K)BH@
:ill in form field with pecified idInameIlabelIval'e
%hen @AI fill in the followin$+H@ :ill in form field with provided table
%hen @AI elect B(8PCoptionD(8+EABFIJJB)K)B from B(8
PCelectD(8+EABFIJJB)K)BH@
6elect option in elect field with pecified idInameI
labelIval'e
%hen @AI check B(8PCoptionD(8+EABFIJJB)K)BH@ 9heck checkbo3 with pecified idInameIlabelIval'e
%hen @AI 'ncheck B(8PCoptionD(8+EABFIJJB)K)BH@ 4ncheck checkbo3 with pecified idInameIlabelI
val'e
%hen @AI attach the file B(8PCpathDEABFK)B to B(8
PCfieldD(8+EABFIJJB)K)BH@
Attache file to field with pecified idInameIlabelI
val'e
Mink9onte3t Define 6tep
###for interactin$ with form
Pattern Description
%hen @AI fill in B(8PCfieldD(8+EABFIJJB)K)B with B(8
PCval'eD(8+EABFIJJB)K)BH@
:ill in form field with pecified idInameIlabelIval'e
%hen @AI fill in B(8PCval'eD(8+EABFIJJB)K)B for B(8
PCfieldD(8+EABFIJJB)K)BH@
:ill in form field with pecified idInameIlabelIval'e
%hen @AI fill in the followin$+H@ :ill in form field with provided table
%hen @AI elect B(8PCoptionD(8+EABFIJJB)K)B from B(8
PCelectD(8+EABFIJJB)K)BH@
6elect option in elect field with pecified idInameI
labelIval'e
%hen @AI check B(8PCoptionD(8+EABFIJJB)K)BH@ 9heck checkbo3 with pecified idInameIlabelIval'e
%hen @AI 'ncheck B(8PCoptionD(8+EABFIJJB)K)BH@ 4ncheck checkbo3 with pecified idInameIlabelI
val'e
%hen @AI attach the file B(8PCpathDEABFK)B to B(8
PCfieldD(8+EABFIJJB)K)BH@
Attache file to field with pecified idInameIlabelI
val'e
%hat5 miin$ here8
;herkin* the D6? Behat 'e to define behavior* pecifie two
m'lti"line ar$'ment type+ table and pytrin$
http+@@doc#behat#or$@$'ide@L#$herkin#htmlMm'ltiline"ar$'ment
When I fill in the followin%:
@ email @ usere!am"le#com @
@ messa%e @ &ello (here' @
Given lorem i"sum:
"""
(his can be a multi*line strin%
"""
Mink9onte3t Define 6tep
###for 0'eryin$ the D>M
Pattern Description
!hen @AI ho'ld ee B(8PCte3tD(8+EABFIJJB)K)B in the B(8
PCelementDEABFK)B elementH@
9heck that element with pecified 966 contain
pecified te3t
!hen @Athe B(8PCelementDEABFK)B element ho'ld
contain B(8PCval'eD(8+EABFIJJB)K)BH@
9heck that element with pecified 966 contain
pecified ,!M?
!hen @AI ho'ld ee an8 B(8PCelementDEABFK)B
elementH@
9heck that element with pecified 966 e3it on pa$e
!hen @AI ho'ld not ee an8 B(8PCelementDEABFK)B
elementH@
9heck that element with pecified 966 doen5t e3it
on pa$e
!hen @Athe B(8PCfieldD(8+EABFIJJB)K)B field ho'ld
contain B(8PCval'eD(8+EABFIJJB)K)BH@
9heck that form field with pecified idInameIlabelI
val'e ha pecified val'e
!hen @Athe B(8PCfieldD(8+EABFIJJB)K)B field ho'ld not
contain B(8PCval'eD(8+EABFIJJB)K)BH@
9heck that form field with pecified idInameIlabelI
val'e doen5t have pecified val'e
!hen @Athe B(8PCcheckbo3D(8+EABFIJJB)K)B checkbo3
ho'ld be checkedH@
9heck that checkbo3 with pecified idInameIlabelI
val'e i checked
!hen @Athe B(8PCcheckbo3D(8+EABFIJJB)K)B checkbo3
ho'ld not be checkedH@
9heck that checkbo3 with pecified idInameIlabelI
val'e i 'nchecked
Mink9onte3t Define 6tep
###for e3aminin$ repone
Pattern Description
!hen @AI ho'ld be on B(8PCpa$eDEABFG)BH@ 9heck that c'rrent pa$e path i e0'al to pecified
!hen @Athe 'rl ho'ld match B(8PCpatternD(8+
EABFIJJB)K)BH@
9heck that c'rrent pa$e path matche pattern
!hen @Athe repone tat' code ho'ld be (8
PCcodeDJdG)H@
9heck that c'rrent pa$e repone tat' i e0'al to
pecified
!hen @AI ho'ld ee B(8PCte3tD(8+EABFIJJB)K)BH@ 9heck that pa$e contain pecified te3t
!hen @AI ho'ld not ee B(8PCte3tD(8+EABFIJJB)K)BH@ 9heck that pa$e doen5t contain pecified te3t
!hen @Athe repone ho'ld contain B(8PCte3tD(8+
EABFIJJB)K)BH@
9heck that ,!M? repone contain pecified trin$
!hen @Athe repone ho'ld not contain B(8PCte3tD(8+
EABFIJJB)K)BH@
9heck that ,!M? repone doen5t contain pecified
trin$
!hen @Aprint lat reponeH@ Print lat repone to conole
!hen @Ahow lat reponeH@ >pen lat repone content in brower
!ake Advanta$e of Mink9onte3t
###for feat're that re0'ire web acceptance tetin$
AB"h"
names"ace AcmeC+emo,undleCFeaturesCConte!t;
use ,ehatC,ehat,undleCConte!tC,ehatConte!t;
/..
. Feature conte!t#
./
class FeatureConte!t e!tends ,ehatConte!t
9
<
!ake Advanta$e of Mink9onte3t
###for feat're that re0'ire web acceptance tetin$
AB"h"
names"ace AcmeC+emo,undleCFeaturesCConte!t;
use ,ehatC,ehat,undleCConte!tC$inDConte!t;
/..
. Feature conte!t#
./
class FeatureConte!t e!tends $inDConte!t
9
<
?et5 &3ec'te >'r :eat're %ith Behat
) a""/console behat Acme+emo,undle **envEtest
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form / contact#feature:0
Given I am on "/demo/contact" / FeatureConte!t::visit23
When I fill in "Email" with "usere!am"le#com" / FeatureConte!t::fillField23
Form field with id@name@label@value "Email" not found
And I fill in "$essa%e" with "&ello there'" / FeatureConte!t::fillField23
And I "ress "Send" / FeatureConte!t::"ress,utton23
(hen I should see "$essa%e sent'" / FeatureConte!t::assert:a%eContains(e!t23
1 scenario 21 failed3
4 ste"s 21 "assed= F sDi""ed= 1 failed3
) a""/console behat Acme+emo,undle **envEtest
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form / contact#feature:0
Given I am on "/demo/contact" / FeatureConte!t::visit23
When I fill in "Email" with "usere!am"le#com" / FeatureConte!t::fillField23
Form field with id@name@label@value "Email" not found
And I fill in "$essa%e" with "&ello there'" / FeatureConte!t::fillField23
And I "ress "Send" / FeatureConte!t::"ress,utton23
(hen I should see "$essa%e sent'" / FeatureConte!t::assert:a%eContains(e!t23
1 scenario 21 failed3
4 ste"s 21 "assed= F sDi""ed= 1 failed3
>f co're it fail# %e haven5t written any code yetN
!hi i the red tep of !DD#
?et5 &3ec'te >'r :eat're %ith Behat
A )ote Abo't 6tep (e'lt
###of which there are even

Success+ a definition wa fo'nd and e3ec'tin$ it did not throw an


&3ception

Undefined+ a definition co'ldn5t be fo'nd7 all 'be0'ent tep will be


kipped

Pending+ the definition threw the pecial Pendin$&3ception* which


mean yo' have work to do7 kip remainin$ tep

Failure+ a definition throw an &3ception7 Behat will kip remainin$


tep and terminate with e3it tat' L

By defa'lt* Behat relie on P,P4nit for aertion* b't yo' can roll yo'r own#

Skipped+ tep which were never e3ec'ted

Ambiguous+ m'ltiple definition matched a tep

Redundant+ m'ltiple definition hare the ame pattern


Implement the 9ontact :orm
Implement the 9ontact :orm
Implement the 9ontact :orm
?et5 !ry !hat A$ain
) a""/console behat Acme+emo,undle **envEtest
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form / contact#feature:0
Given I am on "/demo/contact" / FeatureConte!t::visit23
When I fill in "Email" with "usere!am"le#com" / FeatureConte!t::fillField23
And I fill in "$essa%e" with "&ello there'" / FeatureConte!t::fillField23
And I "ress "Send" / FeatureConte!t::"ress,utton23
(hen I should see "$essa%e sent'" / FeatureConte!t::assert:a%eContains(e!t23
1 scenario 21 "assed3
4 ste"s 24 "assed3
?et5 !ry !hat A$ain
) a""/console behat Acme+emo,undle **envEtest
Feature: Contact form
In order to contact an email address
As a visitor
I need to be able to submit a contact form
Scenario: Successfully submit the contact form / contact#feature:0
Given I am on "/demo/contact" / FeatureConte!t::visit23
When I fill in "Email" with "usere!am"le#com" / FeatureConte!t::fillField23
And I fill in "$essa%e" with "&ello there'" / FeatureConte!t::fillField23
And I "ress "Send" / FeatureConte!t::"ress,utton23
(hen I should see "$essa%e sent'" / FeatureConte!t::assert:a%eContains(e!t23
1 scenario 21 "assed3
4 ste"s 24 "assed3
;reatN >'r tet pa#
!hi i the green tep of !DD#
%hat &le 9an Mink Do8

Provide a in$le API for brower behavior

,!!P a'thentication* cookie* header* eion

Pa$e e3amination via OPath or 966 elector

Pa$e manip'lation (e#$# complete form* click* hover* dra$"and"drop)

&3itin$ driver can be 'ed interchan$eably

6ymfony< tet client 1 im'lated re0'et ervin$

;o'tte 1 headle* P,P web craper

6ahi 1 brower"control toolkit (neceary for J6)

Poible f't're driver

6eleni'm 1 another brower"control toolkit

PhantomJ6 1 headle %ebPit with J6 'pportN


!hankN
http+@@behat#or$@
http+@@mink#behat#or$@
http+@@$ith'b#com@Behat

Potrebbero piacerti anche