Effective Testing using Behavior-Driven Development

Preview:

DESCRIPTION

A talk on BDDs, Behat and the process

Citation preview

EFFECTIVE TESTING USING BEHAVIOUR-DRIVEN

DEVELOPMENT

Alexander Kress

About me

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

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

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

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

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

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”

Behat – PHP BDD Framework

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

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

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…}”””

Mink

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

• “removes API differences between different browser emulators”

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(); }

Mink – useful methods

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

Extending Behat and Mink

• phpunit• Database injection• Mocks• Code coverage

This will never work!

Do BDDs remind you of anything?

User Stories!

Who thinks in user stories?

Product Owners

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

It does work!

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

Be careful what you test for

Other things to watch our for

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

Who uses BDDs

QUESTIONS?