Behavior Driven
Development in
Perl
Tudor Constantin - perl hacker @ Evozon
Contents

● Objectives
●   TDD and its disadvantages
●   How BDD works
●   BDD and The Mojolicious-Boilerplate
●   Wishlist
●   Conclusions
●   References
Objectives

Assuming you have the following user story to
implement:
Feature: Standard Signup
    In order to begin using the application
    As a new user
    I want to create an account
Scenario: Signup with valid email/password combination
  Given I do not have an account
  When I signup with email and password
  Then I should be logged in
  And my profile details should be filled in
Objectives

Following TDD, you'd have to start writing tests
that will guarantee your app functionality
Objectives

Objective achieved

With BDD, that user story IS your test suite for
that scenario

Perl BDD Module:
● Peter Sergeant's Test::BDD::Cucumber
Objectives
Feature: Standard Signup
    In order to begin using the application
    As a new user
    I want to create an account
Scenario: Signup with valid email/password combination
  Given I do not have an account
  When I signup with email and password
  Then I should be logged in
  And my profile details should be filled in
Legend:
(this text is parsed by the BDD framework)
(your actual test knows what to do with this text)
TDD and its disadvantages

●   Where to start
●   What to test and what not to test
●   How much to test in one go
●   What name should that test have
●   How to organize the tests in files
●   How to setup and share context between
    tests
How BDD Works

1. Stakeholders write a formal user story
   a. Feature section - free form text describing that
      particular feature of the application
   b. Background section - parsable text meant to set up
      the context for testing that feature - ex: "Given I am
      logged in as administrator"
       i. each Feature might have a Background section
   c. Scenario(s) section(s)
       i. each feature has one or more scenarios
   d. Steps
       i. each scenario has one or more steps
How BDD Works

2. The developer implements each step
Example of Implementation for the step
'Givena mojo test object for the
"Boilerplate" application'
How BDD Works
Given qr/a mojo test object for the "(.+)"
application/, func ($c) {
  use_ok( $1 );
  my $tm = Test::Mojo->new( $1 );
  ok( $tm, "Object created" );
  $c->stash->{'feature'}->{'tm'} = $tm;
  ok( $c->stash->{'feature'}->{'tm'}, "Got
our Test::Mojo object" );
 };
BDD and The Mojolicious-Boilerplate

● Started using BDD as the way to test apps
  using the Mojolicious-Boilerplate
● There are passing scenarios
● There are failing scenarios
● Get involved and take on the opportunity to
  learn BDD in practice
BDD and The Mojolicious-Boilerplate

Scenario file: Mojolicious-Boilerplate / t / features /   mojo.feature
BDD and The Mojolicious-Boilerplate

Steps File: Mojolicious-Boilerplate / t / features / step_definitions /   mojo_steps.pl
Wishlist Road Map

● Create more scenarios/user-stories
● Find a way to get a more convenient map
  between Mojolicious Routes, URL display
  text and URL value
● Create a Bootstrap-specific grammar
  ○ will make the testing process even less "programmer
    bound"
  ○ most HTML elements are contained in tags with
    Bootstrap specific css classes
● More, better documentation
Conclusions

● easy way to get started in writing tests
● test-scenarios and tests are self documented
● natural way of logically split the test files into
  functionality related files/folders/objects
References
●   http://en.wikipedia.org/wiki/Behavior_Driven_Development
●   Peter Sergeant's Test::BDD::Cucumber::Manual::Tutorial
●   Mojolicious Boilerplate
●   Story Driven Development With Cucumber

Perl Behavior Driven Development (BDD)

  • 1.
    Behavior Driven Development in Perl TudorConstantin - perl hacker @ Evozon
  • 2.
    Contents ● Objectives ● TDD and its disadvantages ● How BDD works ● BDD and The Mojolicious-Boilerplate ● Wishlist ● Conclusions ● References
  • 3.
    Objectives Assuming you havethe following user story to implement: Feature: Standard Signup In order to begin using the application As a new user I want to create an account Scenario: Signup with valid email/password combination Given I do not have an account When I signup with email and password Then I should be logged in And my profile details should be filled in
  • 4.
    Objectives Following TDD, you'dhave to start writing tests that will guarantee your app functionality
  • 5.
    Objectives Objective achieved With BDD,that user story IS your test suite for that scenario Perl BDD Module: ● Peter Sergeant's Test::BDD::Cucumber
  • 6.
    Objectives Feature: Standard Signup In order to begin using the application As a new user I want to create an account Scenario: Signup with valid email/password combination Given I do not have an account When I signup with email and password Then I should be logged in And my profile details should be filled in Legend: (this text is parsed by the BDD framework) (your actual test knows what to do with this text)
  • 7.
    TDD and itsdisadvantages ● Where to start ● What to test and what not to test ● How much to test in one go ● What name should that test have ● How to organize the tests in files ● How to setup and share context between tests
  • 8.
    How BDD Works 1.Stakeholders write a formal user story a. Feature section - free form text describing that particular feature of the application b. Background section - parsable text meant to set up the context for testing that feature - ex: "Given I am logged in as administrator" i. each Feature might have a Background section c. Scenario(s) section(s) i. each feature has one or more scenarios d. Steps i. each scenario has one or more steps
  • 9.
    How BDD Works 2.The developer implements each step Example of Implementation for the step 'Givena mojo test object for the "Boilerplate" application'
  • 10.
    How BDD Works Givenqr/a mojo test object for the "(.+)" application/, func ($c) { use_ok( $1 ); my $tm = Test::Mojo->new( $1 ); ok( $tm, "Object created" ); $c->stash->{'feature'}->{'tm'} = $tm; ok( $c->stash->{'feature'}->{'tm'}, "Got our Test::Mojo object" ); };
  • 11.
    BDD and TheMojolicious-Boilerplate ● Started using BDD as the way to test apps using the Mojolicious-Boilerplate ● There are passing scenarios ● There are failing scenarios ● Get involved and take on the opportunity to learn BDD in practice
  • 12.
    BDD and TheMojolicious-Boilerplate Scenario file: Mojolicious-Boilerplate / t / features / mojo.feature
  • 13.
    BDD and TheMojolicious-Boilerplate Steps File: Mojolicious-Boilerplate / t / features / step_definitions / mojo_steps.pl
  • 14.
    Wishlist Road Map ●Create more scenarios/user-stories ● Find a way to get a more convenient map between Mojolicious Routes, URL display text and URL value ● Create a Bootstrap-specific grammar ○ will make the testing process even less "programmer bound" ○ most HTML elements are contained in tags with Bootstrap specific css classes ● More, better documentation
  • 15.
    Conclusions ● easy wayto get started in writing tests ● test-scenarios and tests are self documented ● natural way of logically split the test files into functionality related files/folders/objects
  • 16.
    References ● http://en.wikipedia.org/wiki/Behavior_Driven_Development ● Peter Sergeant's Test::BDD::Cucumber::Manual::Tutorial ● Mojolicious Boilerplate ● Story Driven Development With Cucumber