t3con13: web application development using behaviour driven development

45
Speed up web application development using Behaviour Driven Development 9th European TYPO3 Conference, October 30th 2013 Martin Helmich, Mittwald CM Service [email protected] Photo: „Rawhead“, CC BY-NC-SA http://www.flickr.com/photos/rawhead/3780249375/

Upload: mhelmich

Post on 08-May-2015

2.106 views

Category:

Technology


0 download

DESCRIPTION

Slides of my presentation at the 9th European TYPO3 Conference conference (http://t3con13de.typo3.org) in Stuttgart on October 30th, 2013.

TRANSCRIPT

Page 1: T3CON13: Web application development using Behaviour Driven Development

Speed����������� ������������������  up����������� ������������������  web����������� ������������������  application����������� ������������������  development����������� ������������������  using����������� ������������������  Behaviour����������� ������������������  Driven����������� ������������������  Development9th����������� ������������������  European����������� ������������������  TYPO3����������� ������������������  Conference,����������� ������������������  ����������� ������������������  October����������� ������������������  30th����������� ������������������  2013

Martin����������� ������������������  Helmich,����������� ������������������  Mittwald����������� ������������������  CM����������� ������������������  [email protected]

Photo: „Rawhead“, CC BY-NC-SAhttp://www.flickr.com/photos/rawhead/3780249375/

Page 2: T3CON13: Web application development using Behaviour Driven Development

YOUR����������� ������������������  SPEAKER

Martin����������� ������������������  HelmichSoftware����������� ������������������  Architect����������� ������������������  at����������� ������������������  Mittwald

TYPO3����������� ������������������  addicted����������� ������������������  since����������� ������������������  2004Caffeine����������� ������������������  addicted����������� ������������������  since����������� ������������������  2007

Page 3: T3CON13: Web application development using Behaviour Driven Development

Show����������� ������������������  of����������� ������������������  hands:

Who����������� ������������������  has����������� ������������������  (not)����������� ������������������  heard����������� ������������������  of����������� ������������������  BDD?

Thesupermat,����������� ������������������  CC����������� ������������������  BY-SAhttp://commons.wikimedia.org/wiki/File:F%C3%AAte_de_l%27Humanit%C3%A9_2012_-_009.jpg

Page 4: T3CON13: Web application development using Behaviour Driven Development

Franck����������� ������������������  Nieto,����������� ������������������  CC����������� ������������������  NC-BY-SAhttp://www.flickr.com/photos/francki/9686485433/

A����������� ������������������  (really����������� ������������������  short)����������� ������������������  introduction����������� ������������������  to

Scrum

Page 5: T3CON13: Web application development using Behaviour Driven Development

Requirements����������� ������������������  are����������� ������������������  phrased����������� ������������������  as

User����������� ������������������  stories

As a <role>,I want <something>so that <benefit>

„Cohn,����������� ������������������  Mike:����������� ������������������  User����������� ������������������  Stories����������� ������������������  Applied����������� ������������������  -����������� ������������������  For����������� ������������������  Agile����������� ������������������  Software����������� ������������������  DevelopmentAddison-Wesley,����������� ������������������  2004

Page 6: T3CON13: Web application development using Behaviour Driven Development

Requirements����������� ������������������  are����������� ������������������  phrased����������� ������������������  as

User����������� ������������������  stories

As an anonymous user,I want to register on the siteso that I can use the site's

personalized services.

Page 7: T3CON13: Web application development using Behaviour Driven Development

Each����������� ������������������  user����������� ������������������  story����������� ������������������  has

Acceptance����������� ������������������  criteria

In the menu, there is a „register“ item.On the register page the user can enter the following information:

user namereal nameemail address

After registration, the user should receive a confirmation email.

Page 8: T3CON13: Web application development using Behaviour Driven Development

Product����������� ������������������  Backlog

Sprint����������� ������������������  Backlog

Sprint(1~4����������� ������������������  weeks)

Daily

Page 9: T3CON13: Web application development using Behaviour Driven Development

ProductOwner

DevelopmentTeamProduct

Backlog

writesstories

SprintBacklog

Selectedby����������� ������������������  team

Productincrement

createdby����������� ������������������  team

Verifies����������� ������������������  thatacceptance����������� ������������������  criteria����������� ������������������  are����������� ������������������  met

Page 10: T3CON13: Web application development using Behaviour Driven Development

Another(also����������� ������������������  really����������� ������������������  short)introduction����������� ������������������  to

TDD

„MacroShoots“,����������� ������������������  CC����������� ������������������  NC-BY-SAhttp://www.flickr.com/photos/41398593@N05/4365961229/

Page 11: T3CON13: Web application development using Behaviour Driven Development

Failingtest

PassingtestRefactor

Test-Driven����������� ������������������  Development

Page 12: T3CON13: Web application development using Behaviour Driven Development

Unit����������� ������������������  tests����������� ������������������  are

great!

Page 13: T3CON13: Web application development using Behaviour Driven Development

Unit����������� ������������������  tests����������� ������������������  are

great!

But

not����������� ������������������  sufficient.

Page 14: T3CON13: Web application development using Behaviour Driven Development

Acceptance����������� ������������������  Test����������� ������������������  using

PHPUnit����������� ������������������  &����������� ������������������  Selenium

<?phpclass MyTest extends PHPUnit_Extensions_Selenium2TestCase{    protected function setUp()    {        $this->setBrowser('*firefox');        $this->setBrowserUrl('http://login.mittwald.de/');    }     public function testLoginWithCorrectCredentialsWorksCorrectly()    {        $this->url('login.html');        $this->byXPath('//input[@id="login_username"]')->value('123456'); $this->byXPath('//input[@id="login_password"]')->value('supersecret'); $this->byXPath('//input[@type="submit" and @name="login_password"]')->clickAndWait();

$this->frame(0); $this->assertContains( 'Welcome to the customer center', $this->byXPath('//body')->text() );    }}?>

Page 15: T3CON13: Web application development using Behaviour Driven Development

Acceptance����������� ������������������  Test����������� ������������������  using

PHPUnit����������� ������������������  &����������� ������������������  Selenium

<?phpclass MyTest extends PHPUnit_Extensions_Selenium2TestCase{    protected function setUp()    {        $this->setBrowser('*firefox');        $this->setBrowserUrl('http://login.mittwald.de/');    }     public function testLoginWithCorrectCredentialsWorksCorrectly()    {        $this->url('login.html');        $this->byXPath('//input[@id="login_username"]')->value('123456'); $this->byXPath('//input[@id="login_password"]')->value('supersecret'); $this->byXPath('//input[@type="submit" and @name="login_password"]')->clickAndWait();

$this->frame(0); $this->assertContains( 'Welcome to the customer center', $this->byXPath('//body')->text() );    }}?>

Hey,����������� ������������������  XPath����������� ������������������  expressions����������� ������������������  arereally����������� ������������������  cool! DEV

Page 16: T3CON13: Web application development using Behaviour Driven Development

Acceptance����������� ������������������  Test����������� ������������������  using

PHPUnit����������� ������������������  &����������� ������������������  Selenium

<?phpclass MyTest extends PHPUnit_Extensions_Selenium2TestCase{    protected function setUp()    {        $this->setBrowser('*firefox');        $this->setBrowserUrl('http://login.mittwald.de/');    }     public function testLoginWithCorrectCredentialsWorksCorrectly()    {        $this->url('login.html');        $this->byXPath('//input[@id="login_username"]')->value('123456'); $this->byXPath('//input[@id="login_password"]')->value('supersecret'); $this->byXPath('//input[@type="submit" and @name="login_password"]')->clickAndWait();

$this->frame(0); $this->assertContains( 'Welcome to the customer center', $this->byXPath('//body')->text() );    }}?>

SRSLY?����������� ������������������  WTF?

PO

Page 17: T3CON13: Web application development using Behaviour Driven Development

Introducing

Gherkin

CC����������� ������������������  BY-SA����������� ������������������  http://commons.wikimedia.org/wiki/File:Spreewaldgurke2.jpg

Page 18: T3CON13: Web application development using Behaviour Driven Development

Gherkin����������� ������������������  is����������� ������������������  a����������� ������������������  business-readable����������� ������������������  (business-writable?)����������� ������������������  domain-specific����������� ������������������  language����������� ������������������  for����������� ������������������  writing����������� ������������������  behaviour����������� ������������������  specifications.

CC����������� ������������������  BY-SA����������� ������������������  http://commons.wikimedia.org/wiki/File:Spreewaldgurke2.jpg

Introducing

Gherkin

Page 19: T3CON13: Web application development using Behaviour Driven Development

Acceptance����������� ������������������  Test����������� ������������������  using

Gherkin����������� ������������������  and����������� ������������������  Behat&Mink

Scenario: User login Given I am on "login.mittwald.de" When I fill in "123456" for "username" And fill in "supersecret" for "password" And press "log in" Then I should see "Welcome to the customer center"

Page 20: T3CON13: Web application development using Behaviour Driven Development

Acceptance����������� ������������������  Test����������� ������������������  using

Gherkin����������� ������������������  and����������� ������������������  Behat&Mink

Scenario: User login Given I am on "login.mittwald.de" When I fill in "123456" for "username" And fill in "supersecret" for "password" And press "log in" Then I should see "Welcome to the customer center"

I����������� ������������������  wrote����������� ������������������  this����������� ������������������  all����������� ������������������  by����������� ������������������  myself!

PO

Page 21: T3CON13: Web application development using Behaviour Driven Development

<?phpclass MyTest extends PHPUnit_Extensions_Selenium2TestCase{    protected function setUp()    {        $this->setBrowser('*firefox');        $this->setBrowserUrl('http://login.mittwald.de/');    }     public function testLoginWithCorrectCredentialsWorksCorrectly()    {        $this->url('login.html');        $this->byXPath('//input[@id="login_username"]')->value('123456'); $this->byXPath('//input[@id="login_password"]')->value('supersecret'); $this ->byXPath('//input[@type="submit" and @name="login_password"]') ->clickAndWait();

$this->frame(0); $this->assertContains( 'Welcome to the customer center', $this->byXPath('//body')->text() );    }}?>

Scenario: User login Given I am on "login.mittwald.de" When I fill in "123456" for "username" And fill in "supersecret" for "password" And press "log in" Then I should see "Welcome to the customer center"

24 6Lines����������� ������������������  of����������� ������������������  code

Page 22: T3CON13: Web application development using Behaviour Driven Development

Toolsthat����������� ������������������  caninterpretGherkin

Florian����������� ������������������  Richter,����������� ������������������  CC����������� ������������������  BYhttp://www.flickr.com/photos/florianric/7263382550/

Page 23: T3CON13: Web application development using Behaviour Driven Development

Cucumber Behat

Decym92,����������� ������������������  Public����������� ������������������  Domain,����������� ������������������  http://commons.wikimedia.org/wiki/File:Rubin013.JPGDrew����������� ������������������  McLellan,����������� ������������������  CC����������� ������������������  BY-NC,����������� ������������������  http://www.flickr.com/photos/drewm/3191872515/

Page 24: T3CON13: Web application development using Behaviour Driven Development

Behat Mink

GoutteDriver

SeleniumDriver

WebDriver

SahiDriver

ZombieDriver

Selenium

Sahi

Zombie

Page 25: T3CON13: Web application development using Behaviour Driven Development

Install����������� ������������������  using����������� ������������������  Composer

{ "require": { "behat/behat": "*", "behat/mink": "*", "behat/mink-extension": "*",

"behat/mink-goutte-driver": "*", "behat/mink-selenium-driver": "*", "behat/mink-selenium2-driver": "*", "behat/mink-sahi-driver": "*", "behat/mink-zombie-driver": "*" }}

Page 26: T3CON13: Web application development using Behaviour Driven Development

You����������� ������������������  can����������� ������������������  use����������� ������������������  Behat����������� ������������������  for����������� ������������������  simple

UI����������� ������������������  tests.

Or����������� ������������������  use����������� ������������������  Behat����������� ������������������  for����������� ������������������  actual

Acceptance����������� ������������������  Tests.

Page 27: T3CON13: Web application development using Behaviour Driven Development

Scenario: Registration form Given I am on "index.php?id=512" When I follow "register now!" And I fill in "user_name" with "Max Mustermann" And I fill in "user_email" with "[email protected]" And I attach the file "/Users/mmustermann/max.jpeg" to "user_image" And I press "submit registration" Then I should be on "index.php?id=514" And the response should contain "Thank you ..." And the mailbox "m15273" should contain an email with subject "Your registration"

Page 28: T3CON13: Web application development using Behaviour Driven Development

Scenario: Registration form Given I am on "index.php?id=512" When I follow "register now!" And I fill in "user_name" with "Max Mustermann" And I fill in "user_email" with "[email protected]" And I attach the file "/Users/mmustermann/max.jpeg" to "user_image" And I press "submit registration" Then I should be on "index.php?id=514" And the response should contain "Thank you ..." And the mailbox "m15273" should contain an email with subject "Your registration"

This����������� ������������������  is����������� ������������������  NOT����������� ������������������  anacceptance����������� ������������������  test!

Page 29: T3CON13: Web application development using Behaviour Driven Development

Better!

Feature: Online registration In order to use the site's personalized features As an anonymous user I want to register to the page.

Scenario: Registration is confirmed Given I am on the registration page And I fill in the following: | Full name | Max Mustermann | | Email address | [email protected] | And I upload an image of myself And submit the registration Then I should see a confirmation message And I should receive a confirmation email

Page 30: T3CON13: Web application development using Behaviour Driven Development

Feature: Online registration In order to use the site's personalized features As an anonymous user I want to register to the page.

Scenario: Registration is confirmed Given I am on the registration page And I fill in the following: | Full name | Max Mustermann | | Email address | [email protected] | And I upload an image of myself And submit the registration Then I should see a confirmation message And I should receive a confirmation email

Who����������� ������������������  writes����������� ������������������  thestep����������� ������������������  definitions?

Page 31: T3CON13: Web application development using Behaviour Driven Development

Then /^(?:I| ) should receive a confirmation e?mail$/ do # Connect to mailbox and see if mail is actually there. # Throw exception otherwise.end

class FeatureContext extends Context{ /** * @Then /^(?:|I )should receive a confirmation e?mail$/ */ public function assertRegistrationMailWasReceived() { // Connect to mailbox and see if mail is actually there. // Throw exception otherwise. }}

Page 32: T3CON13: Web application development using Behaviour Driven Development
Page 33: T3CON13: Web application development using Behaviour Driven Development

ProductOwner

Stories����������� ������������������  andacceptance����������� ������������������  tests

DevelopmentTeam

writes

iteratively����������� ������������������  refineimplement����������� ������������������  step����������� ������������������  definitionsuse����������� ������������������  as����������� ������������������  completion����������� ������������������  measure

Page 34: T3CON13: Web application development using Behaviour Driven Development

Integrate����������� ������������������  acceptance����������� ������������������  tests����������� ������������������  into����������� ������������������  your����������� ������������������  continuous����������� ������������������  integration����������� ������������������  process!

(Try����������� ������������������  to����������� ������������������  run����������� ������������������  as����������� ������������������  many����������� ������������������  as����������� ������������������  possible����������� ������������������  on����������� ������������������  each����������� ������������������  build)

Page 35: T3CON13: Web application development using Behaviour Driven Development

Possible����������� ������������������  CI����������� ������������������  build����������� ������������������  pipelinesInitial����������� ������������������  situation:����������� ������������������  No����������� ������������������  acceptance����������� ������������������  tests

Main

Deploy����������� ������������������  to����������� ������������������  Production

Run����������� ������������������  unit����������� ������������������  testsPerform����������� ������������������  CGL����������� ������������������  checksCompile����������� ������������������  stuff

Deploy����������� ������������������  to����������� ������������������  production����������� ������������������  system(e.g.����������� ������������������  using����������� ������������������  Surf,����������� ������������������  Capistrano,����������� ������������������  DPKG/Aptitude)

Easy,����������� ������������������  isn't����������� ������������������  it?

Page 36: T3CON13: Web application development using Behaviour Driven Development

Possible����������� ������������������  CI����������� ������������������  build����������� ������������������  pipelinesVariant����������� ������������������  #1:����������� ������������������  Slow����������� ������������������  but����������� ������������������  safe

Main

Deploy����������� ������������������  to����������� ������������������  Staging

Run����������� ������������������  all����������� ������������������  AT's

Run����������� ������������������  unit����������� ������������������  testsPerform����������� ������������������  CGL����������� ������������������  checksCompile����������� ������������������  stuff

Deploy����������� ������������������  to����������� ������������������  staging����������� ������������������  system����������� ������������������  (e.g.����������� ������������������  using����������� ������������������  Surf,����������� ������������������  Capistrano,����������� ������������������  DPKG/Aptitude)Reset����������� ������������������  staging����������� ������������������  database����������� ������������������  to����������� ������������������  predefined����������� ������������������  state

Run����������� ������������������  all����������� ������������������  acceptance����������� ������������������  tests����������� ������������������  in����������� ������������������  staging����������� ������������������  system

Deploy����������� ������������������  to����������� ������������������  production����������� ������������������  systemDeploy����������� ������������������  to����������� ������������������  Production

Page 37: T3CON13: Web application development using Behaviour Driven Development

Possible����������� ������������������  CI����������� ������������������  build����������� ������������������  pipelinesVariant����������� ������������������  #2:����������� ������������������  Fast����������� ������������������  but����������� ������������������  unsafe

Main

Deploy����������� ������������������  to����������� ������������������  Staging

Run����������� ������������������  all����������� ������������������  AT's

Deploy����������� ������������������  to����������� ������������������  staging����������� ������������������  systemReset����������� ������������������  staging����������� ������������������  database����������� ������������������  to����������� ������������������  predefined����������� ������������������  state

Run����������� ������������������  all����������� ������������������  acceptance����������� ������������������  tests����������� ������������������  in����������� ������������������  staging����������� ������������������  system.

Roll����������� ������������������  back����������� ������������������  production����������� ������������������  system����������� ������������������  if����������� ������������������  errors����������� ������������������  occur.

Deploy����������� ������������������  to����������� ������������������  Production

Rollback����������� ������������������  Production

Page 38: T3CON13: Web application development using Behaviour Driven Development

Possible����������� ������������������  CI����������� ������������������  build����������� ������������������  pipelinesVariant����������� ������������������  #3:����������� ������������������  Somewhere����������� ������������������  in����������� ������������������  between

Main

Deploy����������� ������������������  to����������� ������������������  Staging

Run����������� ������������������  critical����������� ������������������  AT's

Run����������� ������������������  only����������� ������������������  critical����������� ������������������  acceptance����������� ������������������  tests����������� ������������������  in����������� ������������������  staging����������� ������������������  system

Run����������� ������������������  all����������� ������������������  AT's

Rollback����������� ������������������  Production

Run����������� ������������������  remaining����������� ������������������  acceptance����������� ������������������  tests����������� ������������������  in����������� ������������������  staging����������� ������������������  system

Rollback����������� ������������������  production����������� ������������������  system����������� ������������������  if����������� ������������������  errors����������� ������������������  occured

Deploy����������� ������������������  to����������� ������������������  Production

Page 39: T3CON13: Web application development using Behaviour Driven Development

How����������� ������������������  to

sellBDD

or„I‘m����������� ������������������  already����������� ������������������  paying����������� ������������������  you����������� ������������������  for����������� ������������������  unit����������� ������������������  tests,����������� ������������������  why����������� ������������������  the����������� ������������������  ****����������� ������������������  should����������� ������������������  I����������� ������������������  now����������� ������������������  pay����������� ������������������  for����������� ������������������  accepance����������� ������������������  tests,����������� ������������������  too!?“

Dirk����������� ������������������  Houbrechts,����������� ������������������  CC����������� ������������������  BY-NChttp://www.flickr.com/photos/houbi/9583306027/

Page 40: T3CON13: Web application development using Behaviour Driven Development

BDD����������� ������������������  allows����������� ������������������  to...

reduce����������� ������������������  the����������� ������������������  amount����������� ������������������  of����������� ������������������  time����������� ������������������  spent����������� ������������������  on����������� ������������������  software����������� ������������������  testing.

Nick����������� ������������������  Olejniczak,����������� ������������������  CC����������� ������������������  BY-NChttp://www.flickr.com/photos/nicholasjon/4498088305/

Page 41: T3CON13: Web application development using Behaviour Driven Development

BDD����������� ������������������  allows����������� ������������������  to...

measure����������� ������������������  progress����������� ������������������  easily����������� ������������������  and����������� ������������������  objectively

Sven����������� ������������������  Van����������� ������������������  Echelpoel,����������� ������������������  CC����������� ������������������  BY-NC-SAhttp://www.flickr.com/photos/s7nn3/5468396537/

Page 42: T3CON13: Web application development using Behaviour Driven Development

Bogdan����������� ������������������  Sudito,����������� ������������������  CC����������� ������������������  BYhttp://www.flickr.com/photos/bogdansuditu/2377844553/

BDD����������� ������������������  allows...

the����������� ������������������  customer����������� ������������������  to����������� ������������������  phrase����������� ������������������  more����������� ������������������  precise����������� ������������������  specifications.

the����������� ������������������  developer����������� ������������������  to����������� ������������������  implement����������� ������������������  specifications����������� ������������������  more����������� ������������������  precisely.

Page 43: T3CON13: Web application development using Behaviour Driven Development

BDD����������� ������������������  allows����������� ������������������  to...

create����������� ������������������  software����������� ������������������  that����������� ������������������  works(and����������� ������������������  keeps����������� ������������������  working)

Arthur����������� ������������������  John����������� ������������������  Picton,����������� ������������������  CC����������� ������������������  BYhttp://www.flickr.com/photos/arthurjohnpicton/5364226117/

Page 44: T3CON13: Web application development using Behaviour Driven Development

Duncan����������� ������������������  Hull,����������� ������������������  CC����������� ������������������  BYhttp://www.flickr.com/photos/dullhunk/202872717/

Page 45: T3CON13: Web application development using Behaviour Driven Development

References

Gärtner,����������� ������������������  Markus:����������� ������������������  ATDD����������� ������������������  in����������� ������������������  der����������� ������������������  Praxis,����������� ������������������  dpunkt.Verlag����������� ������������������  2013Linz,����������� ������������������  Tilo:����������� ������������������  Testen����������� ������������������  in����������� ������������������  Scrum-Projekten,����������� ������������������  dpunkt.Verlag����������� ������������������  2013

North,����������� ������������������  Dan:����������� ������������������  Introducing����������� ������������������  BDD,����������� ������������������  http://dannorth.net/introducing-bdd/,����������� ������������������  2006

Cohn,����������� ������������������  Mike:����������� ������������������  User����������� ������������������  Stories����������� ������������������  Applied,����������� ������������������  Addison-Wesley����������� ������������������  2004

Literature