Acceptance & Integration Testing With Behat (PHPNw2011)

5,323 views

Published on

The slides from my talk 'Acceptance and integration Testing Using Behat'. Given at PHPNW11 08/10/11.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,323
On SlideShare
0
From Embeds
0
Number of Embeds
34
Actions
Shares
0
Downloads
52
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Acceptance & Integration Testing With Behat (PHPNw2011)

  1. 1. Acceptance & Integration Testing Using Behat Ben Waine Email: ben@ben-waine.co.uk Twitter: @bwaineSunday, 9 October 11
  2. 2. Roadmap •Intro To Behaviour Driven Development •Problems We Are Trying to solve at Sky With BDD •Behat •API Testing with Behat •UI Testing with Behat •Data Driven Testing •Is Behat solving our problem?Sunday, 9 October 11
  3. 3. Me Software Engineer PHP Developer Sky Bet PHP / MySQL Stack PHPUnit / Selenium / BehatSunday, 9 October 11
  4. 4. Behaviour Driven Development (BDD)Sunday, 9 October 11
  5. 5. Behaviour-driven development (BDD) takes the position that you can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as .... everyone knows what’s going on. - Dan NorthSunday, 9 October 11
  6. 6. Writing tests first in a customer friendly language - Drinkwater et al 2011Sunday, 9 October 11
  7. 7. The ProblemSunday, 9 October 11
  8. 8. The Business AnalystSunday, 9 October 11
  9. 9. The TesterSunday, 9 October 11
  10. 10. The DeveloperSunday, 9 October 11
  11. 11. The ProblemSunday, 9 October 11
  12. 12. BehatSunday, 9 October 11
  13. 13. Resistance is futile.......Sunday, 9 October 11
  14. 14. What Does It Test? Scripts API’s Web Pages ModelsSunday, 9 October 11
  15. 15. Integration Testing != Unit TestingSunday, 9 October 11
  16. 16. Anatomy Of A Behat TestSunday, 9 October 11
  17. 17. Sunday, 9 October 11
  18. 18. Sunday, 9 October 11
  19. 19. My Amazing PHP Conference Website!Sunday, 9 October 11
  20. 20. Writing Behat Tests $ben > cd /path/to/projects/tests $ben > behat --initSunday, 9 October 11
  21. 21. Writing Behat TestsSunday, 9 October 11
  22. 22. 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 upSunday, 9 October 11
  23. 23. 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 tableSunday, 9 October 11
  24. 24. Scenarios Given (Some Context) When (Some Event) Then (The Outcome)Sunday, 9 October 11
  25. 25. Given (Some Context) Given there is conference data in the databaseSunday, 9 October 11
  26. 26. 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"Sunday, 9 October 11
  27. 27. 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"Sunday, 9 October 11
  28. 28. 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”Sunday, 9 October 11
  29. 29. Class Methods & AnnotationsSunday, 9 October 11
  30. 30. / Everybody Stand Back / Behat Knows Regular ExpressionsSunday, 9 October 11
  31. 31. Sunday, 9 October 11
  32. 32. Sunday, 9 October 11
  33. 33. Sunday, 9 October 11
  34. 34. Sunday, 9 October 11
  35. 35. Copy and paste these methods into FeatureContext.phpSunday, 9 October 11
  36. 36. Sunday, 9 October 11
  37. 37. Sunday, 9 October 11
  38. 38. 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; }Sunday, 9 October 11
  39. 39. 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); }Sunday, 9 October 11
  40. 40. Fill in the Feature Context File /** * @When /^I use the findConferences method$/ */ public function iUseTheFindConferencesMethod() { $this->result = $this->service->findConferences(); }Sunday, 9 October 11
  41. 41. 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)); }Sunday, 9 October 11
  42. 42. /** * @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"); } }Sunday, 9 October 11
  43. 43. Ready to run the tests again.....Sunday, 9 October 11
  44. 44. Sunday, 9 October 11
  45. 45. Sunday, 9 October 11
  46. 46. Failing TestSunday, 9 October 11
  47. 47. Add some application code.....Sunday, 9 October 11
  48. 48. Sunday, 9 October 11
  49. 49. Sunday, 9 October 11
  50. 50. Sunday, 9 October 11
  51. 51. Passing Test!Sunday, 9 October 11
  52. 52. What about the UI?Sunday, 9 October 11
  53. 53. MinkSunday, 9 October 11
  54. 54. Mink Goutte Sahi Zombie.jsSunday, 9 October 11
  55. 55. Extend Mink Context Includes predefined steps Use Bundled steps to create higher level abstractions.Sunday, 9 October 11
  56. 56. Back To: My Amazing PHP Conference Website!Sunday, 9 October 11
  57. 57. 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" elementSunday, 9 October 11
  58. 58. 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 { }Sunday, 9 October 11
  59. 59. Sunday, 9 October 11
  60. 60. Sunday, 9 October 11
  61. 61. Sunday, 9 October 11
  62. 62. Sunday, 9 October 11
  63. 63. Powerful Headless UI Testing Out The BoxSunday, 9 October 11
  64. 64. 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" tableSunday, 9 October 11
  65. 65. Mink Feature Context class UIContext extends BehatMinkBehatContext MinkContext { protected $pageList = array( "home" => /index.php); protected $elementList = array( "conferences table" => .conferences);Sunday, 9 October 11
  66. 66. /** * @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""); }Sunday, 9 October 11
  67. 67. /** * @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 then "$element" element"); }Sunday, 9 October 11
  68. 68. Javascript Testing with..... SahiSunday, 9 October 11
  69. 69. Back To: My Amazing PHP Conference Website!Sunday, 9 October 11
  70. 70. 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"Sunday, 9 October 11
  71. 71. 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"Sunday, 9 October 11
  72. 72. // 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" ); }Sunday, 9 October 11
  73. 73. Sunday, 9 October 11
  74. 74. DataSunday, 9 October 11
  75. 75. SQL FixtureSunday, 9 October 11
  76. 76. PhabricSunday, 9 October 11
  77. 77. Phabric Inserting Data From 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. | INSERT INTO event `Name`, `Date`, `Venue`, `Desc` VALUE (‘PHPNW’, ‘2011-10-08 09:00’, ‘Ramada Hotel’, ‘Awesome Conf!’)Sunday, 9 October 11
  78. 78. Phabric Transforming Table Headings to DB col names 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. | INSERT INTO event `name`, `date`, `venue`, `description` VALUE (‘PHPNW’, ‘2011-10-08 09:00’, ‘Ramada Hotel’, ‘Awesome Conf!’)Sunday, 9 October 11
  79. 79. Phabric Transforming Table data using user defined callbacks Scenario: Given The following events exist | Name | Date | Venue | Desc | | PHPNW | 08/10/2011 09:00 | Ramada Hotel | Awesome conf! | | PHPUK | 27/02/2012 09:00 | London Business Center | Quite good conf. | INSERT INTO event `name`, `date`, `venue`, `description` VALUE (‘PHPNW’, ‘2011-10-08 09:00’, ‘Ramada Hotel’, ‘Awesome Conf!’)Sunday, 9 October 11
  80. 80. Phabric Inserting Relational DataScenario: Given the following sessions exist | Session Code | name | time | description | | BDD | BDD with behat | 12:50 | TDD is cool! | | CI | Continous Integration | 13:30 | Integrate this! | And the following attendees exist | name | | Jack The Lad | | Simple Simon | | Peter Pan | And the following votes exist | Attendee | Session Code | Vote | | Jack The Lad | BDD | UP | | Simple Simon | BDD | UP | | Peter Pan | BDD | UP |Sunday, 9 October 11
  81. 81. Back To Our Problem At SkySunday, 9 October 11
  82. 82. Balance.Sunday, 9 October 11
  83. 83. A whole world of trouble.Sunday, 9 October 11
  84. 84. Questions?Sunday, 9 October 11
  85. 85. 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/ Phabric joind.in: http://joind.in/3592Sunday, 9 October 11

×