Slideshow transcript
Slide 1: Behaviour Driven Development Agile Mumbai 2008 Elizabeth Keogh Thoughtworks 1
Slide 2: Mobile phones off please! 2
Slide 3: TDD 3
Slide 4: TDD Where to start? 3
Slide 5: TDD Where to start? What to test? 3
Slide 6: TDD Where to start? What to test? How to test? 3
Slide 7: Agiledox Chris Stevenson public class CustomerLookupTest extends TestCase { testFindsCustomerById() { … } testFailsForDuplicateCustomers() { … } … } 4
Slide 8: Agiledox Chris Stevenson public class CustomerLookupTest extends TestCase { testFindsCustomerById() { … } testFailsForDuplicateCustomers() { … } … } CustomerLookup - finds customer by id - fails for duplicate customers 4
Slide 9: What do you do when a test fails? 5
Slide 10: What do you do when a test fails? • Fix the bug 5
Slide 11: What do you do when a test fails? • Fix the bug • Delete the test 5
Slide 12: What do you do when a test fails? • Fix the bug • Delete the test • Move the test 5
Slide 13: Should CustomerLookup - should find customer by id - should fail for duplicate customers 6
Slide 14: ‘Test’ Driven Development Test what? There’s no code! 7
Slide 15: ‘Test’ Driven Development Test what? There’s no code! TDD is about design 7
Slide 16: ‘Test’ Driven Development Test what? There’s no code! TDD is about design TDD is about requirements 7
Slide 17: ‘Test’ Driven Development Test what? There’s no code! TDD is about design TDD is about requirements Use ‘behaviour’ instead of test 7
Slide 18: ‘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
Slide 19: ‘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
Slide 20: JBehave! public class CustomerLookupBehaviour { shouldFindCustomerById() { … } shouldFailForDuplicateCustomers() { … } … } 8
Slide 21: Hey, Chris! Look what I did! 9
Slide 22: Hey, Chris! Look what I did! But that’s just like analysis. 9
Slide 23: Narratives • As a <role> • I want <a feature> • So that <I get a benefit> As a bank card holder I want to withdraw cash from the ATM So that I don’t have to wait till Monday 10
Slide 24: Scenarios • When <an event> • Then <an outcome> 11
Slide 25: Scenarios • When <an event> • Then <an outcome> When I request £20 Then I should get £20! 11
Slide 26: Scenarios • When <an event> • Then <an outcome> When I request £20 Then I should get £20! Not if you don’t have £20. 11
Slide 27: Scenarios • Given <a context> • When <an event> • Then <an outcome> 12
Slide 28: Scenarios Given my account has enough money And my card is working And the ATM has enough cash in When I request £20 Then I should get £20 13
Slide 29: 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
Slide 30: 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
Slide 31: 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
Slide 32: Behaviour - what to test Examples - 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
Slide 33: 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
Slide 34: Hey, Thoughtworks! Look what I did! 19
Slide 35: Hey, Thoughtworks! Look what I did! Be nice if it worked… 19
Slide 36: Hey, Everybody! Look what I did! 20
Slide 37: Hey, Everybody! Look what I did! The RSpec team: Be nice if it was in Ruby… 20
Slide 38: 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
Slide 39: 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




Add a comment on Slide 1
If you have a SlideShare account, login to comment; else you can comment as a guest- Favorites & Groups
Showing 1-50 of 0 (more)