DHAWAL JOSHI
AJIT KUMAR NARAYANAN
MAKING BDD FUN WITH CUCUMBER, JASMINE AND …
BDD ?
BDD
 TDD
• Write Tests first
• Watch it fail
• Write enough code to make the test pass
• Continue
 But, soon
• Where do I start ?, What should I test
• What should I call the tests
• Why is that test failing and what user stories does it impact ?
 BDD
 = TDD + Stakeholder Value + Collaboration + Behaviour
 Usually written on top of DSLs that resemble natural language ( Only to address Collaboration)
Who needs to know BDD?
Product Owner / Stakeholder Developer Tester
Principles
 Think Behaviour & everyone speaks the same language
 And about stakeholder value
 Enough is enough
Traditional Way
Product Owner /
Stakeholder
Developer
Tester
User Stories
Clarifications
Specifications, Design
Test Case Document
There are many
Bugs !
But, see it is in
The test case
document
But, it was never
Specified …
That’s correct. It
Is the right spec !
Well, you never
Told me
I told the tester.
Aren’t you guys in one
Team ???
BDD Way
Product Owner /
Stakeholder
DeveloperTester
User Stories
Clarifications
SpecificationsTest Case Document
We better start
collaborating better
and talk same language
We better start
collaborating better
and talk same language
We better start
collaborating better
and talk same language
Ubiquitous Language
1: Think Behaviour & everyone speaks the same language
Value
2: Stakeholder value
Product Owner /
Stakeholder
In Order to [Business Value]
As a [Role]
I want to [Feature]
1. Protect Revenue
2. Increase Revenue
3. Manage Cost
How much is Enough ?
3: Enough is Enough
1. No Over engineering
2. No Overdoing { planning, Analysis and Design }
3. There is no such thing as a perfect software
Ubiquitous Language
A Language structured around the domain model that everyone in the team
understands.
A Language capable to express a feature, its business value and behaviour (
Behaviour = Context Action Outcome )
ENTER GHERKIN
Gherkin ?
1. Business Readable DSL that expresses the ‘what’ of the software without specifying the
‘how’ i.e it describes Behaviour
2. Serves 2 Purposes : Documentation + Automated testing
3. Line oriented language like python or YAML; Uses indentation to define structure
Gherkin ?
Feature: Search courses
In order to ensure better utilization of courses
Potential students should be able to search for courses
Scenario: Search by topic
Given there are 240 courses which do not have the topic "biology"
And there are 2 courses A001, B205 that each have "biology" as one of the topics
When I search for "biology"
Then I should see the following courses:
| Course code |
| A001 |
| B205 |
Context
Action
Outcome
Behavior
ENTER CUCUMBER
Cucumber ?
1. Is a Tool that executes plain-text functional descriptions as automated tests
2. Understands the ‘Gherkin’ Language
3. Itself is written in Ruby, but can be used for several languages
4. Has several language specific variations : E.g : Cucumber.js , Cucumber JVM etc …
Cucumber ?
Feature: Search courses
In order to ensure better utilization of courses
Potential students should be able to search for courses
Scenario: Search by topic
Given there are 240 courses which do not have the topic "biology"
And there are 2 courses A001, B205 that each have "biology" as one of the topics
When I search for "biology"
Then I should see the following courses:
| Course code |
| A001 |
| B205 |
Feature
Step Definitions ( Regex)
Scenario ( 5- 25)
Cucumber ?
Product Owner /
Stakeholder
DeveloperTester
Features
Step Definitions
Cucumber : Organizing your features
Project Root
features
support
step_defenitions
Thegreatest.feature
Theultimate.feature
Thegreatest_steps.rb
Theultimate_steps.rb
env.rb
Cucumber : Organizing your features
step definition
Given there are 200 elements in the stack Given /^there are (d+) elements in the queue’ do
#DO SOMETHING HERE
@stack= Stack.new
200.times do |i|
@stack.push(‘item’ + i.to_s)
end
end
Cucumber : Organizing your features
step definition
Given there are 200 elements in the stack
When I add another element
When /^I add another element’ do
@stack.push ‘itemanother’
end
Cucumber : Organizing your features
step definition
Given there are 200 elements in the stack
When I add another element
Then there should be 201 elements
Then /^There should be (d+) elements’ do
@stack.count.should == 201
end
MAP THIS TO AGILE TEST MATRIX
Cucumber & Agile test matrix
Cucumber / Variants
BUT, NOT ALL Q2 TESTS ARE API BASED
TESTS
ENTER CAPYBARA, SELENIUM, WEBRAT,
ZOMBIE.JS AND 100 OTHER WEB APP
TEST FRAMEWORKS
CAPYBARA
.is a library that can be used with cucumber to write tests that also need javascript support.
SELENIUM
… automates web browsers across many platform
WEBRAT
… allows to quickly write robust and thorough acceptance tests for a web application. By
leveraging the DOM, it can run tests similarly to an in-browser testing solution without the
associated performance hit (and browser dependency)
JASMINE
Jasmine
1. Yet another BDD framework for testing your java script code
2. Evolved from Rspec
3. Suited for Q1 Tests
4. Can run in browser or server-side environments like node.js
describe (“<UNIT NAME >”, function() {
describe(“<BEHAVIOUR>”, function () {
it(“<SHOULD DO WHAT >, function (){
});
});
});
Jasmine
describe (“Customer Model”, function() {
var cust = new Customer();
describe(“When saved without an ID”, function () {
cust.id = null;
it(“should throw an ID cannot be null exception”, function (){
expect(custid.save()).toThrow(“IDCannotbeNull”);
});
});
});
Your behavior reads : “Customer Model when saved without an ID should throw an ID cannot
be null exception”
Spec File
Cucumber : Organizing your Specs
Project Root
spec
SomeUnitspec.js
models
Customermodelspec.js
SpecRunner.html
Jasmine & Agile test matrix
Cucumber / Variants
TYPICAL FLOW
Typical Flow
Write the Features
(DEV, PO, QE)
Refine Features
(DEV, PO, QE)
Add Step Definitions
(DEV, QE)
Write the code
/ UI that you wished
you had
(QE/Dev)
RunTest
Do you want to add/modify a unit ?
Describe Behavior
(Dev , Verified by Arch)RunTestWrite enough to
Make test pass
Q2
Q1 (Dev Territory)
Pass
Backlog Grooming
Fail
SIMPLE EXAMPLE - LIVE

Behavior Driven Development

  • 1.
    DHAWAL JOSHI AJIT KUMARNARAYANAN MAKING BDD FUN WITH CUCUMBER, JASMINE AND …
  • 2.
  • 3.
    BDD  TDD • WriteTests first • Watch it fail • Write enough code to make the test pass • Continue  But, soon • Where do I start ?, What should I test • What should I call the tests • Why is that test failing and what user stories does it impact ?  BDD  = TDD + Stakeholder Value + Collaboration + Behaviour  Usually written on top of DSLs that resemble natural language ( Only to address Collaboration)
  • 4.
    Who needs toknow BDD? Product Owner / Stakeholder Developer Tester
  • 5.
    Principles  Think Behaviour& everyone speaks the same language  And about stakeholder value  Enough is enough
  • 6.
    Traditional Way Product Owner/ Stakeholder Developer Tester User Stories Clarifications Specifications, Design Test Case Document There are many Bugs ! But, see it is in The test case document But, it was never Specified … That’s correct. It Is the right spec ! Well, you never Told me I told the tester. Aren’t you guys in one Team ???
  • 7.
    BDD Way Product Owner/ Stakeholder DeveloperTester User Stories Clarifications SpecificationsTest Case Document We better start collaborating better and talk same language We better start collaborating better and talk same language We better start collaborating better and talk same language Ubiquitous Language 1: Think Behaviour & everyone speaks the same language
  • 8.
    Value 2: Stakeholder value ProductOwner / Stakeholder In Order to [Business Value] As a [Role] I want to [Feature] 1. Protect Revenue 2. Increase Revenue 3. Manage Cost
  • 9.
    How much isEnough ? 3: Enough is Enough 1. No Over engineering 2. No Overdoing { planning, Analysis and Design } 3. There is no such thing as a perfect software
  • 10.
    Ubiquitous Language A Languagestructured around the domain model that everyone in the team understands. A Language capable to express a feature, its business value and behaviour ( Behaviour = Context Action Outcome )
  • 11.
  • 12.
    Gherkin ? 1. BusinessReadable DSL that expresses the ‘what’ of the software without specifying the ‘how’ i.e it describes Behaviour 2. Serves 2 Purposes : Documentation + Automated testing 3. Line oriented language like python or YAML; Uses indentation to define structure
  • 13.
    Gherkin ? Feature: Searchcourses In order to ensure better utilization of courses Potential students should be able to search for courses Scenario: Search by topic Given there are 240 courses which do not have the topic "biology" And there are 2 courses A001, B205 that each have "biology" as one of the topics When I search for "biology" Then I should see the following courses: | Course code | | A001 | | B205 | Context Action Outcome Behavior
  • 14.
  • 15.
    Cucumber ? 1. Isa Tool that executes plain-text functional descriptions as automated tests 2. Understands the ‘Gherkin’ Language 3. Itself is written in Ruby, but can be used for several languages 4. Has several language specific variations : E.g : Cucumber.js , Cucumber JVM etc …
  • 16.
    Cucumber ? Feature: Searchcourses In order to ensure better utilization of courses Potential students should be able to search for courses Scenario: Search by topic Given there are 240 courses which do not have the topic "biology" And there are 2 courses A001, B205 that each have "biology" as one of the topics When I search for "biology" Then I should see the following courses: | Course code | | A001 | | B205 | Feature Step Definitions ( Regex) Scenario ( 5- 25)
  • 17.
    Cucumber ? Product Owner/ Stakeholder DeveloperTester Features Step Definitions
  • 18.
    Cucumber : Organizingyour features Project Root features support step_defenitions Thegreatest.feature Theultimate.feature Thegreatest_steps.rb Theultimate_steps.rb env.rb
  • 19.
    Cucumber : Organizingyour features step definition Given there are 200 elements in the stack Given /^there are (d+) elements in the queue’ do #DO SOMETHING HERE @stack= Stack.new 200.times do |i| @stack.push(‘item’ + i.to_s) end end
  • 20.
    Cucumber : Organizingyour features step definition Given there are 200 elements in the stack When I add another element When /^I add another element’ do @stack.push ‘itemanother’ end
  • 21.
    Cucumber : Organizingyour features step definition Given there are 200 elements in the stack When I add another element Then there should be 201 elements Then /^There should be (d+) elements’ do @stack.count.should == 201 end
  • 22.
    MAP THIS TOAGILE TEST MATRIX
  • 23.
    Cucumber & Agiletest matrix Cucumber / Variants
  • 24.
    BUT, NOT ALLQ2 TESTS ARE API BASED TESTS
  • 25.
    ENTER CAPYBARA, SELENIUM,WEBRAT, ZOMBIE.JS AND 100 OTHER WEB APP TEST FRAMEWORKS
  • 26.
    CAPYBARA .is a librarythat can be used with cucumber to write tests that also need javascript support. SELENIUM … automates web browsers across many platform WEBRAT … allows to quickly write robust and thorough acceptance tests for a web application. By leveraging the DOM, it can run tests similarly to an in-browser testing solution without the associated performance hit (and browser dependency)
  • 27.
  • 28.
    Jasmine 1. Yet anotherBDD framework for testing your java script code 2. Evolved from Rspec 3. Suited for Q1 Tests 4. Can run in browser or server-side environments like node.js describe (“<UNIT NAME >”, function() { describe(“<BEHAVIOUR>”, function () { it(“<SHOULD DO WHAT >, function (){ }); }); });
  • 29.
    Jasmine describe (“Customer Model”,function() { var cust = new Customer(); describe(“When saved without an ID”, function () { cust.id = null; it(“should throw an ID cannot be null exception”, function (){ expect(custid.save()).toThrow(“IDCannotbeNull”); }); }); }); Your behavior reads : “Customer Model when saved without an ID should throw an ID cannot be null exception” Spec File
  • 30.
    Cucumber : Organizingyour Specs Project Root spec SomeUnitspec.js models Customermodelspec.js SpecRunner.html
  • 31.
    Jasmine & Agiletest matrix Cucumber / Variants
  • 32.
  • 33.
    Typical Flow Write theFeatures (DEV, PO, QE) Refine Features (DEV, PO, QE) Add Step Definitions (DEV, QE) Write the code / UI that you wished you had (QE/Dev) RunTest Do you want to add/modify a unit ? Describe Behavior (Dev , Verified by Arch)RunTestWrite enough to Make test pass Q2 Q1 (Dev Territory) Pass Backlog Grooming Fail
  • 34.