The WHY behind TDD/BDD and the HOW with RSpec

Preview:

DESCRIPTION

A brief introduction to the why behind TDD and an explanation on how BDD builds on that. A quick walk through of the ruby BDD framework, rspec.

Citation preview

The why behind TDD/BDD and the how with RSpec

ben@benmabey.com

$ whois benmabey.com

Registrant: Ben Mabey

Programming Ruby Since: may-2006BDDing it Since: may-2007Employer: Alliance Health Networks

$ whois benmabey.com

Registrant: Ben Mabey

Programming Ruby Since: may-2006BDDing it Since: may-2007Employer: Alliance Health Networks

$ whois benmabey.com

Why do we test?

Why do we test?

Confidence. Verification that our system behaves as it is suppose to.

Why do we test?

Confidence. Verification that our system behaves as it is suppose to.

This can be done manually or can be automated. Automation is clearly our friend...

Why automation?

Why automation?

Confidence. Regression Tests!

Why automation?

Confidence. Regression Tests!

Refactoring - Shortened feedback loop makes improving our design much easier and less painful.

Riddle me this...Which tests instill more confidence of a

system’s behavior in you?(Assume they have the same coverage.)

Riddle me this...Which tests instill more confidence of a

system’s behavior in you?(Assume they have the same coverage.)

Tests written after functionality is added.

Riddle me this...Which tests instill more confidence of a

system’s behavior in you?(Assume they have the same coverage.)

Tests written after functionality is added.

Tests written before functionality is added.

Riddle me this...Which tests instill more confidence of a

system’s behavior in you?(Assume they have the same coverage.)

Tests written after functionality is added.

Neither. Both provide equal confidence.

Tests written before functionality is added.

Why test first then?

Why test first then?Most of us aren’t disciplined enough to test last. Once we have manually tested the system taking the time to write an automated test seems like a pain.

Why test first then?Most of us aren’t disciplined enough to test last. Once we have manually tested the system taking the time to write an automated test seems like a pain.

Code that isn’t written with tests in mind tends to be very difficult to test without refactoring it first.

Clear up a common misconception...

Clear up a common misconception...

TDD != writing tests first

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

TDD isn’t about testing...

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

TDD isn’t about testing...

TDD is about DESIGN

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

TDD isn’t about testing...

TDD is about DESIGN

RED -> GREEN -> REFACTOR

Design is aPROCESS

not aPHASE

The TDD cycle of RED->GREEN->REFACTOR enablesyou to iterate over your design constantly by

removing duplication (refactoring). Testing units in isolation allows you to achieve high cohesion and

loose coupling regularly.

Side benefits of TDD are...

Side benefits of TDD are...Verification of behavior.

Side benefits of TDD are...Verification of behavior.

Documentation.

Side benefits of TDD are...Verification of behavior.

Documentation.“The act of writing a unit test is more an act of design than of verification. It is also more an act of documentation than of verification. The act of writing a unit test closes a remarkable number of feedback loops, the least of which is the one pertaining to verification of function” -”Uncle” Bob Martin

Riddle me that...Which tests instill more confidence of a

system’s design and long term maintainability?

Riddle me that...Which tests instill more confidence of a

system’s design and long term maintainability?

Tests written after functionality is added.

Riddle me that...Which tests instill more confidence of a

system’s design and long term maintainability?

Tests written after functionality is added.

Tests written with good TDD skillz.

Riddle me that...Which tests instill more confidence of a

system’s design and long term maintainability?

Tests written after functionality is added.

It depends. Are you Bob Martin or Jim Coplien? :p

Tests written with good TDD skillz.

Riddle me that...Which tests instill more confidence of a

system’s design and long term maintainability?

Tests written after functionality is added.

It depends. Are you Bob Martin or Jim Coplien? :p

Tests written with good TDD skillz.

I’m with Uncle Bob on this one *when* TDD is done right...

So why BDD?

So why BDD?

Doing TDD right andunderstanding what it is

all about is hard.

So why BDD?Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

So why BDD?

“I had a problem. While using and teaching agile practices like test-driven development (TDD) on

projects in different environments, I kept coming across the same confusion and misunderstandings.

Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand

why a test fails.”

Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

So why BDD?

“The deeper I got into TDD, the more I felt that my own journey had been less of a wax-on, wax-off process of gradual mastery than a series of blind

alleys. I remember thinking ‘If only someone had told me that!’ far more often than I thought ‘Wow, a door has opened.’ I decided it must be possible to present TDD in a way that gets straight to the good stuff

and avoids all the pitfalls.”

Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

So why BDD?

“My response is behaviour-driven development (BDD). It has evolved out of established agile practices and

is designed to make them more accessible and effective for teams new to agile software delivery. Over time, BDD has grown to encompass the wider picture of agile analysis and automated acceptance

testing.”

Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

BDD with RSpec

BDD with RSpecAcceptance Test Driven Testing

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Test Driven Development with a vocabulary that focuses on behavior not tests.

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Test Driven Development with a vocabulary that focuses on behavior not tests.

Story Framework

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Test Driven Development with a vocabulary that focuses on behavior not tests.

Story Framework

As a.. I want.. So that..

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Test Driven Development with a vocabulary that focuses on behavior not tests.

Story Framework

As a.. I want.. So that..

Scenarios - Ruby or plain text

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Test Driven Development with a vocabulary that focuses on behavior not tests.

Story Framework

As a.. I want.. So that..

Scenarios - Ruby or plain text

Example Framework - DSL for specifying behavior

So... how does this help?Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

So... how does this help?

“I had a problem. While using and teaching agile practices like test-driven development (TDD) on

projects in different environments, I kept coming across the same confusion and misunderstandings.

Programmers wanted to know where to start, what to test and what not to test, how much to test in one go, what to call their tests, and how to understand

why a test fails.”

Introducing BDD - Dan Northhttp://dannorth.net/introducing-bdd

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

outside-in

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

outside-in

user stories

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Story: (one line describing the story)

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Story: (one line describing the story)

As a [role]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Story: (one line describing the story)

As a [role]I want to [some action]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Story: (one line describing the story)

As a [role]I want to [some action]So that [business value!]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as Scenarios

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: title

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]And [additional context]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]And [additional context]When [event]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]And [additional context]When [event]Then [outcome]

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]And [additional context]When [event]Then [outcome]

Scenario: another variant

What’s in a story?http://dannorth.net/whats-in-a-story - Dan North

Acceptance Criteria as ScenariosScenario: titleGiven [some context]And [additional context]When [event]Then [outcome]

Scenario: another variantGiven...

Story Time

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

very little-focused

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

very little-focused

sentence template

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

very little-focused

sentence template

documentation

Example Framework

Story Framework

outside-in

user stories

anything else

So... how does this help?where to start

what to test

what not to test

how much to test in one go

what to call their tests

understand why a test fails

idea taken from http://www.slideshare.net/Robbert/bdd-rspec

very little-focused

sentence template

documentation

Test::Unit to RSpec

Test::Unit to RSpec

class DogTest

Test::Unit to RSpec

class DogTest describe Dog

Test::Unit to RSpec

class DogTest describe Dog

def test_bark

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup before(:each) {}

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup before(:each) {}

def teardown

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup before(:each) {}

def teardown after(:each) {}

Matchersa.should == b, a.should equal(b), a.should eql(b)

a.should_not ....

a.should_be nil

a.should be_happy # calls a.happy?

a.should match(/regex/), a.should =~ /regex/)

lambda { a.blah }.should raise_error

http://rspec.info/rdoc/classes/Spec/Matchers.html

Mocks

MocksInteraction-Based Testing

http://www.martinfowler.com/articles/mocksArentStubs.html

http://nat.truemesh.com/archives/000342.html

Not concerned about the state. Just how it interacts with other objects.

MocksInteraction-Based Testing

http://www.martinfowler.com/articles/mocksArentStubs.html

http://nat.truemesh.com/archives/000342.html

Not concerned about the state. Just how it interacts with other objects.

Helps drive design.

MocksInteraction-Based Testing

http://www.martinfowler.com/articles/mocksArentStubs.html

http://nat.truemesh.com/archives/000342.html

Not concerned about the state. Just how it interacts with other objects.

Helps drive design.

Allows you to test collaborators in isolation.

MocksInteraction-Based Testing

http://www.martinfowler.com/articles/mocksArentStubs.html

http://nat.truemesh.com/archives/000342.html

Not concerned about the state. Just how it interacts with other objects.

Helps drive design.

Allows you to test collaborators in isolation.

Need stories to make sure it all “works”.

MocksInteraction-Based Testing

http://www.martinfowler.com/articles/mocksArentStubs.html

http://nat.truemesh.com/archives/000342.html

Not concerned about the state. Just how it interacts with other objects.

Helps drive design.

Allows you to test collaborators in isolation.

Need stories to make sure it all “works”.

Mocks

RSpec Built in Mocking framework

Mocha

FlexMock

Example Time

TipsOne Expectation per test

Write Expectation first

Isolation

Detect Code smells in setup with stubs

ExampleGroup level shared behavior helpers

Blocks are your friend

spec, autotest, drb server, RSpactor

Continuous Integration - CC.rb

Resourceshttp://rspec.info/

http://www.benmabey.com/2008/02/04/rspec-plain-text-stories-webrat-chunky-bacon/

http://blog.davidchelimsky.net/

Recommended Reading

Test Driven Development: By Example, by Kent Beck

Test-Driven Development: A Practical Guide, by David Astels

Domain-Driven Design: Tackling Complexity in the Heart of Software by Eric Evans

Refactoring: Improving the Design of Existing Code by Martin Fowler

Working Effectively with Legacy Code by Michael Feathers

Agile Software Development, Principles, Patterns, and Practices by Bob Martin

Recommended