behavior driven development

Post on 28-Oct-2014

5 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

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

TRANSCRIPT

Behaviour Driven Development

Agile Mumbai 2008Elizabeth KeoghThoughtworks

1

Mobile phonesoff please!

2

TDD

3

TDD

Where to start?

3

TDD

Where to start?

What to test?

3

TDD

Where to start?

What to test?

How to test?

3

AgiledoxChris Stevenson

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

4

AgiledoxChris Stevenson

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

4

What do you do when a test fails?

5

What do you do when a test fails?

• Fix the bug

5

What do you do when a test fails?

• Fix the bug

• Delete the test

5

What do you do when a test fails?

• Fix the bug

• Delete the test

• Move the test

5

Should

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

6

‘Test’ Driven Development

Test what? There’s no code!

7

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design

7

‘Test’ Driven Development

Test what? There’s no code!

TDD is about design TDD is about requirements

7

‘Test’ Driven Development

Test what? There’s no code!

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

7

‘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

‘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

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

8

Hey, Chris! Look what I did!

9

But that’s just like analysis.

Hey, Chris! Look what I did!

9

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

Scenarios

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

11

Scenarios

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

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

11

Scenarios

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

Not if you don’t have £20.

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

11

Scenarios

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

12

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

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

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

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

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

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

Hey, Thoughtworks! Look what I did!

19

Be nice if it worked…

Hey, Thoughtworks! Look what I did!

19

Hey, Everybody! Look what I did!

20

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

Hey, Everybody! Look what I did!

20

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

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

top related