behaviour driven development n.
Skip this Video
Download Presentation
Behaviour Driven Development

Loading in 2 Seconds...

play fullscreen
1 / 37

Behaviour Driven Development - PowerPoint PPT Presentation

  • Uploaded on

Behaviour Driven Development. With NBehave Linking User Stories and Domain Driven Design to Testing. David Ross W: E: Behavior Driven Development Tools and techniques. Talk overview What is BDD and how to use NBehave Topics

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Behaviour Driven Development' - murphy-rice

Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
behaviour driven development

Behaviour Driven Development

With NBehave

Linking User Stories and Domain Driven Design to Testing

David Ross



behavior driven development tools and techniques
Behavior Driven DevelopmentTools and techniques

Talk overview

  • What is BDD and how to use NBehave


  • What are high quality requirements?
  • Top down and Bottom up Testing
  • NBehave
    • Using library
    • Using the test runner
  • Top down design with Rhino.Mocks and Nbehave
  • Acceptance testing
behavior driven development analysis design methodology by dan north
Behavior Driven DevelopmentAnalysis/Design Methodology by Dan North

Response to:

  • confusion about Test Driven Development and Test First Development
  • Lack of rigor around User Stories (Requirements)
  • Projects that generate documentation out of Unit tests

Culminated in:

  • the BDD method for writing User Stories
  • JBehave unit testing framework
behavior driven development
Behavior Driven Development

BDD is not:

  • a testing framework or style of testing
  • a method for writing tests

BDD is:

  • A formalised template for User Stories
  • Able to create testable User Stories since they are defined in a formal manner
  • Promotes “security” to a first call citizen in the analysis process

Simple Trading application

  • Excel spreadsheet replacement

Simple Trading application


  • Allow Portfolio Manager to decide the percentage split between Cash, Property and Stock within the Portfolio
  • Inform the Trader when the Portfolio needs to be rebalanced (ie split in the pie has changed significantly)
  • Allow Operations to standardise Portfolio names
test first development
Small steps

Red - Analysis/Design Hat

Create a test that defines the expected outcome

Asking "What I want is...“

Green - Creative Hat (Fun part)

Experiment/Prototype/Build until the test passes

Refactor- Engineering

Refactor, refactor, refactor

Test First Development
  • Red
  • Green
  • Refactor
test first development how easy is it to use tfd for the following
Test First DevelopmentHow easy is it to use TFD for the following


Value = Units * Market Price

Increase the performance of the User Security module by automatically caching the user’s manager Id

The system must email the Portfolio manager when the Portfolio needs to be rebalanced

Build a web site so that customers can see the performance of the portfolio

test first development do you feel guilty
Test First DevelopmentDo you feel guilty??

Do you write a Failing unit test before every piece of code?

Do you write a line of Unit Test code for EVERY line of production code?

behavior driven
Behavior Driven

How do you write a test when you don’t know where you are going?

Maybe we need requirements that are easy to write tests for?

requirements what are they
RequirementsWhat are they

Describes what a system should do but not how it will be done

Contract between the implementation team and the analysis team

Audience is typically the stake holders and not the development team

Precise and clear over verbose

We want requirements that

Can be tested!!!

Are attainable!!! 

Are traceable into code!!!

requirements gathering bdd merges two different approaches
Requirements GatheringBDD merges two different approaches

User Stories

Short one or two line statement that maps to a few days of development work

Highly focused

Domain Driven Design

Ubiquitous language – Ensure business terminology permeates into code

Clear single definition (within the project) for each business concept - Account has a single meaning


BDD User StoriesFormal template for User Stories

Story Template

As a [User/Role]I want [Behaviour]so that [I receive benefit]


As a “Operations staff member”I want “To create a Portfolio”so that “So that it can be traded”

bdd user stories formal template for user stories
BDD User StoriesFormal template for User Stories

Scenario Template

Given some initial context (the givens),When an event occurs,then ensure some outcomes.


Given “New portfolio is called Balanced Fund”When “Portfolio already exists”then “Portfolio can not be created”



What is it?

  • .NET Library for writing automated BDD style tests
  • Fluent syntax for building up a BDD Style User Stories
  • Test runner that executes the stories and generates a report listing the contents of the stories
  • BSD License
creating a bdd theme
Creating a BDD Theme

Collection of related stories

[Theme("Portfolio Maintenance")]

public class PortfolioMaintenanceStory



public void portfolio_create_story () { ... }


public void portfolio_delete_story() { ... }



creating a story
Creating a Story

Fluent interface to define the story

var story = new Story("Create portfolio");

story.AsA(“Operations team member")

.IWant("To create a new portfolio on behalf of a portfolio manager")

.SoThat("the portfolio manager can configure the portfolio and the front office can trade");


Creating a Scenario

Fluent interface to define the story

story.WithScenario("portfolio does not exist")

.Given("portfolio name is $name", "Aggressive Fund")

.When("portfolio does not exist in the database")

.Then("new portfolio should be created");

story.WithScenario("portfolio already exists")

.Given("portfolio name is $name", "Aggressive Fund")

.When("portfolio already exists in database")

.Then("an error should occur");


Executing the test

WithNBehave runner or mbUnit

  • Ouch we get an error???
  • The scenario throws an exception – NBehave isn’t validating any code
  • Use Pending

story.WithScenario("portfolio does not exist")

.Pending("In progress")

.Given("portfolio name is $name", "Aggressive Fund")

.When("portfolio does not exist in the database")

.Then("new portfolio should be created");

behavior driven development1
Across the Project (Story) Life Cycle


Pending Scenario

Stubbed Behavior Cycle (Red, Green, Refactor)

Real Behavior Cycle (Red, Green, Refactor)

Acceptance Test

Behavior Driven Development

Stubbing out the behavior

WithNBehave and Rhino.Mocks

Steps to generate an API

  • Go through the User story and find all the domain objects
  • Create a class for each domain object but don’t add state
  • Go through the User story and create a find all the services that are required (Repositories etc)
  • Create an interface for each service but don’t add any methods

Stubbing out the behavior

WithNBehave and Mocking

Steps to generate the API continued...

  • Slowly implement the story
  • Add domain object properties as required
  • Add methods signatures as required
  • Use Stubs to explore Inputs/Outputs to the methods
stubbing example
Stubbing example

Create a new portfolio story

stringportfolioName = "";

Portfolio p = null;

varportfolioRepository = MockRepository.GenerateStub<IPortfolioRepository>();

varportfolioService = MockRepository.GenerateStub<IPortfolioService>();

s.WithScenario("portfolio already exists")

.Given("portfolio name is $name", "Aggressive Fund", n => {

portfolioName = n;

p = new Portfolio { Name = portfolioName };


Stub(x => x.FindPortfolioByName(portfolioName)).Return(p);


Stub(X => X.CreatePortfolio(p)).Throw(new ItemExistsException());


stubbing example1
Stubbing example

Create a new portfolio story

.When("portfolio already exists in database",

() => Assert.IsNotNull(



.Then("new portfolio create should fail",

() => Assert.Throws<ItemExistsException>(() => portfolioService.CreatePortfolio(p))


tfd bdd comparison
Test First

Bottom Up approach

Lots of little steps working towards a solution



Behavior Driven

Top down approach

Design starting with interfaces (using stubs)

Back fill with code later


API creation

TFD/BDD Comparison
test driven development
Development/Testing PhasesTest Driven Development


Domain Model

User Stories

Behaviour Driven Development

GUI Testing


User Acceptance


Unit & Component Test


Implemented Behaviour


Integration Test

No Mocks


Deployment Verification Test

user acceptance test
User Acceptance Test


  • test cases are compiled
  • Must be created by developers or technical testers
  • Are created at the start of the project


  • User Acceptance tests are Wiki based and so are dynamic
  • Can be created by end users and business analysts
  • Can be created at the start of the project
  • But are only “implemented” by the development late in the SDLC
nbehave s pecific features
NBehavespecific features

What else is in NBehave?

fixtures to specifications
Fixtures to Specifications

Loosing Assert from tests. Easier to read?

For BAs, yes...



Assert.Contains(s, “hello world”);

s.ShouldContains(“hello world”);

NBehave contains extension methods for NUnit, mbUnit, xUnit

fixtures to specifications1
Fixtures to Specifications

Is it necessary? Using Namespace Alias

[TestFixture] [Context]

[Test] [Specification]


public class When_using_tokenized_stories



public void should_replace_token_with_value()


Stories for BDD. Specifications for focused tests.

The word Test has become a dirty word...

stories to documentation
NBehave-Console.exe PebbleSteps.NBehave.Stories.dll /o stories.txt

Theme: Trade Compliance

Story: Pretrade compliance check

As a Trader

I want verify a trade against the portfolio before I call the broker

So that the portfolio does not move out of compliance

Scenario: trade will move portfolio out of compliance

Given portfolio with name Balanced Fund

And asset allocation pie contains segments Cash 0.4, Stock 0.3, Property 0.3

And allocation pie 0.05

And portfolio contains Stock IBM 1, Cash GBP 1, Property SW5 1Hb 1, Stock BHP 1

When market prices are IBM 1, GBP 1, SW5 1Hb 1, BHP 1

Then portfolio value is 4

And portfolio constituents are Stock totalValue:2 percentOfPie:0.5 lower:0.25 upper:0.35, Cash totalValue:1 percentOfPie:0.25 lower:0.35 upper:0.45, Property totalValue:1 percentOfPie:0.25 lower:0.25 upper:0.35,

And portfolio is balanced

Stories to documentation

AssemblyFile="NBehave.MSBuild.dll" TaskName="NBehave.MSBuild.NBehaveTask" />







still in beta
Still in Beta…

What’s left to do?

  • The parameter regular expression is on [a-z][A-Z]
    • $example0, $example1 – First parameter is printed twice is the story output
  • Gallio integration/replacement of current story runner.
  • NBehave small dev team...
  • Current story runner text output formatting is poor
    • HTML
    • or a XSLT for current XML output
  • Occasional crash in the parsing engine that links text to delegate parameters
giving it a go
Giving it a go

Adding NBehave to your team

Developer Driven

  • Story as Pseudo code
  • Peer programming – One person writes the scenario the other person removes Pending()and “makes it green”
  • Don’t use “specification” syntax
  • Consider not using NBehave story runner and host Stories within current Continuous Integration setup (Use NUnitTestFixture as opposed to Theme)
giving it a go1
Giving it a go

Adding NBehave to your team continued...

Scrum Master/PM driven

  • Ask for detailed requirements in BBD style
  • Embedded BDD Lifecycle (API->Stubs->Implementation->UAT) into estimates/deliverables
  • Use NBehave story output as a deliverable to analysis/test team – ie as part of the release notes
more information
More information

Code will be placed onto