Вы находитесь на странице: 1из 28

BDD with Behat

Webinar in cooperation of Qafoo & Zend

Tobias Schlitt (@tobySen)

May 8th 2013

BDD with Behat

1 / 28

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

Co-founder of

Co-founder of Helping teams to create high quality web applications. http://qafoo.com Expert consulting Individual

Helping teams to create high quality web applications.

Expert consulting Individual training

Get your team bootstrapped with BDD effectively!

BDD with Behat

2 / 28

Expert consulting Individual training Get your team bootstrapped with BDD effectively! BDD with Behat 2 /
Expert consulting Individual training Get your team bootstrapped with BDD effectively! BDD with Behat 2 /

How to do a project

 
 
 
Project…

Project…

Mess ☑ Unit Tests Awesome Shop 0 articles 0.00 € Smartphone Lorem ipsum dolor sit

Mess

Unit Tests

Awesome Shop

0 articles

0.00 €

Smartphone

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

Comments

1337,-- € 5 items in stock

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

Selenium

 
No, no, no …!

No, no, no …!

BDD with Behat

3 / 28

adipisicing elit, sed do eiusmod tempor. ☑ Selenium   No, no, no …! BDD with Behat
adipisicing elit, sed do eiusmod tempor. ☑ Selenium   No, no, no …! BDD with Behat

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

Uses common language for Business people Analysts Developers Testers BDD with Behat 4 / 28
Uses common language for Business people Analysts Developers Testers BDD with Behat 4 / 28

Double Feedback Test Cycle

Double Feedback Test Cycle ct Owner ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass
Double Feedback Test Cycle ct Owner ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass

ct Owner

Double Feedback Test Cycle ct Owner ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass
ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass Make Test
ccep ance
Test
Failing
Developer
Unit-Tes
Refactor
Make Te
Pass
Make Test

BDD with Behat

5 / 28

ct Owner ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass Make Test BDD with
ct Owner ccep ance Test Failing Developer Unit-Tes Refactor Make Te Pass Make Test BDD with

Unit Testing

Unit Testing BDD with Behat 6 / 28
Unit Testing BDD with Behat 6 / 28
Unit Testing BDD with Behat 6 / 28

BDD with Behat

6 / 28

Unit Testing BDD with Behat 6 / 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

importance Still valid for Libraries Complex business logic High impact code BDD with Behat 7 /
importance Still valid for Libraries Complex business logic High impact code BDD with Behat 7 /

Instability vs. Coverage

ty Unit Tests Integration Tests Functional Tests Acceptance Tests Coverage
ty
Unit
Tests
Integration
Tests
Functional
Tests
Acceptance
Tests
Coverage

BDD with Behat

8 / 28

ty Unit Tests Integration Tests Functional Tests Acceptance Tests Coverage BDD with Behat 8 / 28
ty 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

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

Behat

BDD test framework for PHP Inspired by Ruby’s Cucumber Work with Gherkin language framework

 

BDD with Behat

10 / 28

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

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

Goal: Business readable Create custom DSL on Gherkin Tie test code to DSL sentences BDD with
Goal: Business readable Create custom DSL on Gherkin Tie test code to DSL sentences BDD with

Gherkin in Action

 

1

Feature :

DateTime proceeds

to

end

of

month .

 

2

3

Scenario :

End of

February

i s

reached

 

4

 

Given

i t

i s

date ”20130201”

 

5

When

I

proceed

t o

the end

of

the month

6

Then the

date

i s

”20130228”

BDD with Behat

 

12 / 28

end of the month 6 Then the date i s ”2013 − 02 − 28” BDD
end of the month 6 Then the date i s ”2013 − 02 − 28” BDD

Gherkin keywords

 

Given

Precondition Test setup

When

Behavior trigger Test stimulus

Then

Expected behavior Describe observable result

BDD with Behat

13 / 28

trigger Test stimulus Then Expected behavior Describe observable result BDD with Behat 13 / 28
trigger Test stimulus Then Expected behavior Describe observable result BDD with Behat 13 / 28

Binding Code to Sentences

 

1

Feature :

DateTime proceeds

to

end

of

month .

 

2

3

Scenario :

End of

February

i s

reached

 

4

Given

i t

i s

date ”20130201”

5

When

I

proceed

t o

the end

of

the month

6

Then the

date

i s

”20130228”

1

class

ExampleContext extends BehatContext

 

2

{

3

}

BDD with Behat

 

14 / 28

1 class ExampleContext extends BehatContext   2 { 3 } BDD with Behat   14 /
1 class ExampleContext extends BehatContext   2 { 3 } BDD with Behat   14 /

Binding Code to Sentences

 

1

Feature :

DateTime proceeds

to

end

of

month .

2

3

Scenario :

End of

February

i s

reached

4

 

Given

i t

i s

date ”20130201”

   

5

When

I

proceed

t o

the end

of

the month

6

Then the

date

i s

”20130228”

1

class

ExampleContext extends BehatContext

 

2

{

3

protected

$date ;

   

4

5

/∗∗

 

6

 

@Given

/ ˆ

i t

i s

date

( [

ˆ ” ] )

” $ /

   

7

/

8

public

function

thereIsTheDate ( $date )

 

9

{

10

 

$t h i s >date = new \ DateTime ( $date ) ;

 

11

}

12

}

BDD with Behat

 

15 / 28

s − > date = new \ DateTime ( $date ) ;   11 } 12
s − > date = new \ DateTime ( $date ) ;   11 } 12

Binding Code to Sentences

 

1

Feature :

DateTime proceeds

to

end

of

month .

 

2

3

Scenario :

End of

February

i s

reached

 

4

Given

i t

i s

date ”20130201”

 

5

When

I

proceed

t o

the end

of

the month

   

6

Then the

date

i s

”20130228”

 

1

class

ExampleContext extends BehatContext

 

2

{

3

/∗∗

4

@When

/ ˆ

I

proceed

t o

the end

of

the month$ /

 

5

/

6

public

function iProceedToTheEndOfTheMonth ( )

   

7

{

8

$this >date>modify ( ” last

8 $this − > date − > modify ( ” last day of this month ”

day

8 $this − > date − > modify ( ” last day of this month ”

of

of

this

this

month ” ) ;

 

9

}

10

}

BDD with Behat

 

16 / 28

( ” last day of this month ” ) ;   9 } 10 } BDD
( ” last day of this month ” ) ;   9 } 10 } BDD

Binding Code to Sentences

 

1

Feature :

DateTime proceeds

to

end

of

month .

2

3

Scenario :

End of

February

i s

reached

 

4

Given

i t

i s

date ”20130201”

5

When

I

proceed

t o

the end

of

the month

6

 

Then the

date

i s

”20130228”

   

1

class

ExampleContext extends BehatContext

2

{

3

/∗∗

 

4

 

@Then / ˆ the

date

i s

” ( ? P<date > [ ˆ ” ] ) ” $ /

 

5

/

6

public

function

theDateIsNow ( $date )

 

7

{

8

 

\PHPUnit Framework Assert : : assertEquals (

 

9

$date ,

 

10

$t h i s >date >fo rmat ( ” Ymd ” )

 

11

 

)

;

 

12

}

13

BDD with Behat

}

 

17 / 28

” Y − m − d ” )   11   ) ;   12 }
” Y − m − d ” )   11   ) ;   12 }

Behavior Driven Development

DD

Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s

Domain Driv

Domain Model Ubiquitous Language DSL

Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s t
Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s t
Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s t
Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s t

Domain Specic Language

Domain Driv Domain Model Ubiquitous Language DSL Domain Speci fi c Language T e s t

Test

Specication

BDD with Behat

18 / 28

Model Ubiquitous Language DSL Domain Speci fi c Language T e s t Speci fi cation
Model Ubiquitous Language DSL Domain Speci fi c Language T e s t Speci fi cation

Outline

Behat

Mink

Tool Stack Overview

BDD with Behat

19 / 28

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

Mink

Web acceptance test framework Abstracts browser emulations / controllers

Goutte Zombie.js Selenium / Selenium 2 Sahi

Mink extension for Behat provides sentences

BDD with Behat

20 / 28

/ Selenium 2 Sahi Mink extension for Behat provides sentences http://mink.behat.org BDD with Behat 20 /
/ Selenium 2 Sahi Mink extension for Behat provides sentences http://mink.behat.org BDD with Behat 20 /

Behat Mink Example

 

1

Feature :

Browse Wikipedia

 

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 hI n p ut

w it h

Kore

6

And

I

press

sea r chButton

 

7

Then

I

should see

Kore may

r e f e r

t o : ”

8

9

Scenario :

Follow

redirect

l i n k

 

10

Given

I

am on

” / ”

11

When

I

f

i

l

l

i n

s e a r c hI n p ut

w it h

Kore

12

And

I

press

sea r chButton

 

13

And

I

f o l l o w

Kore

( energy

d r i n k ) ”

 

14

Then the

response

st at u s

code should be 200

 

BDD with Behat

 

21 / 28

) ”   14 Then the response st at u s code should be 200  
) ”   14 Then the response st at u s code should be 200  

Mink Behat-extension

 

Mink integration for Behat Pre-build sentences to browse pages Extensible with custom sentences

 

BDD with Behat

22 / 28

pages Extensible with custom sentences   http://extensions.behat.org/mink BDD with Behat 22 / 28
pages Extensible with custom sentences   http://extensions.behat.org/mink BDD with Behat 22 / 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!

BDD with Behat

23 / 28

However Useful for system tests Awesome for refactoring! http://bit.ly/behat_cc BDD with Behat 23 / 28
However Useful for system tests Awesome for refactoring! http://bit.ly/behat_cc BDD with Behat 23 / 28
BDD through the Front-End /∗∗ 1 2 ∗ @Given / ˆ I am logged i
BDD through the Front-End
/∗∗
1
2
∗ @Given
/ ˆ
I
am logged
i n
as
( [
ˆ ” ] ∗ )
” $ /
∗/
3
4
public
function iAmLoggedInAs ( $user )
{
5
6
$this −>currentUsername = $user ;
7
8
return
array (
9
new Step \ Given ( ’ I
am
on
” / ”
)
,
10
new Step \When( ’ I
f
i
l
l
in
” username ”
.
with
$user
.
)
,
11
new Step \When( ’ I
f
i
l
l
in
” password ”
test ” ’ ) ,
with
12
new Step \When( ’ I
press
” Login ” ’ ) ,
13
)
;
}
14
BDD with Behat
24 / 28
” ’ ) , with ” 12 new Step \When( ’ I press ” Login ”
” ’ ) , with ” 12 new Step \When( ’ I press ” Login ”

Outline

Behat

Mink

Tool Stack Overview

BDD with Behat

25 / 28

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

Behat tool stack

Behat tool stack Behat Mink-Extension Custom Extension Parse / Execute Feature-Tests Mink Goutte Sahi

Behat

Mink-Extension

Custom Extension

Parse / Execute Feature-Tests

Parse / Execute

Feature-Tests

Feature-Tests

stack Behat Mink-Extension Custom Extension Parse / Execute Feature-Tests Mink Goutte Sahi Application Access
Mink Goutte Sahi
Mink
Goutte
Sahi

Application

stack Behat Mink-Extension Custom Extension Parse / Execute Feature-Tests Mink Goutte Sahi Application Access

Access

BDD with Behat

26 / 28

Parse / Execute Feature-Tests Mink Goutte Sahi Application Access BDD with Behat 26 / 28
Parse / Execute Feature-Tests Mink Goutte Sahi Application Access 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

Easy to fail Behat is a tool for BDD in PHP Behat + Mink = Awesome
Easy to fail Behat is a tool for BDD in PHP Behat + Mink = Awesome

Thanks for Listening

Tobias Schlitt toby@qafoo.com @tobySen

Stay in touch

Code Coverage with Behat:

Another Behat intro:

Slides later:

Rent a web quality expert:

BDD with Behat

28 / 28

Slides later: http://talks.qafoo.com Rent a web quality expert: http://qafoo.com BDD with Behat 28 / 28
Slides later: http://talks.qafoo.com Rent a web quality expert: http://qafoo.com BDD with Behat 28 / 28