Effective Testing using Behavior-Driven Development


Published on

A talk on BDDs, Behat and the process

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Effective Testing using Behavior-Driven Development

  2. 2. About me• Development manager at RedFlagDeals.com• Worked with BDD based testing for 3 years• Blog devmngr.wordpress.com• Twitter @al3xkress
  3. 3. Test Driven Development• Add a test• Run all tests and see if the new one fails• Write some code• Run the automated tests and see themsucceed• Refactor code• RepeatFrom Wikipedia
  4. 4. From TDD to BDD• TDD is a developer concept• I want QA to be involved• I want PO to be involved• I want any random person on the street tounderstand
  5. 5. Behavior Driven DevelopmentBDD is a second-generation, outside–in,pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. Itdescribes a cycle of interactions with well-defined outputs, resulting in the delivery ofworking, tested software that matters.Dan North
  6. 6. Gherkin – the languageFeature: Some terse yet descriptive text of what is desiredIn order to realize a named business value.Scenario: Some determinable business situationGiven some preconditionAnd some other preconditionWhen some action by the actorAnd some other actionAnd yet another actionThen some testable outcome is achievedAnd something else we can check happens tooBehat documentation
  7. 7. Gherkin – exampleFeature: “Who viewed your profile” page displays a list ofrecent visitorsScenario: Profile visit is reflected on Profile stats pageGiven “Waikei” has recently visited “Alex” profileWhen I login as “Alex”And go to my “Profile stats” pageThen I see that “Waikei” has visited my profileAnd can see that “Waikei” works in “Publishing”
  8. 8. Behat – PHP BDD Framework• Clone of Cucumber – Ruby BDD framework• Quick composer based install• Great documentation• But what does it do?– Steps!
  9. 9. Behat – Writing steps/*** @Given /^"([^"]*)" has recently visited "([^"]*)" profile$/*/public function hasRecentlyVisitedProfile($visitor, $host){#insert a visit from $visitor to $host into the visits table#fail if $visitor or $host do not exist}General Guidelines for writing steps:• Make steps reusable• Keep them short and simple• Refactor all the time
  10. 10. Behat – Syntactic sugarTablesThen I see the following visitors to my profile| Visitor| Industry || Waikei | Publishing || Nancy | IT || Brian | Education |PystringsGiven I make a “GET” request to “URL” I receive”””{"status":200,“num_results":100…}”””
  11. 11. Mink• Composer based install• Supports– Selenium Driver– Web Driver (Selenium 2)– Sahi and others• “removes API differences between differentbrowser emulators”
  12. 12. Mink - example/*** @Then /^I see that "([^"]*)" has visited my profile$/*/public function iSeeThatHasVisitedMyProfile($guest){#Assuming that the browser is current on the “Profile stats”#the simplest method, just search the pageif (strpos($this->getMainSession()->getPage()->getText(),$guest)===false) {throw new Exception("Could not find " . $text . " on page");}}public function getMainSession() {return $this->getMainContext()->getSession();}
  13. 13. Mink – useful methods• Named selectors: link, button, content, etc• CSS, XPath selectors• Form field manipulation• Mouse operations• Custom js execution
  14. 14. Extending Behat and Mink• phpunit• Database injection• Mocks• Code coverage
  15. 15. This will never work!Do BDDs remind you of anything?User Stories!Who thinks in user stories?Product Owners
  16. 16. It does work!• Convince everyone that BDDs are awesome• PO write requirements in BDD scenarios• QA review and validate early• Developers write BDD steps• QA augment developer BDD steps and createnew scenarios and stepsBDDs are a major part of the process
  17. 17. It does work!• No v5.3 of the Spec!• Everyone knows what the PO meant• QA are assisted by developers• No spec!
  18. 18. Be careful what you test for
  19. 19. Other things to watch our for• Maintainability (XPaths, CSS paths)• Coding to test• Step duplication• Length of execution
  20. 20. Who uses BDDs
  21. 21. Other resourceshttp://www.slideshare.net/NgTzeYang/the-lazy-developers-guide-to-bdd-with-cucumberTomorrow! - Webinar - Behavior DrivenDevelopment with Behathttp://qafoo.com/blog/042_webinar_bdd_behat.html
  22. 22. Linkshttp://behat.org/http://mink.behat.org/http://en.wikipedia.org/wiki/Behavior-driven_development
  23. 23. QUESTIONS?