acceptance & integration testing with behat (pbc11)

91
Acceptance & Integration Testing Using Behat Ben Waine Email: [email protected] Twitter: @bwaine Saturday, 29 October 11

Upload: benwaine

Post on 08-May-2015

3.247 views

Category:

Technology


1 download

DESCRIPTION

Slides of the talk I gave at PBC11

TRANSCRIPT

Page 1: Acceptance & Integration Testing With Behat (PBC11)

Acceptance & Integration

Testing Using Behat

Ben WaineEmail: [email protected]

Twitter: @bwaine

Saturday, 29 October 11

Page 2: Acceptance & Integration Testing With Behat (PBC11)

Pair Programming

Say hello to the person next to you....

Saturday, 29 October 11

Page 3: Acceptance & Integration Testing With Behat (PBC11)

Set Up

USB Stick

Saturday, 29 October 11

Page 4: Acceptance & Integration Testing With Behat (PBC11)

Set Up

For instant set up:

1) Copy code into a new vhost with the web root pointing at the ‘public’ folder.

2) Uncomment line 13 in: tests/acceptance/features/bootstrap/UIContext.php

3) Install Sahi from the misc folder.

Saturday, 29 October 11

Page 5: Acceptance & Integration Testing With Behat (PBC11)

Set Up

If setting up today run the Behat command using behat.phar

eg: php behat.phar --tags demo1

If you have previously set up, run the behat command

eg: behat --tags demo1

Goto: tests/acceptance/

Saturday, 29 October 11

Page 6: Acceptance & Integration Testing With Behat (PBC11)

Me

Sky BetPHP / MySQL Stack

PHPUnit / Selenium / Behat

Software EngineerPHP Developer

Saturday, 29 October 11

Page 7: Acceptance & Integration Testing With Behat (PBC11)

Roadmap

•Intro To Behaviour Driven Development

•Introducing Behat

•Gherkin & Steps

•API / Service Layer Testing

•UI Testing

•Phabric - Dynamic Fixture Creation

Saturday, 29 October 11

Page 8: Acceptance & Integration Testing With Behat (PBC11)

joind.in

http://joind.in/talk/view/4328

Saturday, 29 October 11

Page 9: Acceptance & Integration Testing With Behat (PBC11)

What Marco Said......

Saturday, 29 October 11

Page 10: Acceptance & Integration Testing With Behat (PBC11)

Stories

Saturday, 29 October 11

Page 11: Acceptance & Integration Testing With Behat (PBC11)

What is BDD?

Saturday, 29 October 11

Page 12: Acceptance & Integration Testing With Behat (PBC11)

Introducing Behat....

Saturday, 29 October 11

Page 13: Acceptance & Integration Testing With Behat (PBC11)

Resistance is futile.......Saturday, 29 October 11

Origins in Rubys cucumber

Page 14: Acceptance & Integration Testing With Behat (PBC11)

What Does It Test?

ScriptsAPI’s

Web PagesModels

Saturday, 29 October 11

Page 15: Acceptance & Integration Testing With Behat (PBC11)

Integration Testing !=

Unit Testing

Saturday, 29 October 11

Page 16: Acceptance & Integration Testing With Behat (PBC11)

Anatomy Of A Behat Test

Saturday, 29 October 11

Page 17: Acceptance & Integration Testing With Behat (PBC11)

Saturday, 29 October 11

Describes the behaviour in a storyHuman ReadableSamples Later

Page 18: Acceptance & Integration Testing With Behat (PBC11)

Saturday, 29 October 11

PHP that maps to lines of the gherkin.Behat parses gherkin and runs the associated steps.

Page 19: Acceptance & Integration Testing With Behat (PBC11)

Writing Behat Tests

$ben > cd /path/to/projects/tests$ben > behat --init

Saturday, 29 October 11

Bootstrap Behat. Provides you with a structure

Page 20: Acceptance & Integration Testing With Behat (PBC11)

Writing Behat Tests

Saturday, 29 October 11

Page 21: Acceptance & Integration Testing With Behat (PBC11)

Feature Files

Feature: Home Page When visiting the PHPCon site As a site visitor I need to be able to see what ` conferences are coming up

Saturday, 29 October 11

Name of testDescription of test

Page 22: Acceptance & Integration Testing With Behat (PBC11)

ScenariosScenario: Get all conferences Given there is conference data in the database When I go to the homepage Then I should see three conferences in a table

Saturday, 29 October 11

Scenario. Multiple scenarios in a single feature file

Page 23: Acceptance & Integration Testing With Behat (PBC11)

ScenariosGiven

(Some Context)

When (Some Event)

Then (The Outcome)

Saturday, 29 October 11

Keywords. Always follow the same formula

Page 24: Acceptance & Integration Testing With Behat (PBC11)

Given (Some Context)

Given there is conference data in the database

Saturday, 29 October 11

Sets up some state or context

Page 25: Acceptance & Integration Testing With Behat (PBC11)

When (Some Event)

When I go to the homepage

When I use the findConferences method

When I am on "/index.php"

When I fill in "search-text" with "PHP"

Saturday, 29 October 11

Executes whatever it is your testing. A http call, a method invocation, a page load

Page 26: Acceptance & Integration Testing With Behat (PBC11)

Then (The Outcome)

Then I should see three conferences in a table

Then I should get a array of three conferences

Then I should see "PHPNW"

Saturday, 29 October 11

An assertion.Has it worked? Are we seeing what we expect to see.

Page 27: Acceptance & Integration Testing With Behat (PBC11)

ConferenceService.feature

Feature: ConferenceService Class In order to display conferences on PHPCon site As a developer I need to be able to retrieve conferences

Scenario: Get all conferences Given there is conference data in the database When I use the findConferences method Then I should get a array of three conferences AND it should contain the conference “PHPNW”

Saturday, 29 October 11

Written By our TESTERS - Fed In From BAsHow is this feature executed?Each step is reusable! MENTION PHP METHODS + ANNOTATIONS!!!!

Page 28: Acceptance & Integration Testing With Behat (PBC11)

Class Methods & Annotations

Saturday, 29 October 11

Written By our TESTERS - Fed In From BAsHow is this feature executed?Each step is reusable! MENTION PHP METHODS + ANNOTATIONS!!!!

Page 29: Acceptance & Integration Testing With Behat (PBC11)

/ Everybody Stand Back /

Behat Knows Regular Expressions

Saturday, 29 October 11

PHP Annotations used to map steps to linesBehat supplies regex.

Page 30: Acceptance & Integration Testing With Behat (PBC11)

Demo One

Behat’s - Regex Fu

Saturday, 29 October 11

Page 31: Acceptance & Integration Testing With Behat (PBC11)

Introducing....... The Sample Domain

Saturday, 29 October 11

Page 32: Acceptance & Integration Testing With Behat (PBC11)

My Amazing PHP Conference Website

Saturday, 29 October 11

Page 33: Acceptance & Integration Testing With Behat (PBC11)

Service Layers

Saturday, 29 October 11

Page 34: Acceptance & Integration Testing With Behat (PBC11)

Demo One

Saturday, 29 October 11

Page 35: Acceptance & Integration Testing With Behat (PBC11)

Fill in the Feature Context File public function __construct(array $parameters) { $params = array( 'user' => $parameters['database']['username'], 'password' => $parameters['database']['password'], 'driver' => $parameters['database']['driver'], 'path' => $parameters['database']['dbPath'], );

$con = \Doctrine\DBAL\DriverManager::getConnection($params);

$confMapper = new \PHPCon\Conference\Mapper($con); $confService = new \PHPCon\Conference\Service($confMapper);

$this->service = $confService; }

Saturday, 29 October 11

The Feature context file- Ever feature gets a instance. Set up some resources. - Set up the object to test - similar to PHPUnit’s set up method.

Page 36: Acceptance & Integration Testing With Behat (PBC11)

Fill in the Feature Context File

/** * @Given /^there is conference data in the database$/ */ public function thereIsConferneceDataInTheDatabase() { $fileName = self::$dataDir .

'sample-conf-session-data.sql'; self::executeQueriesInFile($fileName); }

Saturday, 29 October 11

Remember - Given sets the state.Loads an sql fixture to the DB.

Page 37: Acceptance & Integration Testing With Behat (PBC11)

Fill in the Feature Context File

/** * @When /^I use the findConferences method$/ */ public function iUseTheFindConferencesMethod() { $this->result = $this->service->findConferences();

}

Saturday, 29 October 11

Remember - When executes the thing you want to test.

Page 38: Acceptance & Integration Testing With Behat (PBC11)

Fill in the Feature Context File /** *@Then /^I should get an array of (\d+) conferences$/ */ public function iShouldGetAnArrayOfConferences

($numberOfCons) { assertInternalType('array', $this->result); assertEquals($numberOfCons, count($this->result)); }

Saturday, 29 October 11

Remember - This verifies the output Also - identified a number. Passes the number into the method.

Page 39: Acceptance & Integration Testing With Behat (PBC11)

/** * @Then /^it should contain the * conference "([^"]*)"$/ */ public function itShouldContainTheConference

($confName) { $names = array();

foreach($this->result as $conf) { $names[$conf->getName()] = true; }

if(!array_key_exists($confName, $names)) { throw new \Exception("Conference "

. $confName . " not found"); } }

Saturday, 29 October 11

Page 40: Acceptance & Integration Testing With Behat (PBC11)

Exceptions == Test Failures

Saturday, 29 October 11

Page 41: Acceptance & Integration Testing With Behat (PBC11)

Passing Behat Test

Demo Two

Saturday, 29 October 11

Page 42: Acceptance & Integration Testing With Behat (PBC11)

Exercise One

Testing a service layer with Behat.

Saturday, 29 October 11

Page 43: Acceptance & Integration Testing With Behat (PBC11)

Exercise One

Open the PDF in the misc folder. Read through Section One.

Start Coding :)

Saturday, 29 October 11

Page 44: Acceptance & Integration Testing With Behat (PBC11)

Failing Test

Saturday, 29 October 11

If exceptions are encountered .....

Page 45: Acceptance & Integration Testing With Behat (PBC11)

Passing Test!

Saturday, 29 October 11

Page 46: Acceptance & Integration Testing With Behat (PBC11)

What about the UI?

Saturday, 29 October 11

We’ve covered testing a service class. But how do you test the UI?

Page 47: Acceptance & Integration Testing With Behat (PBC11)

Mink

Saturday, 29 October 11

Part of the Behat Project.A abstraction over a number of different browser testing tools.

Page 48: Acceptance & Integration Testing With Behat (PBC11)

Mink

Goutte Sahi

Zombie.js

Saturday, 29 October 11

Goutte - headless browserSahi - like seleniumZombie.js -

Page 49: Acceptance & Integration Testing With Behat (PBC11)

Extend Mink Context

Use Bundled steps to create higher level abstractions.

Includes predefined steps

Saturday, 29 October 11

Page 50: Acceptance & Integration Testing With Behat (PBC11)

Back To: My Amazing PHP Conference

Website!

Saturday, 29 October 11

We need a UI to testintroducing.....

Page 51: Acceptance & Integration Testing With Behat (PBC11)

Example

Scenario: View all conferences on the homepage Given there is conference data in the database When I am on "/index.php" Then I should see "PHPNW" in the ".conferences" element And I should see "PHPUK" in the ".conferences" element And I should see "PBC11" in the ".conferences" element

Using Minks Bundled Steps

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 52: Acceptance & Integration Testing With Behat (PBC11)

Link FeatureContext.php

to UIContext.php

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 53: Acceptance & Integration Testing With Behat (PBC11)

# features/bootstrap/UIContext.php

use Behat\Behat\Context\ClosuredContextInterface, Behat\Behat\Context\BehatContext, Behat\Behat\Exception\PendingException;

use Behat\Gherkin\Node\PyStringNode, Behat\Gherkin\Node\TableNode;

require_once 'mink/autoload.php';

class UIContext extends Behat\Mink\Behat\Context\MinkContext{

}

class FeatureContext

public function __construct(array $parameters) { $this->useContext('subcontext_alias', new UIContext($parameters));

! // REST OF FEATURE CONSTRUCTOR

}

Saturday, 29 October 11

1) Linking feature files2) All steps included ‘out the box’

Page 54: Acceptance & Integration Testing With Behat (PBC11)

Demo Three

A Behat UI Test (Using Mink + Goutte)

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 55: Acceptance & Integration Testing With Behat (PBC11)

Exercise Two

Testing the UI with Mink and the headless browser Goutte.

Saturday, 29 October 11

Page 56: Acceptance & Integration Testing With Behat (PBC11)

Exercise Two

Open the PDF in the sources folder. Read through Section Two.

Start Coding :)

Saturday, 29 October 11

Page 57: Acceptance & Integration Testing With Behat (PBC11)

Scenario: View all conferences on the homepage Given there is conference data in the database When I am on the "home" page Then I should see "PHPNW" in the "conferences table” And I should see "PHPUK" in the "conferences table” And I should see "PBC11" in the "conferences table”

Abstracting your scenario

Saturday, 29 October 11

Page 58: Acceptance & Integration Testing With Behat (PBC11)

class UIContext extends Behat\Mink\Behat\Context\MinkContext{ protected $pageList = array( "home" => '/index.php'); protected $elementList = array( "conferences table" => '.conferences');

Mink Feature Context

Saturday, 29 October 11

A simple abstraction that makes a map of pages > urls and elements > css selectors. Example of step delegation. Now you never change the feature file. Just steps.

Page 59: Acceptance & Integration Testing With Behat (PBC11)

/** * @When /^I am on the "([^"]*)" page$/ */ public function iAmOnThePage($pageName) {

if(!isset($this->pageList[$pageName])) { throw new Exception( 'Page Name: not in page list'); } $page = $this->pageList[$pageName]; return new When("I am on \"$page\""); }

Saturday, 29 October 11

A simple abstraction that makes a map of pages > urls and elements > css selectors. Example of step delegation. Now you never change the feature file. Just steps.

Page 60: Acceptance & Integration Testing With Behat (PBC11)

/** * @Then /^I should see "([^"]*)" in the "([^"]*)"$/ */ public function iShouldSeeInThe($text, $element) { if(!isset($this->elementList[$element])) { throw new Exception( 'Element: ' . $element . ‘not in element list'); } $element = $this->elementList[$element]; return new Then("I should see \"$text\" in the \"$element\" element"); }

Saturday, 29 October 11

A simple abstraction that makes a map of pages > urls and elements > css selectors. Example of step delegation. Now you never change the feature file. Just steps.

Page 61: Acceptance & Integration Testing With Behat (PBC11)

Demo Four

A Behat UI Test (Using Mink + Goutte)UI implementation abstracted away from Gherkin.

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 62: Acceptance & Integration Testing With Behat (PBC11)

Exercise Three

Abstracting the UI implementation away from the Gherkin.

Saturday, 29 October 11

Page 63: Acceptance & Integration Testing With Behat (PBC11)

Exercise Three

Open the PDF in the sources folder. Read through Section Three.

Start Coding :)

Saturday, 29 October 11

Page 64: Acceptance & Integration Testing With Behat (PBC11)

Javascript Testing withSahi

Saturday, 29 October 11

Page 65: Acceptance & Integration Testing With Behat (PBC11)

Back To: My Amazing PHP Conference

Website!

Saturday, 29 October 11

We need a UI to testintroducing.....

Page 66: Acceptance & Integration Testing With Behat (PBC11)

@javascriptScenario: Use autocomplete functionality to complete a input field Given there is conference data in the database When I am on the "home" page When I fill in "search-text" with "PHP" And I wait for the suggestion box to appear Then I should see "PHPNW"

Example

Saturday, 29 October 11

Mention @tagsSO you need to test java script. Headless browser isn’t suitable.

Page 67: Acceptance & Integration Testing With Behat (PBC11)

@javascriptScenario: Use autocomplete functionality to complete a input field Given there is conference data in the database When I am on the "home" page When I fill in "search-text" with "PHP" And I wait for the suggestion box to appear Then I should see "PHPNW"

Great Reuse

Saturday, 29 October 11

These are supplied by Mink. Woohoo!REUSE

Page 68: Acceptance & Integration Testing With Behat (PBC11)

// In the UIContext class /**

* @Given /^I wait for the suggestion box to appear$/ */ public function iWaitForTheSuggestionBoxToAppear() { $this->getSession()->wait(5000, "$('.suggestions-results').children().length > 0" ); }

Saturday, 29 October 11

Get session - gets you minkWait, Then execute jquery!

Page 69: Acceptance & Integration Testing With Behat (PBC11)

Demo Five

UI Testing With Sahi

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 70: Acceptance & Integration Testing With Behat (PBC11)

Saturday, 29 October 11

Page 71: Acceptance & Integration Testing With Behat (PBC11)

Exercise Four

UI Testing With Sahi

Saturday, 29 October 11

Page 72: Acceptance & Integration Testing With Behat (PBC11)

Exercise Four

Open the PDF in the sources folder. Read through Section Four.

Start Coding :)

Saturday, 29 October 11

Page 73: Acceptance & Integration Testing With Behat (PBC11)

Data

Saturday, 29 October 11

How do you supply your data?

Page 74: Acceptance & Integration Testing With Behat (PBC11)

SQL Fixture

Saturday, 29 October 11

Can be difficult to maintain.Hides Data in a fixture NOT in your scenarios.

Page 75: Acceptance & Integration Testing With Behat (PBC11)

Phabric

Saturday, 29 October 11

Page 76: Acceptance & Integration Testing With Behat (PBC11)

Scenario: Given The following events exist | Name | Date | Venue | Desc | | PHPNW | 2011-10-08 09:00 | Ramada Hotel | Awesome conf! | | PHPUK | 2012-02-27 09:00 | London Business Center | Quite good conf. |

Gherkin Tables

Saturday, 29 October 11

Page 77: Acceptance & Integration Testing With Behat (PBC11)

Demo Five

Dynamic fixture creation with Phabric.

Saturday, 29 October 11

This is ok - but ties the feature to your implementation.

Page 78: Acceptance & Integration Testing With Behat (PBC11)

Case Study: Behat at Sky Bet

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 79: Acceptance & Integration Testing With Behat (PBC11)

The Problem

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 80: Acceptance & Integration Testing With Behat (PBC11)

The Business Analyst

Saturday, 29 October 11

BA / Product owner has all the knowledge Write stories

Page 81: Acceptance & Integration Testing With Behat (PBC11)

The Tester

Saturday, 29 October 11

Testers test implementation vs the story

Page 82: Acceptance & Integration Testing With Behat (PBC11)

The Developer

Saturday, 29 October 11

Dev - write the codeAutomate the process of testing if code meets the requirements of a story

Page 83: Acceptance & Integration Testing With Behat (PBC11)

The Problem

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 84: Acceptance & Integration Testing With Behat (PBC11)

BA’s Write Stories

Testers Write Gherkin

Developers Write Steps + Code

BDD - Skybet Workflow

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 85: Acceptance & Integration Testing With Behat (PBC11)

BDD - Skybet Workflow

Less Defects

Fewer times through this cycle

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 86: Acceptance & Integration Testing With Behat (PBC11)

The Place Of Acceptance & Integration Tests

Saturday, 29 October 11

Whats the problem we are solving.Three elements of the team.Delivering the product first time. Correctly.

Page 87: Acceptance & Integration Testing With Behat (PBC11)

Balance.

Saturday, 29 October 11

Jason HugginsThe place of acceptance and integration tests

Page 88: Acceptance & Integration Testing With Behat (PBC11)

A whole world of trouble.

Saturday, 29 October 11

Takes a long time to run, Harder to implement, when they break you have to do this again!not as reassuring!

Page 89: Acceptance & Integration Testing With Behat (PBC11)

joind.in

http://joind.in/talk/view/4328

Saturday, 29 October 11

Page 90: Acceptance & Integration Testing With Behat (PBC11)

Questions?

Saturday, 29 October 11

Page 91: Acceptance & Integration Testing With Behat (PBC11)

Behat Github Page: https://github.com/Behat/Behat

Mink On Github: https://github.com/Behat/Mink

Website: http://behat.org/

Phabric On Github: https://github.com/benwaine/Phabric

Links

Saturday, 29 October 11