Jump to content

Behavior-driven development

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by 73.31.155.68 (talk) at 17:17, 26 November 2016 (Removed superfluous wording; actual. Simplified.). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In software engineering, behavior-driven development (BDD) is a software development process that emerged from test-driven development (TDD).[1][2][3][vague] Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software development and management teams with shared tools and a shared process to collaborate on software development.[1][4]

Although BDD is principally an idea about how software development should be managed by both business interests and technical insight, the practice of BDD does assume the use of specialized software tools to support the development process.[2] Although these tools are often developed specifically for use in BDD projects, they can be seen as specialized forms of the tooling that supports test-driven development. The tools serve to add automation to the ubiquitous language that is a central theme of BDD.

BDD is largely facilitated through the use of a simple domain-specific language (DSL) using natural language constructs (e.g., English-like sentences) that can express the behavior and the expected outcomes. Test scripts have long been a popular application of DSLs with varying degrees of sophistication. BDD is considered an effective technical practice especially when the "problem space" of the business problem to solve is complex.[5]

History

Behavior-driven development is an extension of test-driven development:[1] development that makes use of a simple, domain-specific scripting language. These DSLs convert structured natural language statements into executable tests. The result is a closer relationship to acceptance criteria for a given function and the tests used to validate that functionality. As such it is a natural extension of TDD testing in general.

BDD focuses on:

  • Where to start in the process
  • What to test and what not to test
  • How much to test in one go
  • What to call the tests
  • How to understand why a test fails

At the heart of BDD is a rethinking of the approach to the unit testing and acceptance testing that naturally arise with these issues. For example, BDD suggests that unit test names be whole sentences starting with a conditional verb ("should" in English for example) 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] .[1]

Starting from this point, many people developed BDD frameworks over a period of years, finally framing it as a communication and collaboration framework for developers, QA and non-technical or business participants in a software project.[6][7] During the "Agile specifications, BDD and Testing eXchange" in November 2009 in London, Dan North[8] gave the following description of BDD:

BDD is a second-generation, outside-in, pull-based, multiple-stakeholder, multiple-scale, high-automation, agile methodology. It describes a cycle of interactions with well-defined outputs, resulting in the delivery of working, tested software that matters.

Dan North created a BDD framework, JBehave,[1] followed by a story-level BDD framework for Ruby called RBehave[9] which was later integrated into the RSpec project.[10] He also worked with David Chelimsky, Aslak Hellesøy and others to develop RSpec and also to write "The RSpec Book: Behaviour Driven Development with RSpec, Cucumber, and Friends". The first story-based framework in RSpec was later replaced by Cucumber mainly developed by Aslak Hellesøy. Capybara (software), which is a part of the Cucumber testing framework is one such web-based test automation software.

Principles of BDD

Test-driven development is a software development methodology which essentially states that for each unit of software, a software developer must:

  • define a test set for the unit first;
  • make the tests fail;
  • then implement the unit;
  • finally verify that the implementation of the unit makes the tests succeed.

This definition is rather non-specific in that it allows tests in terms of high-level software requirements, low-level technical details or anything in between. One way of looking at BDD therefore, is that it is a continued development of TDD which makes more specific choices than TDD.

Behavior-driven development specifies that tests of any unit of software should be specified in terms of the desired behavior of the unit.[2][4][11] Borrowing from agile software development the "desired behavior" in this case consists of the requirements set by the business — that is, the desired behavior that has business value for whatever entity commissioned the software unit under construction.[2][11] Within BDD practice, this is referred to as BDD being an "outside-in" activity.[12]

Behavioral specifications

Following this fundamental choice, a second choice made by BDD relates to how the desired behavior should be specified. In this area BDD chooses to use a semi-formal format for behavioral specification which is borrowed from user story specifications from the field of object-oriented analysis and design. The scenario aspect of this format may be regarded as an application of Hoare logic to behavioral specification of software units using the Domain Language of the situation.

BDD specifies that business analysts and developers should collaborate in this area and should specify behavior in terms of user stories, which are each explicitly written down in a dedicated document.[11][12] Each user story should, in some way, follow the following structure:[2][12]

Title: The story should have a clear, explicit title.
Narrative
A short, introductory section that specifies
  • who (which business or project role) is the driver or primary stakeholder of the story (the actor who derives business benefit from the story)
  • what effect the stakeholder wants the story to have
  • what business value the stakeholder will derive from this effect
Acceptance criteria or scenarios
a description of each specific case of the narrative. Such a scenario has the following structure:
  • It starts by specifying the initial condition that is assumed to be true at the beginning of the scenario. This may consist of a single clause, or several.
  • It then states which event triggers the start of the scenario.
  • Finally, it states the expected outcome, in one or more clauses.

BDD does not have any formal requirements for exactly how these user stories must be written down, but it does insist that each team using BDD come up with a simple, standardized format for writing down the user stories which includes the elements listed above.[2][12] However, in 2007 Dan North suggested a template for a textual format which has found wide following in different BDD software tools.[12] A very brief example of this format might look like this:

Story: Returns go to stock

As a store owner
In order to keep track of stock
I want to add items back to stock when they're returned.

Scenario 1: Refunded items should be returned to stock
Given that a customer previously bought a black sweater from me
And I have three black sweaters in stock.
When he returns the black sweater for a refund
Then I should have four black sweaters in stock.

Scenario 2: Replaced items should be returned to stock
Given that a customer previously bought a blue garment from me
And I have two blue garments in stock
And three black garments in stock.
When he returns the blue garment for a replacement in black
Then I should have three blue garments in stock
And two black garments in stock.

The scenarios are ideally phrased declaratively rather than imperatively — in the business language, with no reference to elements of the UI through which the interactions take place.[13]

This format is referred to as the Gherkin language, which has a syntax similar to the above example. The term Gherkin, however, is specific to the Cucumber, JBehave and Behat software tools.[14][15][16]

Specification as a ubiquitous language

Behavior-driven development borrows the concept of the ubiquitous language from domain driven design.[2][4] A ubiquitous language is a (semi-)formal language that is shared by all members of a software development team — both software developers and non-technical personnel.[17] The language in question is both used and developed by all team members as a common means of discussing the domain of the software in question.[17] In this way BDD becomes a vehicle for communication between all the different roles in a software project.[2][7]

A common risk with software development includes communication breakdowns between Developers and Business Stakeholders.[18] BDD uses the specification of desired behavior as a ubiquitous language for the project team members. This is the reason that BDD insists on a semi-formal language for behavioral specification: some formality is a requirement for being a ubiquitous language.[2] In addition, having such a ubiquitous language creates a domain model of specifications, so that specifications may be reasoned about formally.[19] This model is also the basis for the different BDD-supporting software tools that are available.

The example given above establishes a user story for a software system under development. This user story identifies a stakeholder, a business effect and a business value. It also describes several scenarios, each with a precondition, trigger and expected outcome. Each of these parts is exactly identified by the more formal part of the language (the term Given might be considered a keyword, for example) and may therefore be processed in some way by a tool that understands the formal parts of the ubiquitous language.

Most BDD applications use text-based DSLs and specification approaches. However, graphical modeling of integration scenarios has also been applied sucessfully in practice, e.g., for testing purposes. [20]

Specialized tooling support

Much like test-driven design practice, behavior-driven development assumes the use of specialized support tooling in a project. Inasmuch as BDD is, in many respects, a more specific version of TDD, the tooling for BDD is similar to that for TDD, but makes more demands on the developer than basic TDD tooling.[clarification needed][citation needed]

Tooling principles

In principle a BDD support tool is a testing framework for software, much like the tools that support TDD. However, where TDD tools tend to be quite free-format in what is allowed for specifying tests, BDD tools are linked to the definition of the ubiquitous language discussed earlier.

As discussed, the ubiquitous language allows business analysts to write down behavioral requirements in a way that will also be understood by developers. The principle of BDD support tooling is to make these same requirements documents directly executable as a collection of tests. If this cannot be achieved because of reasons related to the technical tool that enables the execution of the specifications, then either the style of writing the behavioral requirements must be altered or the tool must be changed.[21] The exact implementation of behavioral requirements varies per tool, but agile practice has come up with the following general process:

  • The tooling reads a specification document.
  • The tooling directly understands completely formal parts of the ubiquitous language (such as the Given keyword in the example above). Based on this, the tool breaks each scenario up into meaningful clauses.
  • Each individual clause in a scenario is transformed into some sort of parameter for a test for the user story. This part requires project-specific work by the software developers.
  • The framework then executes the test for each scenario, with the parameters from that scenario.

Dan North has developed a number of frameworks that support BDD (including JBehave and RBehave), whose operation is based on the template that he suggested for recording user stories.[2] These tools use a textual description for use cases and several other tools (such as CBehave) have followed suit. However, this format is not required and so there are other tools that use other formats as well. For example, Fitnesse (which is built around decision tables), has also been used to roll out BDD.[22]

Tooling examples

There are several different examples of BDD software tools in use in projects today, for different platforms and programming languages.

Possibly the most well-known is JBehave, which was developed by Dan North. The following is an example taken from that project:[15]

Consider an implementation of the Game of Life. A domain expert (or business analyst) might want to specify what should happen when someone is setting up a starting configuration of the game grid. To do this, he might want to give an example of a number of steps taken by a person who is toggling cells. Skipping over the narrative part, he might do this by writing up the following scenario into a plain text document (which is the type of input document that JBehave reads):

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..

The bold print is not part of the input; it is included here to show which words are recognized as formal language. JBehave recognizes the terms Given (as a precondition which defines the start of a scenario), When (as an event trigger) and Then (as a postcondition which must be verified as the outcome of the action that follows the trigger). Based on this, JBehave is capable of reading the text file containing the scenario and parsing it into clauses (a set-up clause and then three event triggers with verifiable conditions). JBehave then takes these clauses and passes them on to code that is capable of setting a test, responding to the event triggers and verifying the outcome. This code must be written by the developers in the project team (in Java, because that is the platform JBehave is based on). In this case, the code might look like this:

private Game game;
private StringRenderer renderer;

@Given("a $width by $height game")
public void theGameIsRunning(int width, int height) {
    game = new Game(width, height);
    renderer = new StringRenderer();
    game.setObserver(renderer);
}
    
@When("I toggle the cell at ($column, $row)")
public void iToggleTheCellAt(int column, int row) {
    game.toggleCellAt(column, row);
}

@Then("the grid should look like $grid")
public void theGridShouldLookLike(String grid) {
    assertThat(renderer.asString(), equalTo(grid));
}

The code has a method for every type of clause in a scenario. JBehave will identify which method goes with which clause through the use of annotations and will call each method in order while running through the scenario. The text in each clause in the scenario is expected to match the template text given in the code for that clause (for example, a Given in a scenario is expected to be followed by a clause of the form "a X by Y game"). JBehave supports the matching of clauses to templates and has built-in support for picking terms out of the template and passing them to methods in the test code as parameters. The test code provides an implementation for each clause type in a scenario which interacts with the code that is being tested and performs a test based on the scenario. In this case:

  • The theGameIsRunning method reacts to a Given clause by setting up the initial game grid.
  • The iToggleTheCellAt method reacts to a When clause by firing off the toggle event described in the clause.
  • The theGridShouldLookLike method reacts to a Then clause by comparing the state of the game grid to the expected state from the scenario.

The primary function of this code is to be a bridge between a text file with a story and the code being tested. Note that the test code has access to the code being tested (in this case an instance of Game) and is very simple in nature. The test code has to be simple, otherwise a developer would end up having to write tests for his tests.

Finally, in order to run the tests, JBehave requires some plumbing code that identifies the text files which contain scenarios and which inject dependencies (like instances of Game) into the test code. This plumbing code is not illustrated here, since it is a technical requirement of JBehave and does not relate directly to the principle of BDD-style testing.

Story versus specification

A separate subcategory of behavior-driven development is formed by tools that use specifications as an input language rather than user stories. An example of this style is the RSpec tool that was also developed by Dan North. Specification tools don't use user stories as an input format for test scenarios but rather use functional specifications for units that are being tested. These specifications often have a more technical nature than user stories and are usually less convenient for communication with business personnel than are user stories.[2][23] An example of a specification for a stack might look like this:

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

Such a specification may exactly specify the behavior of the component being tested, but is less meaningful to a business user. As a result, specification-based testing is seen in BDD practice as a complement to story-based testing and operates at a lower level. Specification testing is often seen as a replacement for free-format unit testing.[23]

Specification testing tools like RSpec and JDave are somewhat different in nature from tools like JBehave. Since they are seen as alternatives to basic unit testing tools like JUnit, these tools tend to favor forgoing the separation of story and testing code and prefer embedding the specification directly in the test code instead. For example, an RSpec test for a hashtable might look like this:[24]

describe Hash do
  let(:hash) { Hash[:hello, 'world'] }

  it { expect(Hash.new).to eq({}) }

  it "hashes the correct information in a key" do
    expect(hash[:hello]).to eq('world')
  end

  it 'includes key' do
    hash.keys.include?(:hello).should be true
  end
end

This example shows a specification in readable language embedded in executable code. In this case a choice of the tool is to formalize the specification language into the language of the test code by adding methods named it and should. Also there is the concept of a specification precondition – the before section establishes the preconditions that the specification is based on.

The result of test will be:

 Hash
   should eq {}
   includes key
   hashes the correct information in a key

See also

References

  1. ^ a b c d e "Behaviour-Driven Development". Retrieved 12 August 2012.
  2. ^ a b c d e f g h i j k Haring, Ronald (February 2011). de Ruiter, Robert (ed.). "Behavior Driven development: Beter dan Test Driven Development". Java Magazine (in Dutch) (1). Veen Magazines: 14–17. ISSN 1571-6236.
  3. ^ Solis, Carlos; Wang, Xiaofeng. "A Study of the Characteristics of Behaviour Driven Development". Software Engineering and Advanced Applications (SEAA), 2011 37th EUROMICRO Conference on: 383–387. doi:10.1109/SEAA.2011.76.
  4. ^ a b c Bellware, Scott (June 2008). "Behavior-Driven Development". Code Magazine. Retrieved 12 August 2012.
  5. ^ Tharayil, Ranjith (15 February 2016). "When to embrace Behaviour Driven Development (BDD)?". SolutionsIQ. Retrieved 15 February 2016.
  6. ^ The RSpec Book – Question about Chapter 11: Writing software that matters
  7. ^ a b North, Dan (31 May 2012). "BDD is like TDD if…". faster organisations, faster software. Dan North & Associates. Retrieved 12 August 2012.
  8. ^ Dan North: How to sell BDD to the business
  9. ^ D.North, Introducing RBehave
  10. ^ S.Miller, InfoQ: RSpec incorporates RBehave
  11. ^ a b c North, Dan (March 2006). "Introducing BDD". Dan North. Retrieved 12 August 2012.
  12. ^ a b c d e North, Dan (11 February 2007). "What's in a Story?". Dan North. Retrieved 12 August 2012.
  13. ^ Mabey, Ben. "Imperative vs. Declarative Scenarios in user stories". Retrieved 19 May 2008.
  14. ^ "Gherkin". Retrieved 12 August 2012.
  15. ^ a b "What is JBehave?". JBehave.org. Retrieved 20 October 2015.
  16. ^ "Writing Features - Behat 3.0.12 documentation". behat documentation. Archived from the original on 19 September 2015. Retrieved 20 October 2015. {{cite web}}: Unknown parameter |deadurl= ignored (|url-status= suggested) (help)
  17. ^ a b Evans, Eric (2003). Domain-Driven Design: Tackling Complexity in the Heart of Software. Addison-Wesley. ISBN 0-321-12521-5. Retrieved August 12, 2012.
  18. ^ Geneca (16 Mar 2011). "Why Software Projects Fail". Retrieved 16 March 2011.
  19. ^ Mahmudul Haque Azad (6 Feb 2011). "Say Hello To Behavior Driven Development". Retrieved 12 August 2012.
  20. ^ Lübke, Daniel and van Lessen, Tammo (2016). "Modeling Test Cases in BPMN for Behavior-Driven Development". IEEE Software. 33 (5): 15–21. doi:10.1109/MS.2016.117.{{cite journal}}: CS1 maint: multiple names: authors list (link)
  21. ^ Adam Craven (September 21, 2015). "Fundamentals of Enterprise-Scale Behaviour-Driven Development (BDD)". Retrieved 14 January 2016.
  22. ^ Ketil Jensen (December 13, 2009). "BDD with Scenario tables in Fitnesse Slim". Walk the walk. Wordpress. Retrieved 12 August 2012.
  23. ^ a b Roy Osherove (October 4, 2008). "BDD: Behavior vs. Spec Frameworks". Retrieved 12 August 2012.
  24. ^ Jason Seifer (7 December 2011). "An Introduction To RSpec". Retrieved 27 October 2012.