executable user stories with rspec and bdd
DESCRIPTION
Are you writing code that never gets used? Are you struggling to make the code fit your requirements?Behaviour-Driven Development (BDD) is a fresh breathe of air in the agile community, and its primary focus is to deliver software that matters. -For the end users. BDD builds upon several agile tools and practices like Acceptance-Test Driven Planning, Example-Driven Devlopment, FIT, User Stories and TDD, and bring them together in a consistent whole.This presentation will give an introduction to the principles and practices of BDD and demonstrate how you can make your plain text User Stories executable with RSpec's Story Framework. The framework itself is written in Ruby, but it can run against production code written in any programming language.Watch a video at http://www.bestechvideos.com/2009/04/03/executable-user-stories-with-rspec-and-bddTRANSCRIPT
Behaviour-Driven DevelopmentWriting software that matters
Aslak Hellesøy - Chief Scientist - BEKK
2
Dual audience
Stakeholders
Programmers
Origin and inspiration
JBehaveDan NorthChris Matts
Test Driven Development
Kent Beck
FITWard
Cunningham
User StoriesMike Cohn
Example Driven DevelopmentBrian Marick
Chelimsky, Hellesøy, Takita, Astels, Baker
Connextra StoriesSeveral
Domain Driven Design
Eric Evans
Acceptance-Test Driven
Planning
4
Software development is all about delivering
business value
What’s being used
Source: CHAOS report
Where errors are introduced
Source: CHAOS report
8
BDD: Business value
10
Ubiqutous Language
BDD User Stories
Withdraw Cash
As an Account Holder
I want to withdraw cash in an ATM
So that I can get cash when the bank
is closed
Role
Operation
Business value1
Origin: Connextra
13
Challenge:Common understanding
of “Done”
BDD Scenarios
Business value
1
Given I have £200 in my accountWhen I ask to withdraw £20Then I should be given £20And my balance should be £180
ATDPAcceptance-Test Driven Planning
WriteScenarios
Product Backlog
IterationPlanning
Iteration N (end) Iteration N+1 (start)
TDD
Red
Green
Refactor
Make the
test pass
Write a
failing
test
Write a
passing test
1 2
3?
Developers don’t know...
• Where to start
• What to test
• What not to test
• How much to test in one go
• What to call tests
• How to fix a broken test
• That TDD is about design
Outside-In
2
3
4
5
1
0
10,000
20,000
30,000
40,000
June 2007 February 2008
Downloads
0
25
50
75
100
June 2007 February 2008
Contributors
0
10
20
30
40
June 2007 February 2008
RubyForge rank
0
828.25
1,656.50
2,484.75
3,313.00
June 2007 February 2008
SVN commits
JRuby << RSpec
RSpec consists of 2 parts
Story Framework
Example Framework
Story: Log in
As a Project contributor I want to log in So that I can see what projects I have access to Scenario: User provides wrong credentials Given I am on the login page When I log in with login nobody and password useless Then I should be informed that my password is incorrect And I should not see aslak.hellesoy on the page
Scenario: User provides correct credentials Given I am on the login page When I log in with login aslak.hellesoy and password temporary Then I should see aslak.hellesoy on the page And I should have the opportunity to log out
RSpec User Stories
require 'bowling'
describe Bowling do before(:each) do @bowling = Bowling.new end
it "should score 0 for gutter game" do 20.times { @bowling.hit(0) } @bowling.score.should == 0 endend
RSpec Examples
Outside-In
2
3
4
5
Story: Withdraw Money
As an account holder
I want to withdraw money
So that I can get cash when the bank is closed
Scenario: Overdraft account
Given an account with -1000 kr
When I ask for 200 kr
Given the balance should be -1000 kr
1
describe Bank do
it "should not be creatable with negative balance" do
lambda do
Bank.new(-9)
end.should(raise_error("Negative balance"))
end
end
Summary
• As a <Role>, I want <Operation> So that <Business value>
• Given <Known state>, When I <Action>, Then <Expected outcome>
• Work Outside-in in the layer onion
• Write scenarios and tests bottom up (start with outcome)
• Tests are sentences
References
• http://behaviour-driven.org/
• http://dannorth.net/introducing-bdd/
• http://dannorth.net/whats-in-a-story/
• http://rspec.info/
• http://jtestr.codehaus.org/
• http://jruby.codehaus.org/
• http://blog.aslakhellesoy.com/