Behaviour Driven Development -...
Transcript of Behaviour Driven Development -...
![Page 1: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/1.jpg)
Behaviour Driven Development
Marc Jeanson
Redline Software Inc.
www.redlinesoftware.com
Twitter @marcjeanson
![Page 2: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/2.jpg)
Why Do SoftwareProjects Fail?
Deliver late or over budget (often both!)
Deliver the wrong thing
Unstable in production
Costly to maintain
![Page 3: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/3.jpg)
![Page 4: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/4.jpg)
FastFeedback
Loops
![Page 5: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/5.jpg)
TDD&
BDD
![Page 6: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/6.jpg)
Terminology
![Page 7: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/7.jpg)
Terminology OverloadUnit Tests
Integration Tests
Acceptance Tests
Regression Tests
White-box Tests
Black-box Tests
Functional Tests
Non-functional Tests
ATDD
ADHD
...etc
...etc...
![Page 8: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/8.jpg)
What is a Unit Test?
It’s a test on a single unit, such as a class or method, in isolation.
![Page 9: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/9.jpg)
Acceptance Tests
Ideally created by developers and stakeholders together
Tell us what the system needs to do in order for the stakeholders to find it acceptable
Much more useful than receiving a requirements document
![Page 10: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/10.jpg)
Comparison
Acceptance Tests ensure you build the right thing
Unit Tests ensure you build the thing right
![Page 11: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/11.jpg)
Refactoring without tests isn’t refactoring...
...it’s just changing stuff.
![Page 12: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/12.jpg)
Laws of TDDYou are not allowed to write any production code unless it is to make a failing unit test pass.
You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.
You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd
![Page 13: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/13.jpg)
Why TDD?
Avoid wasting time debugging
Improve code quality
Improve design
Regression tests as byproduct
Increased confidence
![Page 14: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/14.jpg)
Behaviour Driven Development
Think of the behaviour of the system instead of thinking of the tests
Driven by business value
Promotes “Outside-In Development”
YAGNI
BDD = TDD + automated acceptance testing
![Page 15: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/15.jpg)
BDD Cycle from The RSpec Book
![Page 16: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/16.jpg)
Gherkin
![Page 17: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/17.jpg)
GherkinLets you describe how the application should behave using a “business readable” DSL
Features (user stories) and scenarios are specified using Gherkin
Also doubles as documentation that stays “in-sync” with your codebase
Tools like Cucumber, SpecFlow, jBehave, etc use Gherkin to generate the test steps
![Page 18: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/18.jpg)
Gherkin Syntax
Feature: [User Story]
Scenario: Title
Given [Context]
When [Action]
Then [Expected Outcome]
![Page 19: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/19.jpg)
Gherkin SyntaxScenario: Title
Given [Context]
And [More Context]
When [Action]
And [More context]
Then [Expected Outcome]
But [Unexpected Outcome]
![Page 20: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/20.jpg)
CucumberDemo
![Page 21: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/21.jpg)
Not just for Rails!
Java with JRuby
.NET using IronRuby
Erlang, Python, PHP, Flex...
https://github.com/cucumber/cucumber/wiki/
![Page 22: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/22.jpg)
Alternatives
.NET: specflow.org
Java: jBehave.org
Javascript: Jasmine
Various: Fitnesse.org
![Page 23: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/23.jpg)
Legacy Apps
![Page 24: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/24.jpg)
How to get started?
Start by adding “black box” tests
➡ Using Cucumber or another acceptance testing framework
➡ Add them gradually, as you need them
Don’t add “white box” tests
![Page 25: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/25.jpg)
Specification vs
Characterization
![Page 26: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/26.jpg)
Specification Tests
These are the tests we’ve been talking about already
Ideally they are written before the production code
They specify the code we wish we had
![Page 27: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/27.jpg)
Characterization Tests
With legacy code, we want to add tests that describe the current behaviour of the application
These tests will be used to ensure that the correct behaviour is preserved in future changes
![Page 28: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/28.jpg)
Recipe1. Write a scenario that exercises some
behaviour of your system
2. Add an assertion that you know will fail
3. Let the failure tell you what the behaviour is
4. Change the test so that it expects the behaviour that the code is producing
5. Repeat
![Page 29: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/29.jpg)
Bug Fixing
Adding characterization tests to cover the entire app can take a long time
Starting with bug fixes can be a good approach
![Page 30: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/30.jpg)
Bug Fix Recipe
1. Translate the bug report into a scenario.
2. Run the scenario. It should fail the same way as your system currently is.
3. Investigate the busted code. Add additional characterization tests if needed.
4. Fix the code so the bug scenario passes.
5. Run all the tests to make sure you didn’t add any regressions.
![Page 31: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/31.jpg)
New Behaviour Recipe1. Add any necessary characterization tests in
the area that you will be changing
2. Add new scenarios to specify the new behaviour
3. Run the tests, something should fail. Examine the code to make it pass.
4. Add extra characterization tests to build up confidence to change the code if necessary
5. Add the code to make it pass
6. Repeat 3-6 until everything passes
![Page 32: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/32.jpg)
Resources
Buy these books...I stole most of the content in this section from them
![Page 33: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/33.jpg)
Resources
http://destroyallsoftware.comhttp://cleancoders.com
![Page 34: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/34.jpg)
Questions?
![Page 35: Behaviour Driven Development - redlinesoftware.comredlinesoftware.com/assets/2012/01/15/intro_bdd_agile_wpg.pdf · 15/01/2012 · Behaviour Driven Development Think of the behaviour](https://reader033.fdocuments.in/reader033/viewer/2022050521/5fa46b3d9268962b587ffc38/html5/thumbnails/35.jpg)
Contact Info
Marc Jeanson
Redline Software Inc.
www.redlinesoftware.com
Twitter: @marcjeanson
http://github.com/marcjeanson