99
The why behind TDD/BDD and the how with RSpec [email protected]

The WHY behind TDD/BDD and the HOW with RSpec

Embed Size (px)

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

Page 1: The WHY behind TDD/BDD and the HOW with RSpec

The why behind TDD/BDD and the how with RSpec

[email protected]

Page 2: The WHY behind TDD/BDD and the HOW with RSpec
Page 3: The WHY behind TDD/BDD and the HOW with RSpec

$ whois benmabey.com

Page 4: The WHY behind TDD/BDD and the HOW with RSpec

Registrant: Ben Mabey

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

$ whois benmabey.com

Page 5: The WHY behind TDD/BDD and the HOW with RSpec

Registrant: Ben Mabey

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

$ whois benmabey.com

Page 6: The WHY behind TDD/BDD and the HOW with RSpec

Why do we test?

Page 7: The WHY behind TDD/BDD and the HOW with RSpec

Why do we test?

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

Page 8: The WHY behind TDD/BDD and the HOW with RSpec

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...

Page 9: The WHY behind TDD/BDD and the HOW with RSpec

Why automation?

Page 10: The WHY behind TDD/BDD and the HOW with RSpec

Why automation?

Confidence. Regression Tests!

Page 11: The WHY behind TDD/BDD and the HOW with RSpec

Why automation?

Confidence. Regression Tests!

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

Page 12: The WHY behind TDD/BDD and the HOW with RSpec

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

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

Page 13: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 14: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 15: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 16: The WHY behind TDD/BDD and the HOW with RSpec

Why test first then?

Page 17: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 18: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 19: The WHY behind TDD/BDD and the HOW with RSpec

Clear up a common misconception...

Page 20: The WHY behind TDD/BDD and the HOW with RSpec

Clear up a common misconception...

TDD != writing tests first

Page 21: The WHY behind TDD/BDD and the HOW with RSpec

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

Page 22: The WHY behind TDD/BDD and the HOW with RSpec

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

TDD isn’t about testing...

Page 23: The WHY behind TDD/BDD and the HOW with RSpec

Clear up a common misconception...

TDD != writing tests first

TDD =~ writing test first.. but

TDD isn’t about testing...

TDD is about DESIGN

Page 24: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 25: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 26: The WHY behind TDD/BDD and the HOW with RSpec

Side benefits of TDD are...

Page 27: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 28: The WHY behind TDD/BDD and the HOW with RSpec

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

Documentation.

Page 29: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 30: The WHY behind TDD/BDD and the HOW with RSpec

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

system’s design and long term maintainability?

Page 31: The WHY behind TDD/BDD and the HOW with RSpec

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

system’s design and long term maintainability?

Tests written after functionality is added.

Page 32: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 33: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 34: The WHY behind TDD/BDD and the HOW with RSpec

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...

Page 35: The WHY behind TDD/BDD and the HOW with RSpec

So why BDD?

Page 36: The WHY behind TDD/BDD and the HOW with RSpec

So why BDD?

Doing TDD right andunderstanding what it is

all about is hard.

Page 37: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 38: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 39: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 40: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 41: The WHY behind TDD/BDD and the HOW with RSpec

BDD with RSpec

Page 42: The WHY behind TDD/BDD and the HOW with RSpec

BDD with RSpecAcceptance Test Driven Testing

Page 43: The WHY behind TDD/BDD and the HOW with RSpec

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Page 44: The WHY behind TDD/BDD and the HOW with RSpec

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

Page 45: The WHY behind TDD/BDD and the HOW with RSpec

BDD with RSpecAcceptance Test Driven Testing

Business Analysis

Executable Acceptance Criteria

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

Page 46: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 47: The WHY behind TDD/BDD and the HOW with RSpec

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..

Page 48: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 49: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 50: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 51: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 52: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 53: The WHY behind TDD/BDD and the HOW with 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

Page 54: The WHY behind TDD/BDD and the HOW with 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

Page 55: The WHY behind TDD/BDD and the HOW with 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

Page 56: The WHY behind TDD/BDD and the HOW with 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

Page 57: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 58: The WHY behind TDD/BDD and the HOW with RSpec

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

Story: (one line describing the story)

Page 59: The WHY behind TDD/BDD and the HOW with RSpec

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

Story: (one line describing the story)

As a [role]

Page 60: The WHY behind TDD/BDD and the HOW with RSpec

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]

Page 61: The WHY behind TDD/BDD and the HOW with RSpec

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!]

Page 62: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 63: The WHY behind TDD/BDD and the HOW with RSpec

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

Acceptance Criteria as Scenarios

Page 64: The WHY behind TDD/BDD and the HOW with RSpec

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

Acceptance Criteria as ScenariosScenario: title

Page 65: The WHY behind TDD/BDD and the HOW with RSpec

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

Acceptance Criteria as ScenariosScenario: titleGiven [some context]

Page 66: The WHY behind TDD/BDD and the HOW with RSpec

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

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

Page 67: The WHY behind TDD/BDD and the HOW with RSpec

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]

Page 68: The WHY behind TDD/BDD and the HOW with RSpec

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]

Page 69: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 70: The WHY behind TDD/BDD and the HOW with RSpec

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...

Page 71: The WHY behind TDD/BDD and the HOW with RSpec

Story Time

Page 72: The WHY behind TDD/BDD and the HOW with 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

Page 73: The WHY behind TDD/BDD and the HOW with 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

Page 74: The WHY behind TDD/BDD and the HOW with 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

sentence template

Page 75: The WHY behind TDD/BDD and the HOW with 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

sentence template

documentation

Page 76: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 77: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

Page 78: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest

Page 79: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

Page 80: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark

Page 81: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

Page 82: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx

Page 83: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

Page 84: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup

Page 85: The WHY behind TDD/BDD and the HOW with RSpec

Test::Unit to RSpec

class DogTest describe Dog

def test_bark it “should bark”

assert_xxx object.should be_xxx

def setup before(:each) {}

Page 86: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 87: The WHY behind TDD/BDD and the HOW with RSpec

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) {}

Page 88: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 89: The WHY behind TDD/BDD and the HOW with RSpec

Mocks

Page 91: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 92: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 93: The WHY behind TDD/BDD and the HOW with RSpec

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.

Page 94: The WHY behind TDD/BDD and the HOW with RSpec

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”.

Page 95: The WHY behind TDD/BDD and the HOW with RSpec

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”.

Page 96: The WHY behind TDD/BDD and the HOW with RSpec

Mocks

RSpec Built in Mocking framework

Mocha

FlexMock

Page 97: The WHY behind TDD/BDD and the HOW with RSpec

Example Time

Page 98: The WHY behind TDD/BDD and the HOW with RSpec

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

Page 99: The WHY behind TDD/BDD and the HOW with RSpec

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