CSC/ECE 517 Fall 2014/ch1a 25 rs
JBehave
Behavior Driven Development
Overview
Behavior driven design (BDD) exists as an extension to Test Driven Development(TDD). Overall, BDD can be thought of as a specialized form of TDD that utilizes specialized tools (such as JBehave) to accomplish similar tasks with more emphasis on the accessibility of the program tests as opposed to the rigid, structured format of conventional acceptance tests.
Comparison to Test Driven Development
The language used for test cases immediately distinguishes BDD from TDD. Also, in BDD, the test cases emcompass both the specification for the program as well as the actual test case.
Background
JBehave is a Java-based framework that aims to function as a fully fledged development paradigm, utilizing communication and automation while using behavior based language in its descriptors. JBehave is the first ever created Behavior Driven Design platform, completed by Dan North.
Concepts
Three Core Principles of BDD
- Business and Technology should refer to the same system in the same way - ItsAllBehaviour
- Any system should have an identified, verifiable value to the business - WheresTheBusinessValue
- Up-front analysis, design and planning all have a diminishing return - EnoughIsEnough
Stories
A story is fundamentally defined as an "automatically executable increment of business functionality." We can think of this as a single component of functionality. Stories are further comprised of scenarios, which are defined as concrete examples of the behavior of the system. In other words, a specific instance of an implementation of a story. Scenarios are further broken down into steps, which are single statements that detail (step by step, if you will) exactly how a scenario will play out.
Keywords
BDD relies on the use of a small vocabulary to minimize miscommunication and to ensure that everyone involved (business, developers, testers, analysts and managers) are not only on the same page but using the same words.
Given, When, and Then are keywords utilized when writing JBehave scenarios. A scenario can have any number of steps and a story can contain many scenarios.
Given details the premises of the test, When details the exact incident, and Then details the expected outcome. To re-emphasize, the english readability is a novelty of BDD concepts.
Example of general story format:
Narrative: In order to communicate effectively to the business some functionality As a development team I want to use Behaviour-Driven Development Scenario: A scenario is a collection of executable steps of different type Given step represents a precondition to an event When step represents the occurrence of the event Then step represents the outcome of the event Scenario: Another scenario exploring different combination of events Given a precondition When a negative event occurs Then a the outcome should be captured
Integrating with Eclipse
Using the Eclipse Installer:
- Help > Install New Software...
- Add the new site location http://jbehave.org/reference/eclipse/updates/
- Select JBehave Eclipse feature and follow standard Eclipse installation procedure
Examples
Given a 5 by 5 game When I toggle the cell at (3, 2) Then the grid should look like ..... ..... ..... ..X.. ..... When I toggle the cell at (3, 1) Then the grid should look like ..... ..... ..... ..X.. ..X.. When I toggle the cell at (3, 2) Then the grid should look like ..... ..... ..... ..... ..X..
This exhibits the simplicity of the language involved. Though it is a simple example, it is immediately readable and understandable even by those inexperienced in programming.
Specification: Stack When a new stack is created Then it is empty When an element is added to the stack Then that element is at the top of the stack When a stack has N elements And element E is on top of the stack Then a pop operation returns E And the new size of the stack is N-1
Similarly, this tests a very simple data structure that can be quickly understood by those not well versed in the exact underworkings of how a stack functions; they only need to know the most basic outline of a stack in order to find this Story completely readable.
And finally, a more specific stack testing implementation:
Narrative: In order to develop an application that requires a stack efficiently As a development team I would like to use an interface and implementation in Java directly Scenario: Basic functionality of a Stack Given an empty stack When the string Java is added And the string C++ is added And the last element is removed again Then the resulting element should be Java Scenario: Stack search Given an empty stack When the string Java is added And the string C++ is added And the string PHP is added And the element Java is searched for Then the position returned should be 3
References
1. http://jbehave.org/
2. http://en.wikipedia.org/wiki/Behavior-driven_development
3. http://www.ryangreenhall.com/articles/bdd-by-example.html
4. http://behaviour-driven.org/
5. https://blog.codecentric.de/en/2011/03/automated-acceptance-testing-using-jbehave/