BDD using behat

Post on 08-May-2015

1.031 views 0 download

description

It's not obvious what the stake holders really want. How do you know what tests to write? How to you know that you've written the last test? Are you writing the correct code? These are all questions that developers have to answer on a daily basis. BDD can help you and your PO answers these questions faster and with more accuracy. Behat is the BDD framework for PHP that makes it easy to get started with your feature development.

Transcript of BDD using behat

Behaviour Driven Development

using Behat

Isn’t it obvious…???

○ What test should I write next?

○ How do I know when I’ve written the last test?

○ Am I writing the right code?

“”

If you can't explain it simply, you don't understand it well enough

!Albert Einstein

○ Write Human-readable stories that describe the behaviour of your application

○ Focuses the communication between the business and the developers

○ Code is self documenting

○ Code is easier to maintain

Why BDD?

INSTALL

○ Composer○ PHAR○ GIT

BEHAT

INIT

○ mkdir Calculator

○ cd Calculator

○ behat --init

./features /bootstrap FeatureContext.php

Gherkins

The Basics

Feature - Gherkins

# features/BananaCalculator.features

# Describe the business value of this feature

Feature: Banana Calculator

As Bob the Banana merchant,I want a calculator that can add the amount of bananasso that I can know how many bananas I currently have

# features/BananaCalculator.features

# Describe the business rules with scenarios

Scenario: Will add 2 banana amounts

Scenario - Gherkins

# features/BananaCalculator.features

# Describe the steps

Given I have 3 Bananas

When I add 5 Bananas

Then I should have 8 Banana

Steps - Gherkins

Feature: Banana CalculatorAs Bob the Banana merchant,I want a calculator that can add the amount of bananasso that I can know how many bananas I currently haveScenario: Will add 2 banana amounts Given I have 3 Bananas When I add 5 Bananas Then I should have 8 Banana

Example

Scenario Outline

Passing Parameters and Tagging

@BeforeSuite

Managing Sub-Context

○ Context files can get really big really fast○ Certain steps are repeated across multiple Feature files

Mink

test in browser

Mink

In order to test, that our web application behaves correctly, we need a way to simulate this interaction between browser and web application in our tests

Mink is an open source acceptance test framework for web applications

Goutte

○ Headless Browser○ No JS support○ Good for testing REST API

java -jar selenium-server-standalone.jarjava -jar selenium-server-standalone.jar -Dwebdriver.chrome.driver="/usr/local/bin/chromedriver"

Selenium

○ JS Support

○ Can run multiple browser

○ Need to start Selenium RC

○ Not Headless

○ Need to load Drivers for different browsers

○ Doesn’t play nice at times

Sahi

○ JS Support (kinda)○ Can run Multiple browsers○ Need to setup localhost proxy

in browser○ Not headless○ Issues with Ajax Request

PhantomJS

○ JS Support○ Headless browser○ Pure Awesomeness!phantomjs --webdriver=8643

DEMO

BDD Anti-Patterns

just don’t

When i fill in 'input.flooble-widgets[:first]' with 'FooBar'

○ Write BDD in the domain language

○ You should never see any DOM elements

Anti-pattern

When I subscribe to the newsletter

rather than And I set the value of 'input#newsletter-signup-checkbox' to '1'

and When I search for articles containing the words 'rockets'

rather than When I fill in 'input#search-terms' with 'rockets'

And I click 'button#search-button'

And I wait 10 seconds

○ This is not a requirement

○ Roll up the execution into the page element

public function search($keywords) { $searchForm = $this->find('css', 'form#search'); if (!$searchForm) { throw new ElementNotFoundException($this->getSession(), 'form', 'css', 'form#search'); } $searchForm->fillField('q', $keywords); $searchForm->pressButton('Google Search'); return $this->getPage('Search results'); }

Try this

IN CONCLUSION

BDD & TDD = BFF

Thank you