BDD

Behavior-Driven Development, BDD, is an Agile software development process that encourages collaboration among developers, QA and non-technical or business participants in a software project.

TOC

It encourages teams to use conversation and concrete examples to formalize a shared understanding of how the application should behave. It emerged from test-driven development (TDD). 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.

Similar process as TDD at the level of features, based on user needs. Use natural languages to create examples by structuring a bridge between “world of programming” and “natural language”. To make the examples executable: Scenarios (how the software behaves from the stand point of user).

Principles of BDD

Test-Driven Development (TDD) 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

Behavioral specifications

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. Each User Story should, in some way, follow the following structure:

  • Title
    • An explicit title.
  • Narrative
    • A short introductory section with the following structure:
      • As a: the person or role who will benefit from the feature
      • I want: the feature
      • so that: the benefit or value of the feature
  • Acceptance criteria
    • A description of each specific scenario of the narrative with the following structure:
      • Given: the initial context at the beginning of the scenario, in one or more clauses
      • When: the event that triggers the scenario
      • Then: the expected outcome, in one or more clauses

Example:

Title: Returns and exchanges go to inventory.

As a store owner, I want to add items back to inventory when they are returned or exchanged, so that I can track inventory.

Scenario 1: Items returned for refund should be added to inventory. Given that a customer previously bought a black sweater from me and I have three black sweaters in inventory, when they return the black sweater for a refund, then I should have four black sweaters in inventory.

Scenario 2: Exchanged items should be returned to inventory. Given that a customer previously bought a blue garment from me and I have two blue garments in inventory and three black garments in inventory, when they exchange the blue garment for a black garment, then I should have three blue garments in inventory and two black garments in inventory.

Resources

Java

JBehave

Tutorial, Github

Input document that JBehave reads. 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).

src
    main
    test
        java
            com
                quanpan302
                    bdd
                        "package-info.java"
                        "CellGame.java",   import net.serenitybdd.jbehave.SerenityStories;
                        "CellSteps.java",  import org.junit.Assert.*;
        resources
            com
                quanpan302
                    bdd
                        "cell_game.story"

Input file that JBehave reads.

// cell_game.story

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

Java script that JBehave executes.

// CellSteps.java
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));
}

Selenium

Team, Github

Selenium automates browsers.

Javascript

Mocha

Team, Github

var assert = require('assert');
describe('Array', function () {

  describe('#indexOf()', function () {
  
    it('should return -1 when the value is not present', function () {
      assert.equal([1, 2, 3].indexOf(4), -1);
    });
  });
});

Jasmine

Team, Github

describe("A suite is just a function", function() {
  var a;

  it("and so is a spec", function() {
    a = true;

    expect(a).toBe(true);
  });
});

Selenium, automates browsers

Team, Github

Javascript

Python

Java

Resources