behavior driven development

39
Behaviour Driven Development Agile Mumbai 2008 Elizabeth Keogh Thoughtworks 1

Upload: naresh-jain

Post on 28-Oct-2014

5 views

Category:

Technology


1 download

DESCRIPTION

Liz Keogh\'s BDD presentation at Agile Mumbai 2008 Conf

TRANSCRIPT

Page 1: Behavior Driven Development

Behaviour Driven Development

Agile Mumbai 2008Elizabeth KeoghThoughtworks

1

Page 2: Behavior Driven Development

Mobile phonesoff please!

2

Page 3: Behavior Driven Development

TDD

3

Page 4: Behavior Driven Development

TDD

Where to start?

3

Page 5: Behavior Driven Development

TDD

Where to start?

What to test?

3

Page 6: Behavior Driven Development

TDD

Where to start?

What to test?

How to test?

3

Page 7: Behavior Driven Development

AgiledoxChris Stevenson

public class CustomerLookupTest extends TestCase { testFindsCustomerById() { … } testFailsForDuplicateCustomers() { … } …}

4

Page 8: Behavior Driven Development

AgiledoxChris Stevenson

public class CustomerLookupTest extends TestCase { testFindsCustomerById() { … } testFailsForDuplicateCustomers() { … } …} CustomerLookup- finds customer by id- fails for duplicate customers

4

Page 9: Behavior Driven Development

What do you do when a test fails?

5

Page 10: Behavior Driven Development

What do you do when a test fails?

• Fix the bug

5

Page 11: Behavior Driven Development

What do you do when a test fails?

• Fix the bug

• Delete the test

5

Page 12: Behavior Driven Development

What do you do when a test fails?

• Fix the bug

• Delete the test

• Move the test

5

Page 13: Behavior Driven Development

Should

CustomerLookup- should find customer by id- should fail for duplicate customers

6

Page 14: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

7

Page 15: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design

7

Page 16: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design TDD is about requirements

7

Page 17: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design TDD is about requirements Use ‘behaviour’ instead of test

7

Page 18: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design TDD is about requirements Use ‘behaviour’ instead of test

Describe the next most important thing it doesn’t do…

7

Page 19: Behavior Driven Development

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design TDD is about requirements Use ‘behaviour’ instead of test

Describe the next most important thing it doesn’t do… and should

7

Page 20: Behavior Driven Development

JBehave!public class CustomerLookupBehaviour { shouldFindCustomerById() { … } shouldFailForDuplicateCustomers() { … } …}

8

Page 21: Behavior Driven Development

Hey, Chris! Look what I did!

9

Page 22: Behavior Driven Development

But that’s just like analysis.

Hey, Chris! Look what I did!

9

Page 23: Behavior Driven Development

Narratives

• As a <role>• I want <a feature>• So that <I get a benefit>

As a bank card holderI want to withdraw cash from the ATMSo that I don’t have to wait till Monday

10

Page 24: Behavior Driven Development

Scenarios

• When <an event>• Then <an outcome>

11

Page 25: Behavior Driven Development

Scenarios

• When <an event>• Then <an outcome>

When I request £20Then I should get £20!

11

Page 26: Behavior Driven Development

Scenarios

• When <an event>• Then <an outcome>

Not if you don’t have £20.

When I request £20Then I should get £20!

11

Page 27: Behavior Driven Development

Scenarios

• Given <a context>• When <an event>• Then <an outcome>

12

Page 28: Behavior Driven Development

Scenarios

Given my account has enough moneyAnd my card is workingAnd the ATM has enough cash inWhen I request £20Then I should get £20

13

Page 29: Behavior Driven Development

JBehave!public class UserWithdrawsCash extends ScenarioDrivenStory {

public UserWithdrawsCash() { super(new Narrative( "Bank card holder", "to be able to withdraw cash from an ATM", "I don't have to wait till Monday )); }

public void specify() { addScenario(new HappyScenario()); addScenario(new HappyScenarioWithOverdraft()); addScenario(new OverdrawnWithoutPermission()); addScenario(new InLotsOfTrouble());

14

Page 30: Behavior Driven Development

JBehave!public class HappyScenarioWithOverdraft extends

MultiStepScenario {

public void specifySteps() { given(new AccountHasOverdraftPermission()); given(new UserHasNoMoney()); when(new UserRequestsCash()); then(new ATMShouldDispenseCash()); then(new ATMShouldReturnBankCardToCustomer()); then(new AccountBalanceShouldBeReduced()); }}

15

Page 31: Behavior Driven Development

Scenarios - where to start

• Know what ‘done’ looks like - and why!• Start from the outside - the UI - and

work in• Write scenarios which use and verify

the user interface• Reuse contexts, events and outcomes

16

Page 32: Behavior Driven Development

Behaviour - what to testExamples - how to test

• Describe behaviour using examples, starting with the UI

• Given, when, then• Find classes that can help the UI do its job• Mock out each class that doesn’t exist yet• Then describe the behaviour of the mocked

class, etc.• Until the scenario works• Then onto the next most important thing that

doesn’t work!

17

Page 33: Behavior Driven Development

BDD Vocabulary• Story / Narrative / Role / Feature / Benefit• Scenario / Context / Event / Outcome /

Given / When / Then• Outside-in• Examples• Behaviour• Responsibility / should / ensure that• Mocks• Ubiquitous Language (from DDD)

18

Page 34: Behavior Driven Development

Hey, Thoughtworks! Look what I did!

19

Page 35: Behavior Driven Development

Be nice if it worked…

Hey, Thoughtworks! Look what I did!

19

Page 36: Behavior Driven Development

Hey, Everybody! Look what I did!

20

Page 37: Behavior Driven Development

The RSpec team: Be nice if it was in Ruby…

Hey, Everybody! Look what I did!

20

Page 38: Behavior Driven Development

RSpec!Story: I can park a car in a lot

As a manager of parking lots I want to park a car in my lot

So that I can charge people for parking there

Scenario: the parking lot is empty

Given a parking lot with a capacity of 20 And the parking lot is empty

When I try to park a car Then it should tell me I have succeeded And the number of cars should be 1

Scenario: the parking lot is full

Given a parking lot with a capacity of 20 And the parking lot has 20 cars in it When I try to park a car

21

Page 39: Behavior Driven Development

With thanks to

• Dan North: http://dannorth.net • Chris Matts• The JBehave team: http://jbehave.org• The RSpec team: http://rspec.info • Thoughtworks: http://thoughtworks.com • Eric Evans: http://www.domainlanguage.com• and the BDD community

• You can find my BDD blog at: http://sirenian.livejournal.com/bdd

22