web acceptance testing with behat

60
AUTOMATED WEB ACCEPTANCE TESTING WITH BEHAT Fabian Kiss YMC AG | May 2012 Picture by viZZZual.com, licensed under the CC BY 2.0

Upload: fabian-kiss

Post on 08-May-2015

6.502 views

Category:

Technology


1 download

DESCRIPTION

How to support acceptance testing of web applications with the PHP tool Behat in a BDD manner. Including an overview of acceptance testing in agile software development in general. Concluding with the most frequent challenges of using Behat and similar tools in practice.

TRANSCRIPT

Page 1: Web Acceptance Testing with Behat

AUTOMATEDWEB ACCEPTANCE TESTINGWITH BEHAT

Fabian KissYMC AG | May 2012

Picture by viZZZual.com, licensed under the CC BY 2.0

Page 2: Web Acceptance Testing with Behat

TESTINGIN AGILE

Pict

ure

by S

heff

ield

Tig

er, l

icen

sed

unde

r the

CC

BY 2

.0

Page 3: Web Acceptance Testing with Behat

MIKE COHN'S AGILE TESTING PYRAMID

Automated

tests

Succeeding with Agile: Software Development Using Scrum, Addison-Wesley

Page 4: Web Acceptance Testing with Behat

MIKE COHN'S AGILE TESTING PYRAMID

Succeeding with Agile: Software Development Using Scrum, Addison-Wesley

UI

Service

Unit

Page 5: Web Acceptance Testing with Behat

MIKE COHN'S AGILE TESTING PYRAMID

Succeeding with Agile: Software Development Using Scrum, Addison-Wesley

UI

Service

Unit

least investment

most investment

Page 6: Web Acceptance Testing with Behat

MIKE COHN'S AGILE TESTING PYRAMID

Succeeding with Agile: Software Development Using Scrum, Addison-Wesley

UI

Service

Unit

least investment

most investment

Page 7: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

Manual + automated tests

Page 8: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

AcceptanceTesting

...

Unit Testing...

Usability Testing

...

Performance Testing

Page 9: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

AcceptanceTesting

...

Unit Testing...

Usability Testing

...

Performance Testing

Business-facing

Technology-facing

Supp

ort p

rogr

amm

ing Critiqu e pro duct

Page 10: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

AcceptanceTesting

...

Unit Testing...

Usability Testing

...

Performance Testing

Business-facing

Technology-facing

Supp

ort p

rogr

amm

ing Critiqu e pro duct

Page 11: Web Acceptance Testing with Behat

Manual + automated tests

Page 12: Web Acceptance Testing with Behat

Manual + automated tests

Page 13: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

AcceptanceTesting

...

Unit Testing...

Usability Testing

...

Performance Testing

Page 14: Web Acceptance Testing with Behat

BRIAN MARICK'S AGILE TESTING MATRIX

http://www.exampler.com/old-blog/2003/08/21/

AcceptanceTesting

...

Unit Testing...

Usability Testing

...

Performance Testing

BDD

TDD/BDD

Page 15: Web Acceptance Testing with Behat

BDD

Page 16: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDD

Page 17: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDD

RSpec

Page 18: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDDCucumber

Behat

RSpec

Page 19: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDDCucumber

Behat

Gherkin DSL

RSpec

Page 20: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDDCucumber

Behat

Gherkin DSL

RSpec

Page 21: Web Acceptance Testing with Behat

BDD

Specification-oriented BDD

Scenario-oriented BDDCucumber

Behat

Gherkin DSL

RSpec

...

.. .

Page 22: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 23: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 24: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 25: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 26: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 27: Web Acceptance Testing with Behat

GHERKIN SYNTAX + SEMANTICS

http://docs.behat.org/guides/1.gherkin.html

Scenario: Some determinable business situation    Given some precondition      And some other precondition     When some action by the actor      And some other action     Then some testable outcome is achieved      And something else we can check happens too 

Page 28: Web Acceptance Testing with Behat

GHERKIN EXAMPLE

http://docs.behat.org/guides/1.gherkin.html

Scenario: Wilson posts to his own blog  Given I am logged in as Wilson  When I try to post to "Expensive Therapy"  Then I should see "Your article was published."

Scenario: Wilson fails to post to somebody else's blog  Given I am logged in as Wilson  When I try to post to "Greg's anti­tax rants"  Then I should see "Hey! That's not your blog!"

Page 29: Web Acceptance Testing with Behat

GHERKIN EXAMPLE

http://docs.behat.org/guides/1.gherkin.html

Scenario: Wilson posts to his own blog  Given I am logged in as Wilson  When I try to post to "Expensive Therapy"  Then I should see "Your article was published."

Scenario: Wilson fails to post to somebody else's blog  Given I am logged in as Wilson  When I try to post to "Greg's anti­tax rants"  Then I should see "Hey! That's not your blog!"

Feature

Page 30: Web Acceptance Testing with Behat

STEPS & STEP DEFINITIONS

http://docs.behat.org/quick_intro.html#basic-usage

Given some precondition

/** * @Given /^some precondition$/ */public function somePrecondition(){    //...}

Page 31: Web Acceptance Testing with Behat

STEPS & STEP DEFINITIONS

http://docs.behat.org/quick_intro.html#basic-usage

Given some precondition

/** * @Given /^some precondition$/ */public function somePrecondition(){    //...}

Step

Step definition

Page 32: Web Acceptance Testing with Behat

STEPS & STEP DEFINITIONS

http://docs.behat.org/quick_intro.html#basic-usage

Given some precondition

/** * @Given /^some precondition$/ */public function somePrecondition(){    //...}

Step

Step definition

Matching

Run

Page 33: Web Acceptance Testing with Behat

STEP ARGUMENTS

http://docs.behat.org/quick_intro.html#some-more-behat-basics

When I try to post to "Expensive Therapy"

/** * @When /^I try to post to "([^"]*)"$/ */public function iTryToPostTo($site){    //...}

Page 34: Web Acceptance Testing with Behat

STEP ARGUMENTS

http://docs.behat.org/quick_intro.html#some-more-behat-basics

When I try to post to "Expensive Therapy"

/** * @When /^I try to post to "([^"]*)"$/ */public function iTryToPostTo($blog){    //...}

Argument

Page 35: Web Acceptance Testing with Behat

SCENARIO BACKGROUNDS

http://docs.behat.org/guides/1.gherkin.html#backgrounds

Background:  Given I am logged in as Wilson

Scenario: Wilson posts to his own blog  When I try to post to "Expensive Therapy"  Then I should see "Your article was published."

Scenario: Wilson fails to post to somebody else's blog  When I try to post to "Greg's anti­tax rants"  Then I should see "Hey! That's not your blog!"

Page 36: Web Acceptance Testing with Behat

SCENARIO BACKGROUNDS

http://docs.behat.org/guides/1.gherkin.html#backgrounds

Background:  Given I am logged in as Wilson

Scenario: Wilson posts to his own blog  When I try to post to "Expensive Therapy"  Then I should see "Your article was published."

Scenario: Wilson fails to post to somebody else's blog  When I try to post to "Greg's anti­tax rants"  Then I should see "Hey! That's not your blog!"

Page 37: Web Acceptance Testing with Behat

SCENARIO OUTLINES

http://docs.behat.org/guides/1.gherkin.html#scenario-outlines

Scenario Outline: Wilson posts to a blog  Given I am logged in as Wilson  When I try to post to "<blog>"  Then I should see "<authorization>"

  Examples:   | blog                  | authorization               |   | Expensive Therapy     | Your article was published. |   | Greg's anti­tax rants | Hey! That's not your blog!  |

Page 38: Web Acceptance Testing with Behat

SCENARIO OUTLINES

http://docs.behat.org/guides/1.gherkin.html#scenario-outlines

Scenario Outline: Wilson posts to a blog  Given I am logged in as Wilson  When I try to post to "<blog>"  Then I should see "<authorization>"

  Examples:   | blog                  | authorization               |   | Expensive Therapy     | Your article was published. |   | Greg's anti­tax rants | Hey! That's not your blog!  |

x 2

Page 39: Web Acceptance Testing with Behat

STEP TABLES

http://docs.behat.org/guides/1.gherkin.html#tables

Scenario: Wilson posts to a blog  Given I am logged in as Wilson  When I try to post to:   | blog                  |   | Expensive Therapy     |   | Greg's anti­tax rants |  Then I should see an authorization outcome

Page 40: Web Acceptance Testing with Behat

STEP TABLES

http://docs.behat.org/guides/1.gherkin.html#tables

Scenario: Wilson posts to a blog  Given I am logged in as Wilson  When I try to post to:   | blog                  |   | Expensive Therapy     |   | Greg's anti­tax rants |  Then I should see an authorization outcome

x 2

Page 41: Web Acceptance Testing with Behat

ACCESSING THE WEB

Pict

ure

by M

isse

rion,

lice

nsed

und

er th

e CC

BY

2.0

Page 42: Web Acceptance Testing with Behat

MINK API

Driver object

$client = new \Selenium\Client($host, $port);$driver = new \Behat\Mink\Driver\SeleniumDriver(    'firefox', 'base_url', $client);

Session object

$session = new \Behat\Mink\Session($driver);$session­>start();$session­>visit('http://my_project.dev/some_page.php')

Exemplary interaction

$page = $session­>getPage();$elementByCss = $page­>find('css', 'h3 > a');$elementByCss­>click();

http://mink.behat.org/

Page 43: Web Acceptance Testing with Behat

MINK API

Driver object

$client = new \Selenium\Client($host, $port);$driver = new \Behat\Mink\Driver\SeleniumDriver(    'firefox', 'base_url', $client);

Session object

$session = new \Behat\Mink\Session($driver);$session­>start();$session­>visit('http://my_project.dev/some_page.php')

Exemplary interaction

$page = $session­>getPage();$elementByCss = $page­>find('css', 'h3 > a');$elementByCss­>click();

http://mink.behat.org/

Page 44: Web Acceptance Testing with Behat

MINK API

Driver object

$client = new \Selenium\Client($host, $port);$driver = new \Behat\Mink\Driver\SeleniumDriver(    'firefox', 'base_url', $client);

Session object

$session = new \Behat\Mink\Session($driver);$session­>start();$session­>visit('http://my_project.dev/some_page.php')

Exemplary interaction

$page = $session­>getPage();$elementByCss = $page­>find('css', 'h3 > a');$elementByCss­>click();

http://mink.behat.org/

Sahi, Goutte, Zombie.js

Page 45: Web Acceptance Testing with Behat

MINK API

Driver object

$client = new \Selenium\Client($host, $port);$driver = new \Behat\Mink\Driver\SeleniumDriver(    'firefox', 'base_url', $client);

Session object

$session = new \Behat\Mink\Session($driver);$session­>start();$session­>visit('http://my_project.dev/some_page.php')

Exemplary interaction

$page = $session­>getPage();$elementByCss = $page­>find('css', 'h3 > a');$elementByCss­>click();

http://mink.behat.org/

Sahi, Goutte, Zombie.js

Page 46: Web Acceptance Testing with Behat

MINK API

Driver object

$client = new \Selenium\Client($host, $port);$driver = new \Behat\Mink\Driver\SeleniumDriver(    'firefox', 'base_url', $client);

Session object

$session = new \Behat\Mink\Session($driver);$session­>start();$session­>visit('http://my_project.dev/some_page.php')

Exemplary interaction

$page = $session­>getPage();$elementByCss = $page­>find('css', 'h3 > a');$elementByCss­>click();

http://mink.behat.org/

Sahi, Goutte, Zombie.js

Page 47: Web Acceptance Testing with Behat

BEHAT REPORT

~/Behat$ behat features/blogpost.feature

Feature: Blog posting  As a blogger  I want to post an article to a blog

  Scenario: Wilson posts to his own blog            # features/blogpost.feature:5    Given I am logged in as "Wilson"                # FeatureContext::iAmLoggedInAs()    When I try to post to "Expensive Therapy"       # FeatureContext::iTryToPostTo()    Then I should see "Your article was published." # FeatureContext::iShouldSee()

  Scenario: Wilson fails to post to somebody else's blog # features/blogpost.feature:10    Given I am logged in as "Wilson"                     # FeatureContext::iAmLoggedInAs()    When I try to post to "Greg's anti­tax rants"        # FeatureContext::iTryToPostTo()      Blog "Greg's anti­tax rants" does not exist    Then I should see "Hey! That's not your blog!"       # FeatureContext::iShouldSee()

2 scenarios (1 passed, 1 failed)6 steps (4 passed, 1 skipped, 1 failed)

Page 48: Web Acceptance Testing with Behat

BEHAT REPORT

~/Behat$ behat features/blogpost.feature

Feature: Blog posting  As a blogger  I want to post an article to a blog

  Scenario: Wilson posts to his own blog            # features/blogpost.feature:5    Given I am logged in as "Wilson"                # FeatureContext::iAmLoggedInAs()    When I try to post to "Expensive Therapy"       # FeatureContext::iTryToPostTo()    Then I should see "Your article was published." # FeatureContext::iShouldSee()

  Scenario: Wilson fails to post to somebody else's blog # features/blogpost.feature:10    Given I am logged in as "Wilson"                     # FeatureContext::iAmLoggedInAs()    When I try to post to "Greg's anti­tax rants"        # FeatureContext::iTryToPostTo()      Blog "Greg's anti­tax rants" does not exist    Then I should see "Hey! That's not your blog!"       # FeatureContext::iShouldSee()

2 scenarios (1 passed, 1 failed)6 steps (4 passed, 1 skipped, 1 failed)

default formatter (“pretty”)

all formatters:http://docs.behat.org/guides/6.cli.html#format-options

­­format pretty

Page 49: Web Acceptance Testing with Behat

CHALLENGES...

Page 50: Web Acceptance Testing with Behat

DEVELOPERS WRITESCENARIOS

Pict

ure

by M

arja

n Kr

ebel

j, lic

ense

d un

der t

he C

C BY

2.0

Page 51: Web Acceptance Testing with Behat

DEVELOPERS WRITESCENARIOS

AN ARTIFACT ASKED FOR?UNDERSTANDABILITY?

Pict

ure

by M

arja

n Kr

ebel

j, lic

ense

d un

der t

he C

C BY

2.0

Page 52: Web Acceptance Testing with Behat

Pict

ure

by C

onor

Law

less

, lic

ense

d un

der t

he C

C BY

2.0

SCENARIO ABSTRACTION LEVEL

Page 53: Web Acceptance Testing with Behat

Pict

ure

by C

onor

Law

less

, lic

ense

d un

der t

he C

C BY

2.0

SCENARIO ABSTRACTION LEVELWhen I fill the form

When I fill my first name in

to field “first name”

When I fill my last name int

o field “last name”

When I fill ...VS.

Page 54: Web Acceptance Testing with Behat

REDUNDANCY

Pict

ure

by R

ob F

aulk

ner,

licen

sed

unde

r the

CC

BY 2

.0

Page 55: Web Acceptance Testing with Behat

REDUNDANCY

LIMITED: GHERKIN'S REUSE CAPABILITIESLIMITED: UNDERSTANDING OF THOSE

Pict

ure

by R

ob F

aulk

ner,

licen

sed

unde

r the

CC

BY 2

.0

Page 56: Web Acceptance Testing with Behat

TIME-CONSUMING REFACTORING

Pict

ure

by E

arls

37a,

lice

nsed

und

er th

e CC

BY

2.0

Page 57: Web Acceptance Testing with Behat

TIME-CONSUMING REFACTORING

EXECUTION IN REAL-TIME

Pict

ure

by E

arls

37a,

lice

nsed

und

er th

e CC

BY

2.0

Page 58: Web Acceptance Testing with Behat

COMPLEX INFRASTRUCTURE

Pict

ure

by D

omen

ico

Nar

done

, lic

ense

d un

der t

he C

C BY

2.0

Page 59: Web Acceptance Testing with Behat

COMPLEX INFRASTRUCTURE

Pict

ure

by D

omen

ico

Nar

done

, lic

ense

d un

der t

he C

C BY

2.0

Behat Mink Driver Browser Emulator Browser

Page 60: Web Acceptance Testing with Behat

FURTHER REFERENCES: http://www.softwarequalityconnection.com/2011/08/how-to-improve-communication-between-qa-and-development/

http://dannorth.net/introducing-bdd/

http://www.infoq.com/news/2011/02/BDD-ATDD

http://www.slideshare.net/headrevision/the-concept-of-behaviordriven-development

http://www.slideshare.net/everzet/bdd-in-symfony2

SOME BEST PRACTICES: http://www.agileengineeringdesign.com/2012/01/7-deadly-sins-of-automated-software-testing/

http://elabs.se/blog/15-you-re-cuking-it-wrong

http://www.engineyard.com/blog/2009/15-expert-tips-for-using-cucumber/

http://eggsonbread.com/2010/09/06/my-cucumber-best-practices-and-tips/