BDD using behat

36
Behaviour Driven Development using Behat

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

Page 1: BDD using behat

Behaviour Driven Development

using Behat

Page 2: BDD 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?

Page 3: BDD using behat
Page 4: BDD using behat

“”

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

!Albert Einstein

Page 5: BDD using behat

○ 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?

Page 6: BDD using behat

INSTALL

○ Composer○ PHAR○ GIT

BEHAT

INIT

○ mkdir Calculator

○ cd Calculator

○ behat --init

./features /bootstrap FeatureContext.php

Page 7: BDD using behat

Gherkins

The Basics

Page 8: BDD using behat

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

Page 9: BDD using behat

# features/BananaCalculator.features

# Describe the business rules with scenarios

Scenario: Will add 2 banana amounts

Scenario - Gherkins

Page 10: BDD using behat

# features/BananaCalculator.features

# Describe the steps

Given I have 3 Bananas

When I add 5 Bananas

Then I should have 8 Banana

Steps - Gherkins

Page 11: BDD using behat

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

Page 12: BDD using behat
Page 13: BDD using behat
Page 14: BDD using behat
Page 15: BDD using behat

Scenario Outline

Page 16: BDD using behat

Passing Parameters and Tagging

Page 17: BDD using behat

@BeforeSuite

Page 18: BDD using behat

Managing Sub-Context

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

Page 19: BDD using behat

Mink

test in browser

Page 20: BDD using behat

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

Page 21: BDD using behat
Page 22: BDD using behat

Goutte

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

Page 23: BDD using behat

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

Page 24: BDD using behat

Sahi

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

in browser○ Not headless○ Issues with Ajax Request

Page 25: BDD using behat

PhantomJS

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

Page 26: BDD using behat
Page 27: BDD using behat

DEMO

Page 28: BDD using behat

BDD Anti-Patterns

just don’t

Page 29: BDD using behat

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

○ Write BDD in the domain language

○ You should never see any DOM elements

Page 30: BDD using behat

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'

Page 31: BDD using behat

And I wait 10 seconds

○ This is not a requirement

○ Roll up the execution into the page element

Page 32: BDD using behat

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

Page 33: BDD using behat

IN CONCLUSION

Page 34: BDD using behat
Page 35: BDD using behat

BDD & TDD = BFF

Page 36: BDD using behat

Thank you