SlideShare a Scribd company logo
BDD using Cucumber-
JVM
Vijay Ramaswamy
Test Architect & Consultant
Introduction to BDD (~5 mins)
 BDD stands for Behavior Driven Development
 An agile software development process founded by Dan North
 Evolved from Test Driven Development (TDD):
 Often misunderstood as a testing methodology, TDD is actually a software
development process
 BDD focuses on getting the words right:
 “What is the intended behavior”, NOT “What should I test?”
 Attempts to prevent the misconceptions that arise while using TDD
 Can be applied at different levels – the overall system, a specific class, etc.
2
BDD for unit tests (~10 mins)
 This is where the BDD movement started from
 Extends the basic principles of TDD (refer to image on the right)
 Test names should be sentences, which avoid using the word
“test”, preferring words such as “should” instead:
 E.g.: emptyStackShouldThrowExceptionOnPop() instead of
testEmptyStackPop()
 Test methods usually implement the test logic directly, without
delegating to a totally different layer of the test framework
 Requires the usage of test doubles (mocks, stubs, etc.) to isolate
the code under test
3
BDD for unit tests (~10 mins)
 Other related terminologies:
 Specification Driven Development
 Key benefit:
 Provides all the benefits of TDD, with an added degree of clarity
 It is easy to implement BDD at this level using existing tools such
as JUnit, Hamcrest matchers, AssertJ, etc.
 Apart from this, specialized tools are also available. A few
Java/Groovy tools in this space: Spock, JDave, EasyB
4
BDD for acceptance tests (~10 mins)
 Acceptance criteria are defined using natural, English-like language (Domain
Specific Language - DSL):
 Think from the end user’s point of view
 Gherkin (Given-When-Then) is one popular language used in this space, although it
is not a mandatory choice
 Acceptance criteria should be directly automatable, using any appropriate
tool of choice
 Acceptance criteria are usually decoupled from their implementation:
 E.g.: Acceptance criteria using Gherkin, implementation using Java
5
BDD for acceptance tests (~10 mins)
 Acceptance criteria should be collaboratively defined by the entire team (not
just QA!) -> This is sometimes known as the 3 amigos principle (3 amigos =
Dev, BA and QA)
 Other related terminologies:
 Acceptance Test Driven Development (ATDD), Specification by Example, Domain
Driven Design
 Key benefit:
 Promotes a shared understanding of user stories within the team, and improves
overall communication
 It is difficult to implement BDD at this level without specialized tooling. Some
of the Java tools in this space: Cucumber-JVM, JBehave, Concordion
6
Outside-in BDD (~5 mins)
 It is possible and encouraged to practice BDD at both levels
mentioned in the last couple of slides:
 Start with BDD at the acceptance test layer
 Proceed to expand the acceptance criteria into lower level specifications
as appropriate
 Focus on getting the specification tests to pass first, followed by getting
the acceptance tests to pass
 This is known as outside-in BDD
7
Cucumber: An introduction
 A tool designed to enable BDD for acceptance tests:
 Originally written in Ruby, then ported to Java
 The key components of Cucumber are:
 Feature files:
 Used to define user stories and corresponding tests
 Glue code:
 Used to implement (automate) tests defined within feature files
 Glue code is further classified into:
 Step definitions
 Hooks
8
Cucumber: Feature files (~15 mins)
 Text files that use Gherkin to define the user stories (features) and
corresponding acceptance criteria (scenarios)
 Acceptance criteria should be in the form of concrete examples:
 Include actual test data within the test steps as relevant
 Feature files promote well defined traceability between requirements and
tests, and are meant to act as the “source of truth” for the entire team
9
Cucumber: Feature files (~15 mins)
 Gherkin syntax:
 Given, When, Then, And, But
 Feature, Scenario, Background, Scenario Outline & Examples
 Step arguments:
 Enable passing in more complex test data as part of a test step
 Options available:
 Doc strings
 Data tables
 Tags:
 User-defined strings prefixed with “@” -> similar to Java annotations
 The goal is to enable grouping of features/scenarios as required
 Comments:
 Use “#” to specify comments if required
10
Cucumber: Glue code (~15 mins)
 Step definitions (stepdefs):
 Code implementation for various steps used within the scenarios:
 One Java method corresponding to each unique test step
 Use annotations (such as @Given, @When, etc.) and regular expressions to match
step definition code to the corresponding steps
 You can use Lambda expressions if using Java 8 or higher
 Use method arguments to read test data specified within the test steps
 Use assertions to validate conditions specified within the test steps
 It is possible to auto-generate stepdef skeleton code from feature files:
 Simply execute your newly created feature, and Cucumber will automatically print out
code stubs corresponding to all unimplemented test steps
 WebDriver can be used to implement stepdefs for web based applications
11
Cucumber: Glue code (contd.)
 Hooks:
 Scenario hooks:
 Code that runs before or after each scenario gets executed (similar to @Before and @After in
TestNG)
 Can be used for global variable initialization, test harness setup, etc.
 Note that hooks are invisible to people reading your feature files; consider using a background
instead of a hook if it makes sense
 Tagged hooks:
 Hooks that will be executed only for specific tags
 “Global” hooks:
 “Global” hooks are those which would run only once per execution (similar to @BeforeClass and
@BeforeSuite in TestNG)
 These are NOT supported in Cucumber!
 If you do need this, you may need to implement some kind of programmatic work-around
12
Cucumber: Test execution (~15 mins)
 Cucumber provides multiple test runners that you can use:
 JUnit runner
 TestNG runner
 Command line runner
 All test runners have a common set of configuration options. A few prominent
options:
 Path to the feature files:
 A list of feature files, or a list of directories containing feature files
 If left unspecified, Cucumber searches within the same package as the test runner
13
Cucumber: Test execution (~15 mins)
 All test runners have a common set of configuration options. A few prominent
options (contd.):
 Path to the glue code:
 A list of packages on the classpath
 If left unspecified, Cucumber searches within the same package as the test runner
 A set of tags to be executed
 Plugins to be used for formatting your test reports (refer next slide for more
details)
 “Strict” execution:
 Set to true if you want Cucumber to fail scenarios with unimplemented / pending step
definitions
 Default is “false”
14
Cucumber: Reporting (~10 mins)
 Cucumber enables generation of test execution reports by specifying a list of
report formatting plugins while configuring your test runner
 Various formatting plugins are available in-built, such as:
 Pretty
 JUnit
 HTML
 JSON
 Usage
15
Cucumber: Reporting (~10 mins)
 It is easy to create your own formatting plugins as well:
 This has led to the development of various custom plugins, many of which are even
more user-friendly and visually appealing than the built-in ones
 It is possible to include your own custom content into a report:
 The scenario.write() API:
 Embed text into a report
 The scenario.embed() API:
 Embed images or even videos into a report
16
Cucumber: Best practices (~5 mins)
 Avoid using Cucumber as a standalone automation framework -> remember
that it is primarily designed as a BDD tool!
 Write scenarios at the business logic level -> avoid implementation details:
 Test implementations may change, but the tests themselves should not!
 Keep scenarios independent of each other as far as possible
 Organize features neatly into Epics or Themes as appropriate, so that they are
easy to search when required
 Reuse stepdefs to the maximum extent possible; avoid duplication of steps
while writing scenarios:
 Use Eclipse autocomplete
 Evolve a common DSL for the entire team
17
Cucumber: Best practices (~5 mins)
 Keep stepdef methods unique:
 It is a common mistake to repeat stepdefs in multiple Java files, leading to
ambiguous matches during execution
 Leverage backgrounds wisely to keep your scenarios more crisp and readable
 Avoid scenario outlines unless really necessary – most times, a single set of
test data should be sufficient to test a given scenario
 Use tags to organize your scenarios and features into groups as appropriate
 Use dependency injection to share state between step definitions as required
 Feature files should be the single source of truth for the entire team:
 If using a system such as JIRA, explore options to integrate
18
Reference material
 Behavior Driven Development references:
 http://behaviourdriven.org/
 https://dannorth.net/introducing-bdd/
 Demo websites for practice:
 http://automationpractice.com
 http://newtours.demoaut.com/
 http://demoqa.com/
 http://www.way2automation.com
19
Recap
 An overview of BDD:
 Introduction to BDD
 BDD for unit tests
 BDD for acceptance tests
 Outside-in BDD
 An overview of Cucumber:
 Introduction to Cucumber
 Feature files
 Glue code
 Test execution
 Reporting
 Best practices
20
THANK YOU!
Vijay Ramaswamy
Test Architect & Consultant
21

More Related Content

PPTX
PPT
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
PPSX
Maven Presentation - SureFire vs FailSafe
PPT
Demystifying Maven
PPT
Maven Overview
PDF
Apache maven, a software project management tool
PPTX
Introduction to Maven
PDF
Selenium - Introduction
Selenium-Browser-Based-Automated-Testing-for-Grails-Apps
Maven Presentation - SureFire vs FailSafe
Demystifying Maven
Maven Overview
Apache maven, a software project management tool
Introduction to Maven
Selenium - Introduction

What's hot (20)

PDF
Maven tutorial
PPTX
An introduction to Maven
PPTX
Maven ppt
PPT
Selenium Java for Beginners by Sujit Pathak
PPTX
Maven
PDF
Apache Maven In 10 Slides
PPT
Testing Java Web Apps With Selenium
ODP
An Introduction to Maven Part 1
ODP
Mastering selenium for automated acceptance tests
PPTX
Maven Basics - Explained
PPTX
Maven 2 Introduction
PDF
Intelligent Projects with Maven - DevFest Istanbul
PPTX
Apache Maven
PDF
Note - Apache Maven Intro
PPTX
An Introduction to Maven
PPT
Selenium2 and Jenkins: Almost pain-free UI Testing
PDF
Basics of Selenium IDE,Core, Remote Control
PPTX
Automation Tools Overview
PDF
Selenium 2 - PyCon 2011
PPTX
Maven tutorial
An introduction to Maven
Maven ppt
Selenium Java for Beginners by Sujit Pathak
Maven
Apache Maven In 10 Slides
Testing Java Web Apps With Selenium
An Introduction to Maven Part 1
Mastering selenium for automated acceptance tests
Maven Basics - Explained
Maven 2 Introduction
Intelligent Projects with Maven - DevFest Istanbul
Apache Maven
Note - Apache Maven Intro
An Introduction to Maven
Selenium2 and Jenkins: Almost pain-free UI Testing
Basics of Selenium IDE,Core, Remote Control
Automation Tools Overview
Selenium 2 - PyCon 2011
Ad

Similar to BDD using Cucumber JVM (20)

PDF
Behavior Driven Development with SpecFlow
PDF
Behavior Driven Testing with SpecFlow
PPTX
Top 20 cucumber interview questions for sdet
PPT
Behavior Driven Development by Example
PPTX
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
PPTX
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
PDF
Master Cucumber cheat sheet for testing .pdf
PPTX
CucumberSeleniumWD
PPTX
So What Do Cucumbers Have To Do With Testing
PPTX
Cucumber jvm best practices v3
PDF
Prod-Like Integration Testing for Distributed Containerized Applications
PDF
Getting started with karate dsl
PPTX
Cypress Testing.pptx
PDF
cucumber harpal.pdf
PDF
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
PPTX
Automated Acceptance Tests & Tool choice
ODP
Best practice adoption (and lack there of)
PPT
Stopping the Rot - Putting Legacy C++ Under Test
PPTX
PHPConf.asia 2016 - BDD with Behat for Beginners
Behavior Driven Development with SpecFlow
Behavior Driven Testing with SpecFlow
Top 20 cucumber interview questions for sdet
Behavior Driven Development by Example
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
DevLabs Alliance top 20 Cucumber Interview Questions for SDET
Master Cucumber cheat sheet for testing .pdf
CucumberSeleniumWD
So What Do Cucumbers Have To Do With Testing
Cucumber jvm best practices v3
Prod-Like Integration Testing for Distributed Containerized Applications
Getting started with karate dsl
Cypress Testing.pptx
cucumber harpal.pdf
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
Automated Acceptance Tests & Tool choice
Best practice adoption (and lack there of)
Stopping the Rot - Putting Legacy C++ Under Test
PHPConf.asia 2016 - BDD with Behat for Beginners
Ad

Recently uploaded (20)

PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
Empathic Computing: Creating Shared Understanding
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
A Presentation on Artificial Intelligence
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
Machine Learning_overview_presentation.pptx
PPTX
TLE Review Electricity (Electricity).pptx
PDF
Encapsulation theory and applications.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPT
Teaching material agriculture food technology
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Encapsulation_ Review paper, used for researhc scholars
Empathic Computing: Creating Shared Understanding
Reach Out and Touch Someone: Haptics and Empathic Computing
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Group 1 Presentation -Planning and Decision Making .pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
A Presentation on Artificial Intelligence
Programs and apps: productivity, graphics, security and other tools
NewMind AI Weekly Chronicles - August'25-Week II
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Network Security Unit 5.pdf for BCA BBA.
Diabetes mellitus diagnosis method based random forest with bat algorithm
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Digital-Transformation-Roadmap-for-Companies.pptx
Machine Learning_overview_presentation.pptx
TLE Review Electricity (Electricity).pptx
Encapsulation theory and applications.pdf
A comparative study of natural language inference in Swahili using monolingua...
Teaching material agriculture food technology
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...

BDD using Cucumber JVM

  • 1. BDD using Cucumber- JVM Vijay Ramaswamy Test Architect & Consultant
  • 2. Introduction to BDD (~5 mins)  BDD stands for Behavior Driven Development  An agile software development process founded by Dan North  Evolved from Test Driven Development (TDD):  Often misunderstood as a testing methodology, TDD is actually a software development process  BDD focuses on getting the words right:  “What is the intended behavior”, NOT “What should I test?”  Attempts to prevent the misconceptions that arise while using TDD  Can be applied at different levels – the overall system, a specific class, etc. 2
  • 3. BDD for unit tests (~10 mins)  This is where the BDD movement started from  Extends the basic principles of TDD (refer to image on the right)  Test names should be sentences, which avoid using the word “test”, preferring words such as “should” instead:  E.g.: emptyStackShouldThrowExceptionOnPop() instead of testEmptyStackPop()  Test methods usually implement the test logic directly, without delegating to a totally different layer of the test framework  Requires the usage of test doubles (mocks, stubs, etc.) to isolate the code under test 3
  • 4. BDD for unit tests (~10 mins)  Other related terminologies:  Specification Driven Development  Key benefit:  Provides all the benefits of TDD, with an added degree of clarity  It is easy to implement BDD at this level using existing tools such as JUnit, Hamcrest matchers, AssertJ, etc.  Apart from this, specialized tools are also available. A few Java/Groovy tools in this space: Spock, JDave, EasyB 4
  • 5. BDD for acceptance tests (~10 mins)  Acceptance criteria are defined using natural, English-like language (Domain Specific Language - DSL):  Think from the end user’s point of view  Gherkin (Given-When-Then) is one popular language used in this space, although it is not a mandatory choice  Acceptance criteria should be directly automatable, using any appropriate tool of choice  Acceptance criteria are usually decoupled from their implementation:  E.g.: Acceptance criteria using Gherkin, implementation using Java 5
  • 6. BDD for acceptance tests (~10 mins)  Acceptance criteria should be collaboratively defined by the entire team (not just QA!) -> This is sometimes known as the 3 amigos principle (3 amigos = Dev, BA and QA)  Other related terminologies:  Acceptance Test Driven Development (ATDD), Specification by Example, Domain Driven Design  Key benefit:  Promotes a shared understanding of user stories within the team, and improves overall communication  It is difficult to implement BDD at this level without specialized tooling. Some of the Java tools in this space: Cucumber-JVM, JBehave, Concordion 6
  • 7. Outside-in BDD (~5 mins)  It is possible and encouraged to practice BDD at both levels mentioned in the last couple of slides:  Start with BDD at the acceptance test layer  Proceed to expand the acceptance criteria into lower level specifications as appropriate  Focus on getting the specification tests to pass first, followed by getting the acceptance tests to pass  This is known as outside-in BDD 7
  • 8. Cucumber: An introduction  A tool designed to enable BDD for acceptance tests:  Originally written in Ruby, then ported to Java  The key components of Cucumber are:  Feature files:  Used to define user stories and corresponding tests  Glue code:  Used to implement (automate) tests defined within feature files  Glue code is further classified into:  Step definitions  Hooks 8
  • 9. Cucumber: Feature files (~15 mins)  Text files that use Gherkin to define the user stories (features) and corresponding acceptance criteria (scenarios)  Acceptance criteria should be in the form of concrete examples:  Include actual test data within the test steps as relevant  Feature files promote well defined traceability between requirements and tests, and are meant to act as the “source of truth” for the entire team 9
  • 10. Cucumber: Feature files (~15 mins)  Gherkin syntax:  Given, When, Then, And, But  Feature, Scenario, Background, Scenario Outline & Examples  Step arguments:  Enable passing in more complex test data as part of a test step  Options available:  Doc strings  Data tables  Tags:  User-defined strings prefixed with “@” -> similar to Java annotations  The goal is to enable grouping of features/scenarios as required  Comments:  Use “#” to specify comments if required 10
  • 11. Cucumber: Glue code (~15 mins)  Step definitions (stepdefs):  Code implementation for various steps used within the scenarios:  One Java method corresponding to each unique test step  Use annotations (such as @Given, @When, etc.) and regular expressions to match step definition code to the corresponding steps  You can use Lambda expressions if using Java 8 or higher  Use method arguments to read test data specified within the test steps  Use assertions to validate conditions specified within the test steps  It is possible to auto-generate stepdef skeleton code from feature files:  Simply execute your newly created feature, and Cucumber will automatically print out code stubs corresponding to all unimplemented test steps  WebDriver can be used to implement stepdefs for web based applications 11
  • 12. Cucumber: Glue code (contd.)  Hooks:  Scenario hooks:  Code that runs before or after each scenario gets executed (similar to @Before and @After in TestNG)  Can be used for global variable initialization, test harness setup, etc.  Note that hooks are invisible to people reading your feature files; consider using a background instead of a hook if it makes sense  Tagged hooks:  Hooks that will be executed only for specific tags  “Global” hooks:  “Global” hooks are those which would run only once per execution (similar to @BeforeClass and @BeforeSuite in TestNG)  These are NOT supported in Cucumber!  If you do need this, you may need to implement some kind of programmatic work-around 12
  • 13. Cucumber: Test execution (~15 mins)  Cucumber provides multiple test runners that you can use:  JUnit runner  TestNG runner  Command line runner  All test runners have a common set of configuration options. A few prominent options:  Path to the feature files:  A list of feature files, or a list of directories containing feature files  If left unspecified, Cucumber searches within the same package as the test runner 13
  • 14. Cucumber: Test execution (~15 mins)  All test runners have a common set of configuration options. A few prominent options (contd.):  Path to the glue code:  A list of packages on the classpath  If left unspecified, Cucumber searches within the same package as the test runner  A set of tags to be executed  Plugins to be used for formatting your test reports (refer next slide for more details)  “Strict” execution:  Set to true if you want Cucumber to fail scenarios with unimplemented / pending step definitions  Default is “false” 14
  • 15. Cucumber: Reporting (~10 mins)  Cucumber enables generation of test execution reports by specifying a list of report formatting plugins while configuring your test runner  Various formatting plugins are available in-built, such as:  Pretty  JUnit  HTML  JSON  Usage 15
  • 16. Cucumber: Reporting (~10 mins)  It is easy to create your own formatting plugins as well:  This has led to the development of various custom plugins, many of which are even more user-friendly and visually appealing than the built-in ones  It is possible to include your own custom content into a report:  The scenario.write() API:  Embed text into a report  The scenario.embed() API:  Embed images or even videos into a report 16
  • 17. Cucumber: Best practices (~5 mins)  Avoid using Cucumber as a standalone automation framework -> remember that it is primarily designed as a BDD tool!  Write scenarios at the business logic level -> avoid implementation details:  Test implementations may change, but the tests themselves should not!  Keep scenarios independent of each other as far as possible  Organize features neatly into Epics or Themes as appropriate, so that they are easy to search when required  Reuse stepdefs to the maximum extent possible; avoid duplication of steps while writing scenarios:  Use Eclipse autocomplete  Evolve a common DSL for the entire team 17
  • 18. Cucumber: Best practices (~5 mins)  Keep stepdef methods unique:  It is a common mistake to repeat stepdefs in multiple Java files, leading to ambiguous matches during execution  Leverage backgrounds wisely to keep your scenarios more crisp and readable  Avoid scenario outlines unless really necessary – most times, a single set of test data should be sufficient to test a given scenario  Use tags to organize your scenarios and features into groups as appropriate  Use dependency injection to share state between step definitions as required  Feature files should be the single source of truth for the entire team:  If using a system such as JIRA, explore options to integrate 18
  • 19. Reference material  Behavior Driven Development references:  http://behaviourdriven.org/  https://dannorth.net/introducing-bdd/  Demo websites for practice:  http://automationpractice.com  http://newtours.demoaut.com/  http://demoqa.com/  http://www.way2automation.com 19
  • 20. Recap  An overview of BDD:  Introduction to BDD  BDD for unit tests  BDD for acceptance tests  Outside-in BDD  An overview of Cucumber:  Introduction to Cucumber  Feature files  Glue code  Test execution  Reporting  Best practices 20
  • 21. THANK YOU! Vijay Ramaswamy Test Architect & Consultant 21