BDD
Behaviour Driven Development
Presented by Wael ANIBA - UP JEE @ESPRIT - 2013/2014 -
[email protected]Why is BDD important?
I had a problem. While using and teaching agile practices like test-driven development (TDD) on projects in different environments. Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand why a test fails.
What is BDD?
BDD is a synthesis and refinement of practices stemming from TDD (Test Driven Development) and ATDD (Acceptance Test Driven Development). It is about implementing an application by describing it from the point of view of its stakeholders. Stories Scenarios and Steps
Principles of BDD
Unit test names be whole sentences starting with the word "should" and should be written in order of business value. Acceptance tests should be written using the standard agile framework of a User story: "As a [role] I want [feature] so that [benefit]". Acceptance criteria should be written in terms of scenarios and implemented as classes: Given [initial context], when [event occurs], then [ensure some outcomes].
User Stories
User Stories
Series of acceptance criteria Called scenarios Each scenario has givens, events and outcomes Used in Agile projects
Behaviour
Expressed as a test method It defines what the application should / shouldnt do
User Stories A Story Structure
Narrative
As a [some_role_here] I want [some_feature_here] So that [behefit/value_of_the_feature_here]
Scenario(s) (this is acceptance criteria)
Given [some_initial_condition(s)] When [event(s)_occurs] Then [ensure_some_outcome(s)]
5
BDD Process
Customer Domain Expert QA Developer
Stories
Matches to
Behaviour
Application
Exercise expectations against
BDD Process
Create scenarios for expected features Then follow the process:
1. 2. 3.
4.
5. 6. 7.
For each scenario describing a feature Run the scenario it fails (go red) Define the first step go red Write down the application code getting the step to pass go green Refactor the code and repeat steps 4 & 5 for each step until 6 The scenario passes go green Refactor the application code
Narrative
Describe the intent of the story
Narrative: In order to communicate effectively to the business functionality As a development team member I want to use Behaviour-Driven Development
Scenario
The intent of the scenario
Narrative: In order to communicate effectively to the business functionality As a development team member I want to use Behaviour-Driven Development
Scenario: A scenario is a collection of executable steps of different type
Steps
Given,When,Then The Gherkin Style
Narrative: In order to communicate effectively to the business functionality As a development team member I want to use Behaviour-Driven Development
Scenario: A scenario is a collection of executable steps of different type Given some initial context (the givens), When an event occurs, Then ensure some outcomes (the assertions)
10
BDD Tools
C
Cspec C++ CppSpec, Spec-CPP .Net NBehave, NSpecify, SpecFlow Groovy GSpec, easyb, Cuke4Duke PHP PHPSpec Python Specipy Ruby RSpec, Shoulda, Cucumber More
11
BDD Tools for java
JBehave Java annotations based, Test frameworks agnostic Cuke4duke Cucumber support for JVM JDave RSpec (Ruby) inspired, Mojo 2 & Hamcrest based beanSpec Java based easyb Java based, Specifications written in Groovy instinct BDD framework for Java, providing annotations for contexts. Inspired by Rspec BDoc - Extracts behaviour from unit tests
12
And the Java Side ?
BDD
Test Driven Development
JBehave
Open Source Java-based BDD Framework
Created by Dan North (2003) to compare BDD to TDD
Current version 3.4.2 Components
JBehave Main JBehave Web
14
Jbehave features
Pure Java implementation Text-based user stories Annotation-based binding of textual steps to Java methods DI support (Spring, Guice, Pico, Weld) Story reporting (HTML, TXT, XML) User stories localization IDE integration Ant and Maven integration Test frameworks agnostic (JUnit, hamcrest, etc.)
15
Jbehave web features
Extension of JBehave providing web integration layer for BDD Features: JBehave Queue allows generic stories to be
run asynchronously
synchronously
Web
Runner allows generic stories to be run Integration automation of stories for
Selenium
web apps using Selenium
16
Jbehave
17
Jbehave Eclipse Plugin
JBehave provides an Eclipse Plugin with a custom JBehave Story Editor:
Syntax highlighting Step hyperlink detection and link to corresponding Java method Step autocompletion Step validation, detecting both unimplemented steps and ambiguous steps, i.e. matching multiple methods Etc.
Installation and Updates from
http://jbehave.org/reference/eclipse/updates/
18
Demo Time!
Test Driven Development
Example
Narrative: In order to make some rapid calculation As a user I want to make some additions Scenario: basic Addition Given I takes the first value 1 And I takes the second value 2 When I add the two values 1 and 2 Then I have 3
20
TDD, ATDD & BDD
If YOURE GOING TO DRIVE DESIGN DO IT IN STYLE