SlideShare a Scribd company logo
1 of 22
Download to read offline
Behavior Driven Development
with Cucumber JVM
By Gopal Shah
Agenda
● TDD session recap
● BDD solution analysis
● Cucumber features
● Alternative BDD frameworks
● Q & A
BDD impact
● Behavior is more useful than “test”
● Ubiquitous language
● Increased collaboration
● Living documentation
Cucumber internals
● Gherkin
● Ignores keywords
● Step results → Passed, Failed, Pending,
Undefined
● World object
Tips & Tricks
● Use capture groups
● Distinguish singular/plural with '?'
● Use non-capturing groups (?:visit|go to)
When I visit the homepage
When I go to the homepage
● Use anchors (“^ … $”)
● Use tags and folders to organize
Use Doc Strings
Scenario: Ban Unscrupulous Users
When I behave unscrupulously
Then I should receive an email containing:
"""
Dear Sir,
Your account privileges have been revoked due to your unscrupulous behavior.
Sincerely,
The Management
"""
And my account should be locked
Doc Strings Step Defn
@When("^I behave unscrupulously$")
@Then("^I should receive an email containing:$")
public void I_should_receive_an_email_containing(String
arg1) throws Throwable {
throw new PendingException();
}
@Then("^my account should be locked$")
Data Tables
Scenario:
Given a board like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | | | |
| 3 | | | |
When player x plays in row 2, column 1
Then the board should look like this:
| | 1 | 2 | 3 |
| 1 | | | |
| 2 | x | | |
| 3 | | | |
Data tables Step Defn
@Given("^a board like this:$")
public void a_board_like_this(String[][] twoDStringArray)
{}
@When("^player x plays in row (d+), column (d+)$")
public void player_x_plays_in_row_column(int row, int
column) {}
@Then("^the board should look like this:$")
public void the_board_should_look_like_this(String[][]
twoDStringArray) {}
Scenario Hooks
import cucumber.annotation.After;
import cucumber.annotation.Before;
@Before public void beforeScenario() {
tomcat.start();
tomcat.deploy("myapp");
}
@After public void afterScenario() {
tomcat.stop();
}
Tagged Hooks
@Before(value = {"@txn"}, order = 100)
public void startTransaction() {
}
@After(value = {"@txn"}, order = 100)
public void rollBackTransaction() {
}
SpringTransactionHooks
Global Hooks
@Before("@web")
public void beforeScenario() {
if (!tomcat.isRunning()) {
tomcat.start();
tomcat.deploy("myapp");
browser = new FirefoxDriver();
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
browser.close();
tomcat.stop();
}
}); } }
Features vs Stories
Recommended NOT to do the following:
features/
story_38971_generate_new_report.feature
story_38986_run_report.feature
story_39004_log_in.feature
How NOT to have bored
stakeholders
Scenario: Check inbox
Given a User "Dave" with password "password"
And a User "Sue" with password "secret"
And an email to "Dave" from "Sue"
When I sign in as "Dave" with password
"password"
Then I should see 1 email from "Sue" in my
inbox
Avoid incidental details
Scenario: Check inbox
Given a User "Dave"
And a User "Sue"
And an email to "Dave" from "Sue"
When I sign in as "Dave"
Then I should see 1 email from "Sue" in my inbox
Can be further improved ...
Scenario: Check inbox
Given I have received an email from "Sue"
When I sign in
Then I should see 1 email from "Sue" in my inbox
Imperative steps
Feature: Animal Submission
As a Zoologist
I want to add a new animal to the site
So that I can share my animal knowledge with the community
Scenario: successful submission
Given I'm on the animal creation page
When I fill in Name with 'Alligator'
And select Phylum as 'Chordata'
And fill in Animal Class with 'Sauropsida'
And fill in Order with 'Crocodilia'
And fill in Family with 'Alligatoridae'
And fill in Genus with 'Alligator'
And check Lay Eggs
And click the Create button
Then I should see the notice 'Thank you for your animal submission!'
And the page should include the animal's name, phylum, animal
class, order, family, and genus
Declarative steps
Feature: Animal Submission
As a Zoologist
I want to add a new animal to the site
So that I can share my animal knowledge with the
community
Scenario: successful submission
Given I'm on the animal creation page
When I add a new animal
Then I should see the page for my newly created animal
And the notice 'Thank you for your animal submission!'
StepDefn for Declarative steps
@When("^I add a new animal$")
public void I_add_a_new_animal() throws Throwable {
fillsIn("Name",with("Alligator"));
selects("Chordata"),from("Phylum"));
fillsIn("Animal Class",with("Sauropsida"));
fillsIn("Order",with("Crocodilia"));
fillsIn("Family",with("Alligatoridae"));
fillsIn("Genus",with("Alligator"));
checks("Lay Eggs");
clicks_button("Create");}
Don't make it too declarative …
Scenario: The whole system
Given the system exists
When I use it
Then it should work, perfectly
Alternative frameworks
● Jbehavior
+ve: namedParameters, compositeSteps
-ve: DocString, formatting, gherkin standards
● Concordian
Composite steps in JBehavior
@Given("%customer has previously bought a %product") //
used in normal parameter matching
@Alias("<customer> has previously bought a <product>") //
used in parameterised scenarios
@Composite(steps = { "Given <customer> is logged in",
"Given <customer> has a cart",
"When a <product> is added to the cart" })
public void aCompositeStep(@Named("customer") String
customer, @Named("product") String product) { }

More Related Content

What's hot

Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
Simon Willison
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Hiroshi Shibamura
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
Inbal Geffen
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
Jeremy Kendall
 

What's hot (20)

Dollar symbol
Dollar symbolDollar symbol
Dollar symbol
 
Learning jQuery in 30 minutes
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
 
jQuery Fundamentals
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
 
The jQuery Divide
The jQuery DivideThe jQuery Divide
The jQuery Divide
 
jQuery Essentials
jQuery EssentialsjQuery Essentials
jQuery Essentials
 
Rediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The LibrariesRediscovering JavaScript: The Language Behind The Libraries
Rediscovering JavaScript: The Language Behind The Libraries
 
Crowdsourcing with Django
Crowdsourcing with DjangoCrowdsourcing with Django
Crowdsourcing with Django
 
Testing Web Applications with GEB
Testing Web Applications with GEBTesting Web Applications with GEB
Testing Web Applications with GEB
 
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2Introduction to DBIx::Lite - Kyoto.pm tech talk #2
Introduction to DBIx::Lite - Kyoto.pm tech talk #2
 
DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009DataMapper @ RubyEnRails2009
DataMapper @ RubyEnRails2009
 
PhoneGap: Local Storage
PhoneGap: Local StoragePhoneGap: Local Storage
PhoneGap: Local Storage
 
Jquery
JqueryJquery
Jquery
 
Jqeury ajax plugins
Jqeury ajax pluginsJqeury ajax plugins
Jqeury ajax plugins
 
Intro to jQuery
Intro to jQueryIntro to jQuery
Intro to jQuery
 
Automatically Spotting Cross-language Relations
Automatically Spotting Cross-language RelationsAutomatically Spotting Cross-language Relations
Automatically Spotting Cross-language Relations
 
Php 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the GoodPhp 102: Out with the Bad, In with the Good
Php 102: Out with the Bad, In with the Good
 
JQuery introduction
JQuery introductionJQuery introduction
JQuery introduction
 
Jquery introduction
Jquery introductionJquery introduction
Jquery introduction
 
jQuery
jQueryjQuery
jQuery
 
Leveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHPLeveraging the Power of Graph Databases in PHP
Leveraging the Power of Graph Databases in PHP
 

Viewers also liked

Limas 131127003659-phpapp02
Limas 131127003659-phpapp02Limas 131127003659-phpapp02
Limas 131127003659-phpapp02
Phond Sarsen
 
Question 1 media studies evaluation mark final
Question 1 media studies evaluation mark final Question 1 media studies evaluation mark final
Question 1 media studies evaluation mark final
salesian2014as
 
Mark Mulcahy Question 1 media studies evaluation
Mark Mulcahy Question 1 media studies evaluationMark Mulcahy Question 1 media studies evaluation
Mark Mulcahy Question 1 media studies evaluation
salesian2014as
 
Mark Question 3 media studies evaluation
Mark Question 3 media studies evaluationMark Question 3 media studies evaluation
Mark Question 3 media studies evaluation
salesian2014as
 
Mary C6 Evaluation Question 1
Mary C6 Evaluation Question 1Mary C6 Evaluation Question 1
Mary C6 Evaluation Question 1
salesian2014as
 

Viewers also liked (16)

Limas 131127003659-phpapp02
Limas 131127003659-phpapp02Limas 131127003659-phpapp02
Limas 131127003659-phpapp02
 
The Great War
The Great WarThe Great War
The Great War
 
Question 1 media studies evaluation mark final
Question 1 media studies evaluation mark final Question 1 media studies evaluation mark final
Question 1 media studies evaluation mark final
 
6. What have you learnt about technologies from constructing this product?
6. What have you learnt about technologies from constructing this product?6. What have you learnt about technologies from constructing this product?
6. What have you learnt about technologies from constructing this product?
 
Laptop tune up
Laptop tune upLaptop tune up
Laptop tune up
 
Evaluation 1
Evaluation 1Evaluation 1
Evaluation 1
 
GRID Signage: Future of Digital Signage
GRID Signage: Future of Digital SignageGRID Signage: Future of Digital Signage
GRID Signage: Future of Digital Signage
 
Presentasi kki12 edit 3 n
Presentasi kki12 edit 3 nPresentasi kki12 edit 3 n
Presentasi kki12 edit 3 n
 
The making of Lys-de-Membre - Part 1
The making of Lys-de-Membre - Part 1The making of Lys-de-Membre - Part 1
The making of Lys-de-Membre - Part 1
 
Mark Mulcahy Question 1 media studies evaluation
Mark Mulcahy Question 1 media studies evaluationMark Mulcahy Question 1 media studies evaluation
Mark Mulcahy Question 1 media studies evaluation
 
Penguasaan bhs
Penguasaan bhsPenguasaan bhs
Penguasaan bhs
 
DPHEP_BLUETWO_001
DPHEP_BLUETWO_001DPHEP_BLUETWO_001
DPHEP_BLUETWO_001
 
Mark Question 3 media studies evaluation
Mark Question 3 media studies evaluationMark Question 3 media studies evaluation
Mark Question 3 media studies evaluation
 
Mary C6 Evaluation Question 1
Mary C6 Evaluation Question 1Mary C6 Evaluation Question 1
Mary C6 Evaluation Question 1
 
Beauty Industry Marketing Facts And Trends
Beauty Industry Marketing Facts And Trends Beauty Industry Marketing Facts And Trends
Beauty Industry Marketing Facts And Trends
 
Presentation1
Presentation1Presentation1
Presentation1
 

Similar to AmdJavaMeetupBDDUsingCucumber

jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
girish82
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
Jarod Ferguson
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
David Giard
 

Similar to AmdJavaMeetupBDDUsingCucumber (20)

jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
 
Cheap frontend tricks
Cheap frontend tricksCheap frontend tricks
Cheap frontend tricks
 
JavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talkJavaScript Neednt Hurt - JavaBin talk
JavaScript Neednt Hurt - JavaBin talk
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
bcgr3-jquery
bcgr3-jquerybcgr3-jquery
bcgr3-jquery
 
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
TDC 2014 - JavaScript de qualidade: hoje, amanhã e sempre!
 
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)How I Learned to Stop Worrying and Love jQuery (Jan 2013)
How I Learned to Stop Worrying and Love jQuery (Jan 2013)
 
JavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New WorldJavaScript & HTML5 - Brave New World
JavaScript & HTML5 - Brave New World
 
Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016Async Redux Actions With RxJS - React Rally 2016
Async Redux Actions With RxJS - React Rally 2016
 
jQuery introduction
jQuery introductionjQuery introduction
jQuery introduction
 
Javascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introductionJavascript & jQuery: A pragmatic introduction
Javascript & jQuery: A pragmatic introduction
 
I put on my mink and wizard behat (tutorial)
I put on my mink and wizard behat (tutorial)I put on my mink and wizard behat (tutorial)
I put on my mink and wizard behat (tutorial)
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
An Introduction to Jquery
An Introduction to JqueryAn Introduction to Jquery
An Introduction to Jquery
 
How to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrainHow to actually use promises - Jakob Mattsson, FishBrain
How to actually use promises - Jakob Mattsson, FishBrain
 
Idioms in swift 2016 05c
Idioms in swift 2016 05cIdioms in swift 2016 05c
Idioms in swift 2016 05c
 
Building Go Web Apps
Building Go Web AppsBuilding Go Web Apps
Building Go Web Apps
 
jQuery Rescue Adventure
jQuery Rescue AdventurejQuery Rescue Adventure
jQuery Rescue Adventure
 
JavaScript and HTML5 - Brave New World (revised)
JavaScript and HTML5 - Brave New World (revised)JavaScript and HTML5 - Brave New World (revised)
JavaScript and HTML5 - Brave New World (revised)
 

Recently uploaded

CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 

Recently uploaded (20)

How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 

AmdJavaMeetupBDDUsingCucumber

  • 1. Behavior Driven Development with Cucumber JVM By Gopal Shah
  • 2. Agenda ● TDD session recap ● BDD solution analysis ● Cucumber features ● Alternative BDD frameworks ● Q & A
  • 3. BDD impact ● Behavior is more useful than “test” ● Ubiquitous language ● Increased collaboration ● Living documentation
  • 4. Cucumber internals ● Gherkin ● Ignores keywords ● Step results → Passed, Failed, Pending, Undefined ● World object
  • 5. Tips & Tricks ● Use capture groups ● Distinguish singular/plural with '?' ● Use non-capturing groups (?:visit|go to) When I visit the homepage When I go to the homepage ● Use anchors (“^ … $”) ● Use tags and folders to organize
  • 6. Use Doc Strings Scenario: Ban Unscrupulous Users When I behave unscrupulously Then I should receive an email containing: """ Dear Sir, Your account privileges have been revoked due to your unscrupulous behavior. Sincerely, The Management """ And my account should be locked
  • 7. Doc Strings Step Defn @When("^I behave unscrupulously$") @Then("^I should receive an email containing:$") public void I_should_receive_an_email_containing(String arg1) throws Throwable { throw new PendingException(); } @Then("^my account should be locked$")
  • 8. Data Tables Scenario: Given a board like this: | | 1 | 2 | 3 | | 1 | | | | | 2 | | | | | 3 | | | | When player x plays in row 2, column 1 Then the board should look like this: | | 1 | 2 | 3 | | 1 | | | | | 2 | x | | | | 3 | | | |
  • 9. Data tables Step Defn @Given("^a board like this:$") public void a_board_like_this(String[][] twoDStringArray) {} @When("^player x plays in row (d+), column (d+)$") public void player_x_plays_in_row_column(int row, int column) {} @Then("^the board should look like this:$") public void the_board_should_look_like_this(String[][] twoDStringArray) {}
  • 10. Scenario Hooks import cucumber.annotation.After; import cucumber.annotation.Before; @Before public void beforeScenario() { tomcat.start(); tomcat.deploy("myapp"); } @After public void afterScenario() { tomcat.stop(); }
  • 11. Tagged Hooks @Before(value = {"@txn"}, order = 100) public void startTransaction() { } @After(value = {"@txn"}, order = 100) public void rollBackTransaction() { } SpringTransactionHooks
  • 12. Global Hooks @Before("@web") public void beforeScenario() { if (!tomcat.isRunning()) { tomcat.start(); tomcat.deploy("myapp"); browser = new FirefoxDriver(); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { browser.close(); tomcat.stop(); } }); } }
  • 13. Features vs Stories Recommended NOT to do the following: features/ story_38971_generate_new_report.feature story_38986_run_report.feature story_39004_log_in.feature
  • 14. How NOT to have bored stakeholders Scenario: Check inbox Given a User "Dave" with password "password" And a User "Sue" with password "secret" And an email to "Dave" from "Sue" When I sign in as "Dave" with password "password" Then I should see 1 email from "Sue" in my inbox
  • 15. Avoid incidental details Scenario: Check inbox Given a User "Dave" And a User "Sue" And an email to "Dave" from "Sue" When I sign in as "Dave" Then I should see 1 email from "Sue" in my inbox
  • 16. Can be further improved ... Scenario: Check inbox Given I have received an email from "Sue" When I sign in Then I should see 1 email from "Sue" in my inbox
  • 17. Imperative steps Feature: Animal Submission As a Zoologist I want to add a new animal to the site So that I can share my animal knowledge with the community Scenario: successful submission Given I'm on the animal creation page When I fill in Name with 'Alligator' And select Phylum as 'Chordata' And fill in Animal Class with 'Sauropsida' And fill in Order with 'Crocodilia' And fill in Family with 'Alligatoridae' And fill in Genus with 'Alligator' And check Lay Eggs And click the Create button Then I should see the notice 'Thank you for your animal submission!' And the page should include the animal's name, phylum, animal class, order, family, and genus
  • 18. Declarative steps Feature: Animal Submission As a Zoologist I want to add a new animal to the site So that I can share my animal knowledge with the community Scenario: successful submission Given I'm on the animal creation page When I add a new animal Then I should see the page for my newly created animal And the notice 'Thank you for your animal submission!'
  • 19. StepDefn for Declarative steps @When("^I add a new animal$") public void I_add_a_new_animal() throws Throwable { fillsIn("Name",with("Alligator")); selects("Chordata"),from("Phylum")); fillsIn("Animal Class",with("Sauropsida")); fillsIn("Order",with("Crocodilia")); fillsIn("Family",with("Alligatoridae")); fillsIn("Genus",with("Alligator")); checks("Lay Eggs"); clicks_button("Create");}
  • 20. Don't make it too declarative … Scenario: The whole system Given the system exists When I use it Then it should work, perfectly
  • 21. Alternative frameworks ● Jbehavior +ve: namedParameters, compositeSteps -ve: DocString, formatting, gherkin standards ● Concordian
  • 22. Composite steps in JBehavior @Given("%customer has previously bought a %product") // used in normal parameter matching @Alias("<customer> has previously bought a <product>") // used in parameterised scenarios @Composite(steps = { "Given <customer> is logged in", "Given <customer> has a cart", "When a <product> is added to the cart" }) public void aCompositeStep(@Named("customer") String customer, @Named("product") String product) { }