Acceptance &                           Integration                          Testing Using                              Beh...
Pair Programming                          Say hello to the person next to you....Saturday, 29 October 11
Set Up                          USB StickSaturday, 29 October 11
Set Up                                  For instant set up:            1) Copy code into a new vhost with the web root    ...
Set Up                                  Goto: tests/acceptance/            If setting up today run the Behat command using...
Me          Software Engineer          PHP Developer         Sky Bet         PHP / MySQL Stack         PHPUnit / Selenium ...
Roadmap        •Intro To Behaviour Driven Development        •Introducing Behat        •Gherkin & Steps        •API / Serv...
joind.in                          http://joind.in/talk/view/4328Saturday, 29 October 11
What Marco Said......Saturday, 29 October 11
StoriesSaturday, 29 October 11
What is BDD?Saturday, 29 October 11
Introducing Behat....Saturday, 29 October 11
Resistance is futile.......Saturday, 29 October 11Origins in Rubys cucumber
What Does It Test?                                Scripts                                 API’s                           ...
Integration Testing                                   !=                             Unit TestingSaturday, 29 October 11
Anatomy Of A Behat TestSaturday, 29 October 11
Saturday, 29 October 11Describes the behaviour in a storyHuman ReadableSamples Later
Saturday, 29 October 11PHP that maps to lines of the gherkin.Behat parses gherkin and runs the associatedsteps.
Writing Behat Tests                          $ben > cd /path/to/projects/tests                          $ben > behat --ini...
Writing Behat TestsSaturday, 29 October 11
Feature Files         Feature: Home Page                  When visiting the PHPCon site                  As a site visitor...
ScenariosScenario: Get all conferences    Given there is conference data in the database    When I go to the homepage    T...
Scenarios                              Given                          (Some Context)                              When    ...
Given                          (Some Context)            Given there is conference data in the databaseSaturday, 29 Octobe...
When                                  (Some Event)                              When I go to the homepage                 ...
Then                            (The Outcome)            Then I should see three conferences in a table            Then I ...
ConferenceService.feature      Feature: ConferenceService Class               In order to display conferences on          ...
Class Methods & AnnotationsSaturday, 29 October 11Written By our TESTERS - Fed In From BAsHow is this feature executed?Eac...
/ Everybody Stand Back /                Behat Knows Regular ExpressionsSaturday, 29 October 11PHP Annotations used to map ...
Demo One                          Behat’s - Regex FuSaturday, 29 October 11
Introducing.......                          The Sample DomainSaturday, 29 October 11
My Amazing PHP Conference                     WebsiteSaturday, 29 October 11
Service LayersSaturday, 29 October 11
Demo OneSaturday, 29 October 11
Fill in the Feature Context File          public function __construct(array $parameters)          {              $params =...
Fill in the Feature Context File        /**          * @Given /^there is conference data in the database$/          */    ...
Fill in the Feature Context File           /**             * @When /^I use the findConferences method$/             */    ...
Fill in the Feature Context File           /**             *@Then /^I should get an array of (d+) conferences$/           ...
/**                   * @Then /^it should contain the                   * conference "([^"]*)"$/                   */     ...
Exceptions == Test FailuresSaturday, 29 October 11
Demo Two                          Passing Behat TestSaturday, 29 October 11
Exercise One                          Testing a service layer with Behat.Saturday, 29 October 11
Exercise One                          Open the PDF in the misc folder.                            Read through Section One...
Failing TestSaturday, 29 October 11If exceptions are encountered .....
Passing Test!Saturday, 29 October 11
What about the UI?Saturday, 29 October 11We’ve covered testing a service class. But how do you test the UI?
MinkSaturday, 29 October 11Part of the Behat Project.A abstraction over a number of different browser testing tools.
Mink                            Goutte           Sahi                                 Zombie.jsSaturday, 29 October 11Gout...
Extend Mink Context                          Includes predefined steps                   Use Bundled steps to create       ...
Back To:                 My Amazing PHP Conference                         Website!Saturday, 29 October 11We need a UI to ...
Example                          Using Minks Bundled Steps  Scenario: View all conferences on the homepage      Given ther...
Link                                FeatureContext.php                                        to                          ...
class FeatureContext                      public function __construct(array $parameters)                      {           ...
Demo Three                          A Behat UI Test (Using Mink + Goutte)Saturday, 29 October 11This is ok - but ties the ...
Exercise Two                          Testing the UI with Mink and                          the headless browser Goutte.Sa...
Exercise Two                          Open the PDF in the sources folder.                             Read through Section...
Abstracting your scenario Scenario: View all conferences on the homepage     Given there is conference data in the databas...
Mink Feature Context class UIContext extends BehatMinkBehatContext MinkContext {     protected $pageList = array(         ...
/**              * @When /^I am on the "([^"]*)" page$/              */             public function iAmOnThePage($pageName...
/**              * @Then /^I should see "([^"]*)" in the "([^"]*)"$/              */             public function iShouldSe...
Demo Four              A Behat UI Test (Using Mink + Goutte)         UI implementation abstracted away from Gherkin.Saturd...
Exercise Three          Abstracting the UI implementation away from the                              Gherkin.Saturday, 29 ...
Exercise Three                          Open the PDF in the sources folder.                            Read through Sectio...
Javascript Testing with                                   SahiSaturday, 29 October 11
Back To:                 My Amazing PHP Conference                         Website!Saturday, 29 October 11We need a UI to ...
Example          @javascript          Scenario: Use autocomplete functionality to          complete a input field         ...
Great Reuse             @javascript             Scenario: Use autocomplete functionality to             complete a input f...
// In the UIContext class                          /**                             * @Given /^I wait for the suggestion bo...
Demo Five                              UI Testing With SahiSaturday, 29 October 11This is ok - but ties the feature to you...
Saturday, 29 October 11
Exercise Four                          UI Testing With SahiSaturday, 29 October 11
Exercise Four                          Open the PDF in the sources folder.                             Read through Sectio...
DataSaturday, 29 October 11How do you supply your data?
SQL FixtureSaturday, 29 October 11Can be difficult to maintain.Hides Data in a fixture NOT in your scenarios.
PhabricSaturday, 29 October 11
Gherkin Tables    Scenario:        Given The         following events   exist        | Name |          Date               ...
Demo Five                          Dynamic fixture creation with Phabric.Saturday, 29 October 11This is ok - but ties the f...
Case Study:                          Behat at Sky BetSaturday, 29 October 11Whats the problem we are solving.Three element...
The ProblemSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first ...
The Business          AnalystSaturday, 29 October 11BA / Product owner has all the knowledgeWrite stories
The TesterSaturday, 29 October 11Testers test implementation vs the story
The                                  DeveloperSaturday, 29 October 11Dev - write the codeAutomate the process of testing i...
The ProblemSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first ...
BDD - Skybet Workflow                                BA’s Write Stories                              Testers Write Gherkin ...
BDD - Skybet Workflow                                  Less Defects                          Fewer times through this cycle...
The Place Of Acceptance &                        Integration TestsSaturday, 29 October 11Whats the problem we are solving....
Balance.Saturday, 29 October 11Jason HugginsThe place of acceptance and integration tests
A whole world of trouble.Saturday, 29 October 11Takes a long time to run,Harder to implement, when they break you have to ...
joind.in                          http://joind.in/talk/view/4328Saturday, 29 October 11
Questions?Saturday, 29 October 11
Links     Behat Github Page: https://github.com/Behat/Behat        Mink On Github: https://github.com/Behat/Mink          ...
Upcoming SlideShare
Loading in...5
×

Acceptance & Integration Testing With Behat (PBC11)

3,416

Published on

Slides of the talk I gave at PBC11

Published in: Technology
1 Comment
8 Likes
Statistics
Notes
No Downloads
Views
Total Views
3,416
On Slideshare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
71
Comments
1
Likes
8
Embeds 0
No embeds

No notes for slide

Acceptance & Integration Testing With Behat (PBC11)

  1. 1. Acceptance & Integration Testing Using Behat Ben Waine Email: ben@ben-waine.co.uk Twitter: @bwaineSaturday, 29 October 11
  2. 2. Pair Programming Say hello to the person next to you....Saturday, 29 October 11
  3. 3. Set Up USB StickSaturday, 29 October 11
  4. 4. Set Up For instant set up: 1) Copy code into a new vhost with the web root pointing at the ‘public’ folder. 2) Uncomment line 13 in: tests/acceptance/features/bootstrap/UIContext.php 3) Install Sahi from the misc folder.Saturday, 29 October 11
  5. 5. Set Up Goto: tests/acceptance/ If setting up today run the Behat command using behat.phar eg: php behat.phar --tags demo1 If you have previously set up, run the behat command eg: behat --tags demo1Saturday, 29 October 11
  6. 6. Me Software Engineer PHP Developer Sky Bet PHP / MySQL Stack PHPUnit / Selenium / BehatSaturday, 29 October 11
  7. 7. Roadmap •Intro To Behaviour Driven Development •Introducing Behat •Gherkin & Steps •API / Service Layer Testing •UI Testing •Phabric - Dynamic Fixture CreationSaturday, 29 October 11
  8. 8. joind.in http://joind.in/talk/view/4328Saturday, 29 October 11
  9. 9. What Marco Said......Saturday, 29 October 11
  10. 10. StoriesSaturday, 29 October 11
  11. 11. What is BDD?Saturday, 29 October 11
  12. 12. Introducing Behat....Saturday, 29 October 11
  13. 13. Resistance is futile.......Saturday, 29 October 11Origins in Rubys cucumber
  14. 14. What Does It Test? Scripts API’s Web Pages ModelsSaturday, 29 October 11
  15. 15. Integration Testing != Unit TestingSaturday, 29 October 11
  16. 16. Anatomy Of A Behat TestSaturday, 29 October 11
  17. 17. Saturday, 29 October 11Describes the behaviour in a storyHuman ReadableSamples Later
  18. 18. Saturday, 29 October 11PHP that maps to lines of the gherkin.Behat parses gherkin and runs the associatedsteps.
  19. 19. Writing Behat Tests $ben > cd /path/to/projects/tests $ben > behat --initSaturday, 29 October 11Bootstrap Behat. Provides you with a structure
  20. 20. Writing Behat TestsSaturday, 29 October 11
  21. 21. Feature Files Feature: Home Page When visiting the PHPCon site As a site visitor I need to be able to see what ` conferences are coming upSaturday, 29 October 11Name of testDescription of test
  22. 22. ScenariosScenario: Get all conferences Given there is conference data in the database When I go to the homepage Then I should see three conferences in a tableSaturday, 29 October 11Scenario.Multiple scenarios in a single feature file
  23. 23. Scenarios Given (Some Context) When (Some Event) Then (The Outcome)Saturday, 29 October 11Keywords.Always follow the same formula
  24. 24. Given (Some Context) Given there is conference data in the databaseSaturday, 29 October 11Sets up some state or context
  25. 25. When (Some Event) When I go to the homepage When I use the findConferences method When I am on "/index.php" When I fill in "search-text" with "PHP"Saturday, 29 October 11Executes whatever it is your testing.A http call, a method invocation, a page load
  26. 26. Then (The Outcome) Then I should see three conferences in a table Then I should get a array of three conferences Then I should see "PHPNW"Saturday, 29 October 11An assertion.Has it worked?Are we seeing what we expect to see.
  27. 27. ConferenceService.feature Feature: ConferenceService Class In order to display conferences on PHPCon site As a developer I need to be able to retrieve conferences Scenario: Get all conferences Given there is conference data in the database When I use the findConferences method Then I should get a array of three conferences AND it should contain the conference “PHPNW”Saturday, 29 October 11Written By our TESTERS - Fed In From BAsHow is this feature executed?Each step is reusable!MENTION PHP METHODS + ANNOTATIONS!!!!
  28. 28. Class Methods & AnnotationsSaturday, 29 October 11Written By our TESTERS - Fed In From BAsHow is this feature executed?Each step is reusable!MENTION PHP METHODS + ANNOTATIONS!!!!
  29. 29. / Everybody Stand Back / Behat Knows Regular ExpressionsSaturday, 29 October 11PHP Annotations used to map steps to linesBehat supplies regex.
  30. 30. Demo One Behat’s - Regex FuSaturday, 29 October 11
  31. 31. Introducing....... The Sample DomainSaturday, 29 October 11
  32. 32. My Amazing PHP Conference WebsiteSaturday, 29 October 11
  33. 33. Service LayersSaturday, 29 October 11
  34. 34. Demo OneSaturday, 29 October 11
  35. 35. Fill in the Feature Context File public function __construct(array $parameters) { $params = array( user => $parameters[database][username], password => $parameters[database][password], driver => $parameters[database][driver], path => $parameters[database][dbPath], ); $con = DoctrineDBALDriverManager::getConnection($params); $confMapper = new PHPConConferenceMapper($con); $confService = new PHPConConferenceService($confMapper); $this->service = $confService; }Saturday, 29 October 11The Feature context file- Ever feature gets a instance. Set up someresources.- Set up the object to test - similar to PHPUnit’s setup method.
  36. 36. Fill in the Feature Context File /** * @Given /^there is conference data in the database$/ */ public function thereIsConferneceDataInTheDatabase() { $fileName = self::$dataDir . sample-conf-session-data.sql; self::executeQueriesInFile($fileName); }Saturday, 29 October 11Remember - Given sets the state.Loads an sql fixture to the DB.
  37. 37. Fill in the Feature Context File /** * @When /^I use the findConferences method$/ */ public function iUseTheFindConferencesMethod() { $this->result = $this->service->findConferences(); }Saturday, 29 October 11Remember - When executes the thing you want totest.
  38. 38. Fill in the Feature Context File /** *@Then /^I should get an array of (d+) conferences$/ */ public function iShouldGetAnArrayOfConferences ($numberOfCons) { assertInternalType(array, $this->result); assertEquals($numberOfCons, count($this->result)); }Saturday, 29 October 11Remember - This verifies the outputAlso - identified a number. Passes the number intothe method.
  39. 39. /** * @Then /^it should contain the * conference "([^"]*)"$/ */ public function itShouldContainTheConference ($confName) { $names = array(); foreach($this->result as $conf) { $names[$conf->getName()] = true; } if(!array_key_exists($confName, $names)) { throw new Exception("Conference " . $confName . " not found"); } }Saturday, 29 October 11
  40. 40. Exceptions == Test FailuresSaturday, 29 October 11
  41. 41. Demo Two Passing Behat TestSaturday, 29 October 11
  42. 42. Exercise One Testing a service layer with Behat.Saturday, 29 October 11
  43. 43. Exercise One Open the PDF in the misc folder. Read through Section One. Start Coding :)Saturday, 29 October 11
  44. 44. Failing TestSaturday, 29 October 11If exceptions are encountered .....
  45. 45. Passing Test!Saturday, 29 October 11
  46. 46. What about the UI?Saturday, 29 October 11We’ve covered testing a service class. But how do you test the UI?
  47. 47. MinkSaturday, 29 October 11Part of the Behat Project.A abstraction over a number of different browser testing tools.
  48. 48. Mink Goutte Sahi Zombie.jsSaturday, 29 October 11Goutte - headless browserSahi - like seleniumZombie.js -
  49. 49. Extend Mink Context Includes predefined steps Use Bundled steps to create higher level abstractions.Saturday, 29 October 11
  50. 50. Back To: My Amazing PHP Conference Website!Saturday, 29 October 11We need a UI to testintroducing.....
  51. 51. Example Using Minks Bundled Steps Scenario: View all conferences on the homepage Given there is conference data in the database When I am on "/index.php" Then I should see "PHPNW" in the ".conferences" element And I should see "PHPUK" in the ".conferences" element And I should see "PBC11" in the ".conferences" elementSaturday, 29 October 11This is ok - but ties the feature to your implementation.
  52. 52. Link FeatureContext.php to UIContext.phpSaturday, 29 October 11This is ok - but ties the feature to your implementation.
  53. 53. class FeatureContext public function __construct(array $parameters) { $this->useContext(subcontext_alias, new UIContext($parameters)); ! // REST OF FEATURE CONSTRUCTOR } # features/bootstrap/UIContext.php use BehatBehatContextClosuredContextInterface, BehatBehatContextBehatContext, BehatBehatExceptionPendingException; use BehatGherkinNodePyStringNode, BehatGherkinNodeTableNode; require_once mink/autoload.php; class UIContext extends BehatMinkBehatContextMinkContext { }Saturday, 29 October 111) Linking feature files2) All steps included ‘out the box’
  54. 54. Demo Three A Behat UI Test (Using Mink + Goutte)Saturday, 29 October 11This is ok - but ties the feature to your implementation.
  55. 55. Exercise Two Testing the UI with Mink and the headless browser Goutte.Saturday, 29 October 11
  56. 56. Exercise Two Open the PDF in the sources folder. Read through Section Two. Start Coding :)Saturday, 29 October 11
  57. 57. Abstracting your scenario Scenario: View all conferences on the homepage Given there is conference data in the database When I am on the "home" page Then I should see "PHPNW" in the "conferences table” And I should see "PHPUK" in the "conferences table” And I should see "PBC11" in the "conferences table”Saturday, 29 October 11
  58. 58. Mink Feature Context class UIContext extends BehatMinkBehatContext MinkContext { protected $pageList = array( "home" => /index.php); protected $elementList = array( "conferences table" => .conferences);Saturday, 29 October 11A simple abstraction that makes a map of pages >urls and elements > css selectors.Example of step delegation.Now you never change the feature file. Just steps.
  59. 59. /** * @When /^I am on the "([^"]*)" page$/ */ public function iAmOnThePage($pageName) { if(!isset($this->pageList[$pageName])) { throw new Exception( Page Name: not in page list); } $page = $this->pageList[$pageName]; return new When("I am on "$page""); }Saturday, 29 October 11A simple abstraction that makes a map of pages >urls and elements > css selectors.Example of step delegation.Now you never change the feature file. Just steps.
  60. 60. /** * @Then /^I should see "([^"]*)" in the "([^"]*)"$/ */ public function iShouldSeeInThe($text, $element) { if(!isset($this->elementList[$element])) { throw new Exception( Element: . $element . ‘not in element list); } $element = $this->elementList[$element]; return new Then("I should see "$text" in the "$element" element"); }Saturday, 29 October 11A simple abstraction that makes a map of pages >urls and elements > css selectors.Example of step delegation.Now you never change the feature file. Just steps.
  61. 61. Demo Four A Behat UI Test (Using Mink + Goutte) UI implementation abstracted away from Gherkin.Saturday, 29 October 11This is ok - but ties the feature to your implementation.
  62. 62. Exercise Three Abstracting the UI implementation away from the Gherkin.Saturday, 29 October 11
  63. 63. Exercise Three Open the PDF in the sources folder. Read through Section Three. Start Coding :)Saturday, 29 October 11
  64. 64. Javascript Testing with SahiSaturday, 29 October 11
  65. 65. Back To: My Amazing PHP Conference Website!Saturday, 29 October 11We need a UI to testintroducing.....
  66. 66. Example @javascript Scenario: Use autocomplete functionality to complete a input field Given there is conference data in the database When I am on the "home" page When I fill in "search-text" with "PHP" And I wait for the suggestion box to appear Then I should see "PHPNW"Saturday, 29 October 11Mention @tagsSO you need to test java script.Headless browser isn’t suitable.
  67. 67. Great Reuse @javascript Scenario: Use autocomplete functionality to complete a input field Given there is conference data in the database When I am on the "home" page When I fill in "search-text" with "PHP" And I wait for the suggestion box to appear Then I should see "PHPNW"Saturday, 29 October 11These are supplied by Mink. Woohoo!REUSE
  68. 68. // In the UIContext class /** * @Given /^I wait for the suggestion box to appear$/ */ public function iWaitForTheSuggestionBoxToAppear() { $this->getSession()->wait(5000, "$(.suggestions-results).children().length > 0" ); }Saturday, 29 October 11Get session - gets you minkWait,Then execute jquery!
  69. 69. Demo Five UI Testing With SahiSaturday, 29 October 11This is ok - but ties the feature to your implementation.
  70. 70. Saturday, 29 October 11
  71. 71. Exercise Four UI Testing With SahiSaturday, 29 October 11
  72. 72. Exercise Four Open the PDF in the sources folder. Read through Section Four. Start Coding :)Saturday, 29 October 11
  73. 73. DataSaturday, 29 October 11How do you supply your data?
  74. 74. SQL FixtureSaturday, 29 October 11Can be difficult to maintain.Hides Data in a fixture NOT in your scenarios.
  75. 75. PhabricSaturday, 29 October 11
  76. 76. Gherkin Tables Scenario: Given The following events exist | Name | Date | Venue | Desc | | PHPNW | 2011-10-08 09:00 | Ramada Hotel | Awesome conf! | | PHPUK | 2012-02-27 09:00 | London Business Center | Quite good conf. |Saturday, 29 October 11
  77. 77. Demo Five Dynamic fixture creation with Phabric.Saturday, 29 October 11This is ok - but ties the feature to your implementation.
  78. 78. Case Study: Behat at Sky BetSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  79. 79. The ProblemSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  80. 80. The Business AnalystSaturday, 29 October 11BA / Product owner has all the knowledgeWrite stories
  81. 81. The TesterSaturday, 29 October 11Testers test implementation vs the story
  82. 82. The DeveloperSaturday, 29 October 11Dev - write the codeAutomate the process of testing if code meets therequirements of a story
  83. 83. The ProblemSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  84. 84. BDD - Skybet Workflow BA’s Write Stories Testers Write Gherkin Developers Write Steps + CodeSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  85. 85. BDD - Skybet Workflow Less Defects Fewer times through this cycleSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  86. 86. The Place Of Acceptance & Integration TestsSaturday, 29 October 11Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.
  87. 87. Balance.Saturday, 29 October 11Jason HugginsThe place of acceptance and integration tests
  88. 88. A whole world of trouble.Saturday, 29 October 11Takes a long time to run,Harder to implement, when they break you have to do this again!not as reassuring!
  89. 89. joind.in http://joind.in/talk/view/4328Saturday, 29 October 11
  90. 90. Questions?Saturday, 29 October 11
  91. 91. Links Behat Github Page: https://github.com/Behat/Behat Mink On Github: https://github.com/Behat/Mink Website: http://behat.org/ Phabric On Github: https://github.com/benwaine/ PhabricSaturday, 29 October 11
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×