Behavior Driven Development: An Overview

38
Behavior Driven Development Manodnya Lele Ben Maynard

Transcript of Behavior Driven Development: An Overview

Page 1: Behavior Driven Development:  An Overview

Behavior Driven Development

Manodnya Lele

Ben Maynard

Page 2: Behavior Driven Development:  An Overview

About Litle & Co.

• Litle & Co. is a leading financial technology company

• One of the largest, private, non-bank proprietary processing platforms serving the Card-not-Presentmarketplace

• Specific expertise in Card-not-Present transactions, deep knowledge of Best Practices, Card Association Regulations, PCI and Data Security requirements

• Litle Vault is the 2011 Stevie Award Winner in New Product & Services (Web/IVR) category

Page 3: Behavior Driven Development:  An Overview

Outline

• What is BDD?

• History

• Why not TDD?

• The Story

• Tools: Compare and Contrast

• Comments and Questions

Page 4: Behavior Driven Development:  An Overview

What is BDD?

BDD can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as the requirement is specific enough that everyone knows what’s going on.

-Dan North

Page 5: Behavior Driven Development:  An Overview

What is BDD?

BDD can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as the requirement is specific enough that everyone knows what’s going on.

-Dan North

Page 6: Behavior Driven Development:  An Overview

What is BDD?

BDD can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as the requirement is specific enough that everyone knows what’s going on.

-Dan North

Page 7: Behavior Driven Development:  An Overview

What is BDD?

BDD can turn an idea for a requirement into implemented, tested, production-ready code simply and effectively, as long as the requirement is specific enough that everyone knows what’s going on.

-Dan North

Page 8: Behavior Driven Development:  An Overview

History

1999

Test First

Development

2003

Behavior Driven

Development

Page 9: Behavior Driven Development:  An Overview

History

1999

Test First

Development

2003

Behavior Driven

Development

Kent Beck

Page 10: Behavior Driven Development:  An Overview

History

Dan NorthKent Beck

Page 11: Behavior Driven Development:  An Overview

Why not TDD?

BAs / Program Managers Techies

Page 12: Behavior Driven Development:  An Overview

Why not TDD?

BAs / Program Managers

• Requirements docs not technical enough

Techies

Page 13: Behavior Driven Development:  An Overview

Why not TDD?

BAs / Program Managers

• Requirements docs not technical enough

Techies

• Test cases and unit tests too technical

Page 14: Behavior Driven Development:  An Overview

Why not TDD?

Page 15: Behavior Driven Development:  An Overview

Why not TDD?

BDDGiven… When… Then…

Page 16: Behavior Driven Development:  An Overview

Why not TDD?

TDD

BDD

Page 17: Behavior Driven Development:  An Overview

Why not TDD?

TDDBuilding the thing right

BDD

Page 18: Behavior Driven Development:  An Overview

Why not TDD?

TDDBuilding the thing right

BDDBuilding the right thing

Page 19: Behavior Driven Development:  An Overview

BDD:The Story

Page 20: Behavior Driven Development:  An Overview

BDD: The Story

• A way to describe the requirement such that everyone

the business folks,

the analyst,

the developer,

the tester,

and others

has a common understanding of the scope of the work.

• A description of a requirement and its business benefit, and a set of criteria by which we all agree that it is “done”.

• Remember: Given… When… Then…

Page 21: Behavior Driven Development:  An Overview

BDD: The Story

• The title should describe an activity

Describes actual behavior by a user of the system.

• The narrative should include a role, a feature and a benefit

“As a [role] I want [feature] so that [benefit]“

• The scenario should be described in terms of givens, eventsand outcomes

Given… When… Then…

Page 22: Behavior Driven Development:  An Overview

BDD: The Story

Scenario 1: Smooth Checkout Process

Given our world class web sales app

And items in my shopping cart

When a user clicks “checkout”

Then the user will move through checkout

with a maximum of two page clicks

And maintain an https session with our site

Scenario 2: Better Site Uptime

Given that a potential buyer

When a user enters our URL into a browser

Then the site will be available over 99.97%

of the time

TitleEasier user experience with online site

NarrativeAs a(n) active, participating member of my companyI want a better online websiteSo that users can easily use and purchase on our website

Page 23: Behavior Driven Development:  An Overview

BDD Tool Overview

Page 24: Behavior Driven Development:  An Overview

Motivating Example

FizzBuzz

• Simple program that outputs a number and expects the player to guess which word applies:

– Divisible by 3: “Fizz”

– Divisible by 5: “Buzz”

– Divisible by 3 and 5: “FizzBuzz”

Page 25: Behavior Driven Development:  An Overview

jbehave

http://jbehave.org

Page 26: Behavior Driven Development:  An Overview

How to write a BDD test in jbehave

• Write the story in a file with a <story_name>.story name

• Create the implementation in Java in a class named <StoryName>Steps.java

• Create an Runner, extending JUnitStory to link the story to the implementation

• Runner also can specify other details such as output formats

Page 27: Behavior Driven Development:  An Overview

jbehave Story

• Story: Play fizz-buzzAs a math game player

I would like to play fizz-buzz

So that I can learn how to calculate multiples

Scenario: When to fizz

Given a fizz-buzz player

When I ask to fizz-buzz for 3

Then I should get a fizz

Scenario: When to fizz-buzz

Given a fizz-buzz player

When I ask to fizz-buzz for <value>

Then I should get a <display>

• Examples:

|value|display|

|1|1|

|2|2|

|3|fizz|

|4|4|

|5|buzz|

|6|fizz|

|7|7|

|8|8|

|9|fizz|

|10|buzz|

|15|fizz-buzz|

|30|fizz-buzz|

Page 28: Behavior Driven Development:  An Overview

jbehave Test Code

public class PlayFizzBuzzSteps

{

private FizzBuzzer fb;

private String answer;

@Given("a fizz-buzz player")

public void givenAFizzBuzzPlayer()

{ fb = new FizzBuzzer(3, 5);

}

@When("I ask to fizz-buzz for <value>")

@Alias("I ask to fizz-buzz for $value")

public void answer(@Named("value") final int value)

{ answer = fb.answer(value);

}

@Then("I should get a <display>")

@Alias("I should get a $display")

public void isBuzz(@Named("display") final String display)

{

assertThat(answer, is(display));

}

Page 29: Behavior Driven Development:  An Overview

jbehave Runner

public class PlayFizzBuzz extends JUnitStory {

public PlayFizzBuzz()

{

addSteps(new InstanceStepsFactory(configuration(), new PlayFizzBuzzSteps())

.createCandidateSteps());

}

@Override

public Configuration configuration()

{

return super.configuration()

.useStoryReporterBuilder(new StoryReporterBuilder().withFormats(Format.CONSOLE,

Format.TXT,

Format.HTML));

}

}

Page 30: Behavior Driven Development:  An Overview

easybhttp://easyb.org

Page 31: Behavior Driven Development:  An Overview

FizzBuzz with easyb

narrative "while playing fizz-buzz", {

as_a "fizz-buzz player"

i_want "to automate my responses"

so_that "i always win"

}

scenario "fizzing", {

given "a fizz-buzz player", {

fb = new sf.projects.fizzbuzz.FizzBuzzer(3, 5)

}

when "3", {

display = fb.answer(3);

}

then "should fizz", { display.shouldBe "fizz" }

}

Page 32: Behavior Driven Development:  An Overview

Cucumberhttp://cukes.info

Page 33: Behavior Driven Development:  An Overview

Cucumber Feature

Feature: Play Fizz Buzz

As a math game player

I would like to play fizz-buzz

So that I can learn how to calculate multiples

Scenario: When to fizz

Given a fizz-buzz player

When I ask to fizz-buzz for 3

Then I should get a fizz

Page 34: Behavior Driven Development:  An Overview

Cucumber Test Code

Given /a fizz-buzz player/ do |n|

@fb = FizzerBuzzer.new

end

When /I ask to fizz-buzz for 3/ do |op|

@result = @fb.answer op

end

Then /I should get a fizz/ do |result|

@result.should == result.to_f

end

Page 35: Behavior Driven Development:  An Overview

Compare and Contrast

Criteria jbehave easyb Cucumber

Language written in

Java Groovy Ruby

Languages supported

Any language on the JVM

Any language on the JVM

Ruby or (with Cuke4Duke) Java

Writing the story and test

Separate files Single file Separate files

Running tests Commandline, Ant, Maven, IDEs like Eclipse, JUnit

Commandline, Ant, Maven, IDEs like Eclipse, JUnit

Commandline or (with Cuke4Duke) Ant, Maven, IDEs like Eclipse

Page 36: Behavior Driven Development:  An Overview

Criteria jbehave easyb Cucumber

Pending tests feature

Supported Supported Supported

Running multiple tests

Supported Supported Supported

Assertion feature Can use Hamcrestmatchers for assertion

Uses ‘ensure’ syntax, similar to assert

Uses Ruby framework

Installation Easy Easy Fair

Documentation support

Good Extremely good No documentation but good community support

Year of inception 2003 2009 2008

Author Dan North Andy Glover Aslak Hellesøy

Page 37: Behavior Driven Development:  An Overview

Summary

Given that you have heard this presentation

And discovered that BDD is really fun

When you are thinking about testing strategies

Then you should give BDD a chance!

• Questions /Comments are welcome….

• Contacts

– Ben Maynard: [email protected]

– Manodnya Lele: [email protected]

Page 38: Behavior Driven Development:  An Overview

Materials

Ready-made BDD project (Sualeh Fatehi)

http://code.google.com/p/test-fizzbuzz/