effective testing using behavior-driven development

26
EFFECTIVE TESTING USING BEHAVIOUR-DRIVEN DEVELOPMENT Alexander Kress

Upload: alexkress

Post on 08-May-2015

895 views

Category:

Technology


1 download

DESCRIPTION

A talk on BDDs, Behat and the process

TRANSCRIPT

Page 1: Effective Testing using Behavior-Driven Development

EFFECTIVE TESTING USING BEHAVIOUR-DRIVEN

DEVELOPMENT

Alexander Kress

Page 2: Effective Testing using Behavior-Driven Development

About me

• Development manager at RedFlagDeals.com• Worked with BDD based testing for 3 years• Blog devmngr.wordpress.com • Twitter @al3xkress

Page 3: Effective Testing using Behavior-Driven Development

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 them

succeed• Refactor code• Repeat

From Wikipedia

Page 4: Effective Testing using Behavior-Driven Development

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 to

understand

Page 5: Effective Testing using Behavior-Driven Development

Behavior Driven Development

BDD is a second-generation, outside–in, pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters.

Dan North

Page 6: Effective Testing using Behavior-Driven Development

Gherkin – the language

Feature: Some terse yet descriptive text of what is desired In order to realize a named business value.

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

Behat documentation

Page 7: Effective Testing using Behavior-Driven Development
Page 8: Effective Testing using Behavior-Driven Development

Gherkin – example

Feature: “Who viewed your profile” page displays a list of recent visitors

Scenario: Profile visit is reflected on Profile stats page Given “Waikei” has recently visited “Alex” profile When I login as “Alex” And go to my “Profile stats” page Then I see that “Waikei” has visited my profile And can see that “Waikei” works in “Publishing”

Page 9: Effective Testing using Behavior-Driven Development

Behat – PHP BDD Framework

• Clone of Cucumber – Ruby BDD framework• Quick composer based install• Great documentation• But what does it do?– Steps!

Page 10: Effective Testing using Behavior-Driven Development
Page 11: Effective Testing using Behavior-Driven Development

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

Page 12: Effective Testing using Behavior-Driven Development

Behat – Syntactic sugarTables

Then I see the following visitors to my profile| Visitor| Industry || Waikei | Publishing || Nancy | IT || Brian | Education |

Pystrings

Given I make a “GET” request to “URL” I receive”””{ "status":200, “num_results":100…}”””

Page 13: Effective Testing using Behavior-Driven Development

Mink

• Composer based install• Supports– Selenium Driver– Web Driver (Selenium 2)– Sahi and others

• “removes API differences between different browser emulators”

Page 14: Effective Testing using Behavior-Driven Development

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 page if (strpos($this->getMainSession()->getPage()->getText(),

$guest)===false) { throw new Exception("Could not find " . $text . " on page"); } }

public function getMainSession() { return $this->getMainContext()->getSession(); }

Page 15: Effective Testing using Behavior-Driven Development

Mink – useful methods

• Named selectors: link, button, content, etc• CSS, XPath selectors• Form field manipulation• Mouse operations• Custom js execution

Page 16: Effective Testing using Behavior-Driven Development
Page 17: Effective Testing using Behavior-Driven Development

Extending Behat and Mink

• phpunit• Database injection• Mocks• Code coverage

Page 18: Effective Testing using Behavior-Driven Development

This will never work!

Do BDDs remind you of anything?

User Stories!

Who thinks in user stories?

Product Owners

Page 19: Effective Testing using Behavior-Driven Development

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 create new

scenarios and steps

BDDs are a major part of the process

Page 20: Effective Testing using Behavior-Driven Development

It does work!

• No v5.3 of the Spec!• Everyone knows what the PO meant• QA are assisted by developers• No spec!

Page 21: Effective Testing using Behavior-Driven Development

Be careful what you test for

Page 22: Effective Testing using Behavior-Driven Development

Other things to watch our for

• Maintainability (XPaths, CSS paths)• Coding to test• Step duplication• Length of execution

Page 23: Effective Testing using Behavior-Driven Development

Who uses BDDs

Page 26: Effective Testing using Behavior-Driven Development

QUESTIONS?