0
Getting Comfortable with
    Behavior Driven
      Development
          Alex Sharp
        FrothLogic, Inc.
        ajsha...
Speaking of “getting
  comfortable”...
I hope you get as comfortable
 with BDD as this guy is with
    EVERYTHING. Boom.
Why are you laughing?
Ok, but seriously.
Let’s talk about...

• Things we’ll cover in this talk:
  1. What is BDD?
  2. Why you should care
  3. How to start doing...
So, what is BDD?
• Behavior Driven Development (BDD) is a
  software development methodology that
  takes an outside-in ap...
So, what is BDD?

• BDD borrows it’s core concepts,
  workflow and principles from it’s
  predecessor, Test Driven Developm...
In fact...
In fact...

• BDD builds on top of Test Driven
  Development (TDD)
In fact...

• BDD builds on top of Test Driven
  Development (TDD)
 • Which begs the question...
Well then what’s TDD?!
Well then what’s TDD?!

 • TDD == Write tests first, production
   code second
Well then what’s TDD?!

 • TDD == Write tests first, production
   code second
    • You’re TAFT, right!?
Well then what’s TDD?!

 • TDD == Write tests first, production
   code second
     • You’re TAFT, right!?
 • Main idea: Us...
Well then what’s TDD?!

 • TDD == Write tests first, production
   code second
     • You’re TAFT, right!?
 • Main idea: Us...
Well then what’s TDD?!


  • TDD/BDD Workflow
   • Red, Green, Refactor
Well then what’s TDD?!
                  Not
                     e:Ye
                            llow
                  ...
Well then what’s TDD?!
Well then what’s TDD?!
  •   TDD/BDD Workflow
Well then what’s TDD?!
  •   TDD/BDD Workflow
      •   Red, Green, Refactor
Well then what’s TDD?!
  •   TDD/BDD Workflow
      •   Red, Green, Refactor
           1. Write a failing test
Well then what’s TDD?!
  •   TDD/BDD Workflow
      •   Red, Green, Refactor
           1. Write a failing test
           ...
Well then what’s TDD?!
  •   TDD/BDD Workflow
      •   Red, Green, Refactor
           1. Write a failing test
           ...
Well then what’s TDD?!
  •   TDD/BDD Workflow
      •   Red, Green, Refactor
           1. Write a failing test
           ...
Whoops, we forgot a
      step.
THINK, MAN, THINK!




Don’t forget to think!!
THINK, MAN, THINK!
THINK, MAN, THINK!

 • The real TDD/BDD Workflow
THINK, MAN, THINK!

 • The real TDD/BDD Workflow
  • THINK, Red, Green, Refactor
THINK, MAN, THINK!

 • The real TDD/BDD Workflow
  • THINK, Red, Green, Refactor
  • Thinking is like, really important
THINK, MAN, THINK!

 • The real TDD/BDD Workflow
  • THINK, Red, Green, Refactor
  • Thinking is like, really important
  •...
THINK, MAN, THINK!

 • The real TDD/BDD Workflow
  • THINK, Red, Green, Refactor
  • Thinking is like, really important
  •...
THINK, MAN, THINK!
                         k !!
                     h in
                    t
             t to
       ...
THINK, MAN, THINK!
                         k !!
                     h in
                    t
             t to
       ...
Problem: Thinking is
      HARD.
Problem: Thinking is
        HARD.

• Unfortunately, TDD, and the nature of
  traditional TDD tools often make it harder
 ...
BDD Picks up where
  TDD leaves off
BDD Picks up where
   TDD leaves off
• TDD defines a really awesome workflow
  (think, red, green, refactor)
BDD Picks up where
   TDD leaves off
• TDD defines a really awesome workflow
  (think, red, green, refactor)
• BDD crystalli...
BDD Picks up where
   TDD leaves off
• TDD defines a really awesome workflow
  (think, red, green, refactor)
• BDD crystalli...
BDD Picks up where
   TDD leaves off
• TDD defines a really awesome workflow
  (think, red, green, refactor)
• BDD crystalli...
BDD Picks up where
  TDD leaves off
BDD Picks up where
   TDD leaves off
• Programmer goals and priorities are more
  clearly aligned with requirements
BDD Picks up where
   TDD leaves off
• Programmer goals and priorities are more
  clearly aligned with requirements
• Why??
BDD Picks up where
   TDD leaves off
• Programmer goals and priorities are more
  clearly aligned with requirements
• Why?...
Programmer pig latin
Programmer pig latin

• 01010000 01110010 01101111 01100111
  01110010 01100001 01101101 01101101
  01100101 01110010 0010...
Come to think of it...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
   • Test code?
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
   • Test code?
   • Descr...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
   • Test code?
   • Descr...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
   • Test code?
   • Descr...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  exactly are we trying to do?
   • Test code?
   • Descr...
Come to think of it...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  are we “trying to do?”
   • Test code?
   • Describe be...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  are we “trying to do?”
                             How...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  are we “trying to do?”
                             How...
Come to think of it...
• In doing this whole TDD/BDD thing, what
  are we “trying to do?”
                             How...
Double-whammy
Double-whammy
•   We can describe the behavior we want first,
    and through that process, we drill down into
    lower le...
Double-whammy
•   We can describe the behavior we want first,
    and through that process, we drill down into
    lower le...
Double-whammy
•   We can describe the behavior we want first,
    and through that process, we drill down into
    lower le...
Dig deeper: What’s the goal of
       “testing” code?

• Is it to test code or describe behavior?? Both.
• TDD is about en...
The Fallacy of TDD
The Fallacy of TDD
• Simply writing tests doesn’t necessarily
  yield any value
The Fallacy of TDD
• Simply writing tests doesn’t necessarily
  yield any value
    •   assert_equal(1, 1) # == true
The Fallacy of TDD
• Simply writing tests doesn’t necessarily
  yield any value
    •   assert_equal(1, 1) # == true


• T...
The Fallacy of TDD
• Simply writing tests doesn’t necessarily
  yield any value
    •   assert_equal(1, 1) # == true


• T...
The Fallacy of TDD
• Simply writing tests doesn’t necessarily
  yield any value
    •   assert_equal(1, 1) # == true


• T...
Problems with TDD
Problems with TDD
Problems with TDD
1. Unit and functional tests are exclusive
   to the “programmer domain”
Problems with TDD
1. Unit and functional tests are exclusive
   to the “programmer domain”
   •   Bad for agile because we...
Problems with TDD
1. Unit and functional tests are exclusive
   to the “programmer domain”
   •   Bad for agile because we...
Problems with TDD
1. Unit and functional tests are exclusive
   to the “programmer domain”
   •   Bad for agile because we...
Problems with TDD
Problems with TDD
2. TDD tends to be associated with isolated tests
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
2. TDD tends to be associated with isolated tests
  •   Unit and functional tests as opposed to integrat...
Problems with TDD
Problems with TDD
3. Writing isolated tests can feel contrived
   (again, especially in web apps), which often
   produces...
Problems with TDD
3. Writing isolated tests can feel contrived
   (again, especially in web apps), which often
   produces...
Problems with TDD
3. Writing isolated tests can feel contrived
   (again, especially in web apps), which often
   produces...
Problems with TDD
Problems with TDD
4. A lot gets lost in translation due to these
   problems
Problems with TDD
4. A lot gets lost in translation due to these
   problems
    • Trying to translate customer
       con...
Problems with TDD
4. A lot gets lost in translation due to these
   problems
    • Trying to translate customer
       con...
Problems with TDD
4. A lot gets lost in translation due to these
   problems
    • Trying to translate customer
       con...
Fear not, there is a
    better way
BDD
BDD
•   BDD takes an outside-in approach
BDD
•   BDD takes an outside-in approach
     •   Starts at the user level, rather than programmer
BDD
•   BDD takes an outside-in approach
      •   Starts at the user level, rather than programmer
•   In short, it’s a d...
BDD
•   BDD takes an outside-in approach
      •   Starts at the user level, rather than programmer
•   In short, it’s a d...
BDD
•   BDD takes an outside-in approach
      •   Starts at the user level, rather than programmer
•   In short, it’s a d...
Sweet BDD Diagram.
Sweet BDD Diagram.
Sweet BDD Diagram.
       Customer
                  requireme
                            nts
Sweet BDD Diagram.
              Customer
                         requireme
                                   nts




  ...
Sweet BDD Diagram.
              Customer
                         requireme
                                   nts




  ...
Sweet BDD Diagram.
              Customer
                         requireme
                                   nts




  ...
Sweet BDD Diagram.
              Customer
                         requireme
                                   nts




  ...
Sweet BDD Diagram.
              Customer
                         requireme
                                   nts




  ...
Sweet BDD Diagram.
              Customer
                          requireme
                                    nts




...
Sweet BDD Diagram.
                             Customer
We start at the                          requireme
              ...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Sweet BDD Diagram.
                                    Customer
       We start at the                          requireme
...
Dig deeper: What’s the point of
        testing code?



• Is it to test code or describe behavior??
Dig deeper: What’s the point of
        testing code?



• Is it to test code or describe behavior?? Both.
Workflow

• Describe code with cucumber integration tests
• Use Red, Green, Refactor
• As you go through this process, writ...
Sample Cucumber Feature
Sample Cucumber Feature
Feature: User login

  Scenario: A registered user attempts to login
    Given I am a registered u...
Sample Cucumber Feature
Sample Cucumber Feature


           BDD Outside-in Workflow
           • Cucumber feature
Sample Cucumber Feature
Feature: User login

  Scenario: A registered user attempts to login
    Given I am a registered u...
Sample Cucumber Feature
Feature: User login

  Scenario: A registered user attempts to login
    Given I am a registered u...
Sample Cucumber Feature
Feature: User login

  Scenario: A registered user attempts to login
    Given I am a registered u...
Sample Cucumber Feature
 Feature: User login

    Scenario: A registered user attempts to login
      Given I am a registe...
Sample Cucumber Feature


                                           BDD Outside-in Workflow
                              ...
Sample Cucumber Feature


                                            BDD Outside-in Workflow
                             ...
Cucumber step definition file
Cucumber step definition file



             BDD Outside-in Workflow
             • Cucumber feature
                • Step ...
Cucumber step definition file

Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass|
  ...
Cucumber step definition file

Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass|
  ...
Cucumber step definition file

Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass|
  ...
Cucumber step definition file

Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass|
  ...
User Model Unit Test
(or spec, in this case)
User Model Unit Test
                     (or spec, in this case)
describe User do
  it { should validate_presence_of :use...
User Model Unit Test
                     (or spec, in this case)
describe User do                                 BDD Out...
User Model Unit Test
                     (or spec, in this case)
describe User do                                 BDD Out...
User Model Unit Test
                     (or spec, in this case)
describe User do                                 BDD Out...
User Model Unit Test
                      (or spec, in this case)
 describe User do                                 BDD O...
User Model Unit Test
                    (or spec, in this case)
class User < ActiveRecord::Base
  validates_presence_of :...
User Model Unit Test
                    (or spec, in this case)
class User < ActiveRecord::Base
  validates_presence_of :...
User Model Unit Test
                    (or spec, in this case)
class User < ActiveRecord::Base
  validates_presence_of :...
User Model Unit Test
                    (or spec, in this case)
class User < ActiveRecord::Base
  validates_presence_of :...
User Model Unit Test
                      (or spec, in this case)
  class User < ActiveRecord::Base
    validates_presenc...
Awesome workflow

• This workflow keeps us focused on building
  business value in our code for our
  customers
   • i.e. cu...
Resources
•   BDD
     •   http://behaviour-driven.org/
•   Cucumber
     •   http://cukes.info
     •   http://github.com...
Resources
•   My Blog
     •   http://alexjsharp.com
     •   I’ll post these slides there
•   My github account
     •   ...
Getting Comfortable with BDD
Getting Comfortable with BDD
Upcoming SlideShare
Loading in...5
×

Getting Comfortable with BDD

6,655

Published on

Slides from the "Getting Comfortable with BDD" talk I gave a Nashville Barcamp 2009.

Published in: Technology
2 Comments
15 Likes
Statistics
Notes
  • @ambertch Thanks! I'll be giving revised versions of the Mongo talk at Ruby Midwest and Ruby Kaigi with updates on more in-depth implementations of Mongo.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • haha! This slideshow is full of win - I attended your session at MongoSF and found this looking at those slides, good stuff...
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
6,655
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
133
Comments
2
Likes
15
Embeds 0
No embeds

No notes for slide
  • Transcript of "Getting Comfortable with BDD"

    1. 1. Getting Comfortable with Behavior Driven Development Alex Sharp FrothLogic, Inc. ajsharp@frothlogic.com http://twitter.com/ajsharp
    2. 2. Speaking of “getting comfortable”...
    3. 3. I hope you get as comfortable with BDD as this guy is with EVERYTHING. Boom.
    4. 4. Why are you laughing?
    5. 5. Ok, but seriously.
    6. 6. Let’s talk about... • Things we’ll cover in this talk: 1. What is BDD? 2. Why you should care 3. How to start doing BDD with cucumber
    7. 7. So, what is BDD? • Behavior Driven Development (BDD) is a software development methodology that takes an outside-in approach to describe application behavior by encouraging intense customer involvement • Don’t worry, we’ll take this explanation from textbook to tolerable over the next 20-ish minutes...
    8. 8. So, what is BDD? • BDD borrows it’s core concepts, workflow and principles from it’s predecessor, Test Driven Development
    9. 9. In fact...
    10. 10. In fact... • BDD builds on top of Test Driven Development (TDD)
    11. 11. In fact... • BDD builds on top of Test Driven Development (TDD) • Which begs the question...
    12. 12. Well then what’s TDD?!
    13. 13. Well then what’s TDD?! • TDD == Write tests first, production code second
    14. 14. Well then what’s TDD?! • TDD == Write tests first, production code second • You’re TAFT, right!?
    15. 15. Well then what’s TDD?! • TDD == Write tests first, production code second • You’re TAFT, right!? • Main idea: Use test code to drive production code
    16. 16. Well then what’s TDD?! • TDD == Write tests first, production code second • You’re TAFT, right!? • Main idea: Use test code to drive production code • More on this later...
    17. 17. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor
    18. 18. Well then what’s TDD?! Not e:Ye llow text has absol utel y no r eleva nce • TDD/BDD Workflow here. • Red, Green, Refactor
    19. 19. Well then what’s TDD?!
    20. 20. Well then what’s TDD?! • TDD/BDD Workflow
    21. 21. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor
    22. 22. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor 1. Write a failing test
    23. 23. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor 1. Write a failing test 2. Write the smallest amount of production code possible to get that test to pass
    24. 24. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor 1. Write a failing test 2. Write the smallest amount of production code possible to get that test to pass 3. Refactor
    25. 25. Well then what’s TDD?! • TDD/BDD Workflow • Red, Green, Refactor 1. Write a failing test 2. Write the smallest amount of production code possible to get that test to pass 3. Refactor • i.e. add more tests and write better production code
    26. 26. Whoops, we forgot a step.
    27. 27. THINK, MAN, THINK! Don’t forget to think!!
    28. 28. THINK, MAN, THINK!
    29. 29. THINK, MAN, THINK! • The real TDD/BDD Workflow
    30. 30. THINK, MAN, THINK! • The real TDD/BDD Workflow • THINK, Red, Green, Refactor
    31. 31. THINK, MAN, THINK! • The real TDD/BDD Workflow • THINK, Red, Green, Refactor • Thinking is like, really important
    32. 32. THINK, MAN, THINK! • The real TDD/BDD Workflow • THINK, Red, Green, Refactor • Thinking is like, really important • It’s easy to forget to think when you can just start hacking L337 c0D3Z
    33. 33. THINK, MAN, THINK! • The real TDD/BDD Workflow • THINK, Red, Green, Refactor • Thinking is like, really important • It’s easy to forget to think when you can just start hacking L337 c0D3Z
    34. 34. THINK, MAN, THINK! k !! h in t t to ge n ’t for • The real TDD/BDD Workflow D o • THINK, Red, Green, Refactor • Thinking is like, really important • It’s easy to forget to think when you can just start hacking L337 c0D3Z
    35. 35. THINK, MAN, THINK! k !! h in t t to ge n ’t for • The real TDD/BDD Workflow D o • THINK, Red, Green, Refactor • Thinking is like, really important • It’s easy to forget to think when you can just start hacking L337 c0D3Z
    36. 36. Problem: Thinking is HARD.
    37. 37. Problem: Thinking is HARD. • Unfortunately, TDD, and the nature of traditional TDD tools often make it harder to think about what you’re trying to do.
    38. 38. BDD Picks up where TDD leaves off
    39. 39. BDD Picks up where TDD leaves off • TDD defines a really awesome workflow (think, red, green, refactor)
    40. 40. BDD Picks up where TDD leaves off • TDD defines a really awesome workflow (think, red, green, refactor) • BDD crystallizes some of the things that TDD leaves unclear or undefined.
    41. 41. BDD Picks up where TDD leaves off • TDD defines a really awesome workflow (think, red, green, refactor) • BDD crystallizes some of the things that TDD leaves unclear or undefined. • BDD == TDD 2.0
    42. 42. BDD Picks up where TDD leaves off • TDD defines a really awesome workflow (think, red, green, refactor) • BDD crystallizes some of the things that TDD leaves unclear or undefined. • BDD == TDD 2.0 • BDD bridges the gap between specs/ requirements and actual implementation
    43. 43. BDD Picks up where TDD leaves off
    44. 44. BDD Picks up where TDD leaves off • Programmer goals and priorities are more clearly aligned with requirements
    45. 45. BDD Picks up where TDD leaves off • Programmer goals and priorities are more clearly aligned with requirements • Why??
    46. 46. BDD Picks up where TDD leaves off • Programmer goals and priorities are more clearly aligned with requirements • Why?? • BDD is implemented in business language not programmer pig latin
    47. 47. Programmer pig latin
    48. 48. Programmer pig latin • 01010000 01110010 01101111 01100111 01110010 01100001 01101101 01101101 01100101 01110010 00100000 01110000 01101001 01100111 00100000 01101100 01100001 01110100 01101001 01101110
    49. 49. Come to think of it...
    50. 50. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do?
    51. 51. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do? • Test code?
    52. 52. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do? • Test code? • Describe behavior?
    53. 53. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do? • Test code? • Describe behavior? • Bill hours?
    54. 54. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do? • Test code? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    55. 55. Come to think of it... • In doing this whole TDD/BDD thing, what exactly are we trying to do? • Test code? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    56. 56. Come to think of it...
    57. 57. Come to think of it... • In doing this whole TDD/BDD thing, what are we “trying to do?” • Test code? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    58. 58. Come to think of it... • In doing this whole TDD/BDD thing, what are we “trying to do?” How bout both, • Test code? AT THE SAME TIME!? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    59. 59. Come to think of it... • In doing this whole TDD/BDD thing, what are we “trying to do?” How bout both, • Test code? AT THE SAME TIME!? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    60. 60. Come to think of it... • In doing this whole TDD/BDD thing, what are we “trying to do?” How bout both, • Test code? AT THE SAME TIME!? • Describe behavior? • Bill hours? • Bawl like ca$h money millionaires?
    61. 61. Double-whammy
    62. 62. Double-whammy • We can describe the behavior we want first, and through that process, we drill down into lower level unit and functional tests for our code.
    63. 63. Double-whammy • We can describe the behavior we want first, and through that process, we drill down into lower level unit and functional tests for our code. • What’s the point of testing code before we pin down what that code should do?
    64. 64. Double-whammy • We can describe the behavior we want first, and through that process, we drill down into lower level unit and functional tests for our code. • What’s the point of testing code before we pin down what that code should do? • In other words, we need to define the behavior we want/expect before we go about testing it.
    65. 65. Dig deeper: What’s the goal of “testing” code? • Is it to test code or describe behavior?? Both. • TDD is about ensuring behavior. • BDD is about describing behavior. • First we describe the behavior, then we ensure that behavior works as expected.
    66. 66. The Fallacy of TDD
    67. 67. The Fallacy of TDD • Simply writing tests doesn’t necessarily yield any value
    68. 68. The Fallacy of TDD • Simply writing tests doesn’t necessarily yield any value • assert_equal(1, 1) # == true
    69. 69. The Fallacy of TDD • Simply writing tests doesn’t necessarily yield any value • assert_equal(1, 1) # == true • To be useful, our tests must have meaning, and that requires:
    70. 70. The Fallacy of TDD • Simply writing tests doesn’t necessarily yield any value • assert_equal(1, 1) # == true • To be useful, our tests must have meaning, and that requires: 1. Listening to our customers.
    71. 71. The Fallacy of TDD • Simply writing tests doesn’t necessarily yield any value • assert_equal(1, 1) # == true • To be useful, our tests must have meaning, and that requires: 1. Listening to our customers. 2. Thinking. Hard.
    72. 72. Problems with TDD
    73. 73. Problems with TDD
    74. 74. Problems with TDD 1. Unit and functional tests are exclusive to the “programmer domain”
    75. 75. Problems with TDD 1. Unit and functional tests are exclusive to the “programmer domain” • Bad for agile because we lose the customer-centric nature of agile at this point in the process
    76. 76. Problems with TDD 1. Unit and functional tests are exclusive to the “programmer domain” • Bad for agile because we lose the customer-centric nature of agile at this point in the process • Agile encourages deep customer involvement and interaction (as opposed to pre-defined requirements gathering phase)
    77. 77. Problems with TDD 1. Unit and functional tests are exclusive to the “programmer domain” • Bad for agile because we lose the customer-centric nature of agile at this point in the process • Agile encourages deep customer involvement and interaction (as opposed to pre-defined requirements gathering phase) • This breaks down when programmers start writing tests in programmer language
    78. 78. Problems with TDD
    79. 79. Problems with TDD 2. TDD tends to be associated with isolated tests
    80. 80. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests
    81. 81. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though)
    82. 82. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good:
    83. 83. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good: • Tests run faster
    84. 84. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good: • Tests run faster • Tests are less brittle because not dependent on other parts of app
    85. 85. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good: • Tests run faster • Tests are less brittle because not dependent on other parts of app • Rarely the case with web apps
    86. 86. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good: • Tests run faster • Tests are less brittle because not dependent on other parts of app • Rarely the case with web apps • Bad:
    87. 87. Problems with TDD 2. TDD tends to be associated with isolated tests • Unit and functional tests as opposed to integration tests • This is both good and bad (mostly bad though) • Good: • Tests run faster • Tests are less brittle because not dependent on other parts of app • Rarely the case with web apps • Bad: • Tests become brittle due to excessive state setup (see next slide)
    88. 88. Problems with TDD
    89. 89. Problems with TDD 3. Writing isolated tests can feel contrived (again, especially in web apps), which often produces crappy test code, which results in crappier production code
    90. 90. Problems with TDD 3. Writing isolated tests can feel contrived (again, especially in web apps), which often produces crappy test code, which results in crappier production code • Typically happens when we have complex state setup (like a web app)
    91. 91. Problems with TDD 3. Writing isolated tests can feel contrived (again, especially in web apps), which often produces crappy test code, which results in crappier production code • Typically happens when we have complex state setup (like a web app) • Secondary problem: This “contrived- ness” pushes devs away from testing altogether, which is even worse.
    92. 92. Problems with TDD
    93. 93. Problems with TDD 4. A lot gets lost in translation due to these problems
    94. 94. Problems with TDD 4. A lot gets lost in translation due to these problems • Trying to translate customer conversations into test code is hard as it is
    95. 95. Problems with TDD 4. A lot gets lost in translation due to these problems • Trying to translate customer conversations into test code is hard as it is • Trying to overcome the frustration of writing seemingly contrived test cases makes it even harder
    96. 96. Problems with TDD 4. A lot gets lost in translation due to these problems • Trying to translate customer conversations into test code is hard as it is • Trying to overcome the frustration of writing seemingly contrived test cases makes it even harder • Frustration from testing leads to loss of focus and decreased productivity
    97. 97. Fear not, there is a better way
    98. 98. BDD
    99. 99. BDD • BDD takes an outside-in approach
    100. 100. BDD • BDD takes an outside-in approach • Starts at the user level, rather than programmer
    101. 101. BDD • BDD takes an outside-in approach • Starts at the user level, rather than programmer • In short, it’s a different way of thinking about and doing TDD
    102. 102. BDD • BDD takes an outside-in approach • Starts at the user level, rather than programmer • In short, it’s a different way of thinking about and doing TDD • Caveat
    103. 103. BDD • BDD takes an outside-in approach • Starts at the user level, rather than programmer • In short, it’s a different way of thinking about and doing TDD • Caveat • Technically, we could adapt our TDD tools to serve a BDD-like role, but we’ll still run into the problems just mentioned
    104. 104. Sweet BDD Diagram.
    105. 105. Sweet BDD Diagram.
    106. 106. Sweet BDD Diagram. Customer requireme nts
    107. 107. Sweet BDD Diagram. Customer requireme nts Custom er requi rements
    108. 108. Sweet BDD Diagram. Customer requireme nts Cu s to me rr eq uir em en ts Custom er requi rements
    109. 109. Sweet BDD Diagram. Customer requireme nts Cu s to me rr eq uir em en ts n ts e em quir r re e m s to Custom Cu er requi rements
    110. 110. Sweet BDD Diagram. Customer requireme nts Cu BDD (integration tests) s to me rr eq uir em en ts n ts e em quir r re e m s to Custom Cu er requi rements
    111. 111. Sweet BDD Diagram. Customer requireme nts Cu BDD (integration tests) s to me rr eq uir em en ts n ts e em quir BDD (integration tests) re e r m s to Custom Cu er requi rements
    112. 112. Sweet BDD Diagram. Customer requireme nts Cu BDD (integration tests) s to me rr eq uir em TDD (unit/functional en tests) ts n ts e em quir BDD (integration tests) re e r m s to Custom Cu er requi rements
    113. 113. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir em TDD (unit/functional en tests) ts n ts e em quir BDD (integration tests) re e r m s to Custom Cu er requi rements
    114. 114. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m s to Custom Cu er requi rements
    115. 115. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m Yes. s to Custom Cu er requi rements
    116. 116. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m Yes. s to Custom Cu er requi rements
    117. 117. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m Yes. s to Custom Cu er requi rements
    118. 118. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m Yes. s to Custom Cu er requi rements
    119. 119. Sweet BDD Diagram. Customer We start at the requireme nts outside and drill down, inwards Cu towards unit and BDD (integration tests) s to functional tests me rr eq uir By now you might be em TDD (unit/functional thinking, “Aren’t en tests) these just integration ts n ts tests showed through e em a totally awesome quir diagram?” BDD (integration tests) re e r m Yes. s to Custom Cu er requi rements
    120. 120. Dig deeper: What’s the point of testing code? • Is it to test code or describe behavior??
    121. 121. Dig deeper: What’s the point of testing code? • Is it to test code or describe behavior?? Both.
    122. 122. Workflow • Describe code with cucumber integration tests • Use Red, Green, Refactor • As you go through this process, write unit and functional tests along the way
    123. 123. Sample Cucumber Feature
    124. 124. Sample Cucumber Feature Feature: User login Scenario: A registered user attempts to login Given I am a registered user with login "ajsharp" and password "secret" When I go to the user login page And I fill in "Login" with "ajsharp" And I fill in "Password" with "secret" And I click the "Log In" button Then I should be redirected to my profile page And I should see "ajsharp's profile"
    125. 125. Sample Cucumber Feature
    126. 126. Sample Cucumber Feature BDD Outside-in Workflow • Cucumber feature
    127. 127. Sample Cucumber Feature Feature: User login Scenario: A registered user attempts to login Given I am a registered user with login "ajsharp" and password "secret" BDD Outside-in Workflow • Cucumber feature
    128. 128. Sample Cucumber Feature Feature: User login Scenario: A registered user attempts to login Given I am a registered user with login "ajsharp" and password "secret" BDD Outside-in Workflow • Cucumber feature
    129. 129. Sample Cucumber Feature Feature: User login Scenario: A registered user attempts to login Given I am a registered user with login "ajsharp" and password "secret" BDD Outside-in Workflow • Cucumber feature
    130. 130. Sample Cucumber Feature Feature: User login Scenario: A registered user attempts to login Given I am a registered user with login "ajsharp" and password "secret" BDD Outside-in Workflow • Cucumber feature From here, cucumber looks “step definition” files that match (via regular expressions -- shout out Perl) the statements in your .feature file.
    131. 131. Sample Cucumber Feature BDD Outside-in Workflow • Cucumber feature From here, cucumber looks “step definition” files that match (via regular expressions -- shout out Perl) the statements in your .feature file.
    132. 132. Sample Cucumber Feature BDD Outside-in Workflow • Cucumber feature From here, cucumber looks “step definition” files that match (via regular expressions -- shout out Perl) the statements in your .feature file. Next Step: Write a failing test
    133. 133. Cucumber step definition file
    134. 134. Cucumber step definition file BDD Outside-in Workflow • Cucumber feature • Step Definition
    135. 135. Cucumber step definition file Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass| @user = User.create!(:username => login, :password => pass) end BDD Outside-in Workflow • Cucumber feature • Step Definition
    136. 136. Cucumber step definition file Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass| @user = User.create!(:username => login, :password => pass) end BDD Outside-in Workflow • Cucumber feature • Step Definition We haven’t implemented the User model yet, so let’s do that....
    137. 137. Cucumber step definition file Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass| @user = User.create!(:username => login, :password => pass) end BDD Outside-in Workflow • Cucumber feature • Step Definition We haven’t implemented the User model yet, so let’s do that.... But wait!! To implement the User model, we need to write some unit tests for it....Red, Green, Refactor
    138. 138. Cucumber step definition file Given /^I am a registered user with username "(.+)" and password "(.+)"$/ do |login, pass| @user = User.create!(:username => login, :password => pass) end BDD Outside-in Workflow • Cucumber feature • Step Definition We haven’t implemented the User model yet, so let’s do that.... But wait!! To implement the User model, we need to write some unit tests for it....Red, Green, Refactor Next Step: Green - To make the cucumber step pass (higher level, more outside) we need to do Red - write failing tests for user model (lower level, more inside)
    139. 139. User Model Unit Test (or spec, in this case)
    140. 140. User Model Unit Test (or spec, in this case) describe User do it { should validate_presence_of :username } it { should validate_presence_of :password } end
    141. 141. User Model Unit Test (or spec, in this case) describe User do BDD Outside-in Workflow it { should validate_presence_of :username } it { should validate_presence_of :password } • Cucumber feature end • Step Definition • User model unit test/spec
    142. 142. User Model Unit Test (or spec, in this case) describe User do BDD Outside-in Workflow it { should validate_presence_of :username } it { should validate_presence_of :password } • Cucumber feature end • Step Definition • User model unit test/spec
    143. 143. User Model Unit Test (or spec, in this case) describe User do BDD Outside-in Workflow it { should validate_presence_of :username } it { should validate_presence_of :password } • Cucumber feature end • Step Definition • User model unit test/spec This will fail, because we haven’t written any production code yet
    144. 144. User Model Unit Test (or spec, in this case) describe User do BDD Outside-in Workflow it { should validate_presence_of :username } it { should validate_presence_of :password } • Cucumber feature end • Step Definition • User model unit test/spec This will fail, because we haven’t written any production code yet Next Step: Green - Make our model unit tests pass by writing some production code
    145. 145. User Model Unit Test (or spec, in this case) class User < ActiveRecord::Base validates_presence_of :username validates_presence_of :password end
    146. 146. User Model Unit Test (or spec, in this case) class User < ActiveRecord::Base validates_presence_of :username BDD Outside-in Workflow validates_presence_of :password end • Cucumber feature • Step Definition • User model unit test/spec • User model production code
    147. 147. User Model Unit Test (or spec, in this case) class User < ActiveRecord::Base validates_presence_of :username BDD Outside-in Workflow validates_presence_of :password end • Cucumber feature • Step Definition • User model unit test/spec • User model production code
    148. 148. User Model Unit Test (or spec, in this case) class User < ActiveRecord::Base validates_presence_of :username BDD Outside-in Workflow validates_presence_of :password end • Cucumber feature • Step Definition • User model unit test/spec • User model production code This will make our unit tests pass. Nice.
    149. 149. User Model Unit Test (or spec, in this case) class User < ActiveRecord::Base validates_presence_of :username BDD Outside-in Workflow validates_presence_of :password end • Cucumber feature • Step Definition • User model unit test/spec • User model production code This will make our unit tests pass. Nice. “Bubble up” back to the cucumber .feature as our unit/functional tests pass
    150. 150. Awesome workflow • This workflow keeps us focused on building business value in our code for our customers • i.e. customer requirements
    151. 151. Resources • BDD • http://behaviour-driven.org/ • Cucumber • http://cukes.info • http://github.com/aslakhellesoy/cucumber • Rspec Beta Book • http://pragprog.com/titles/achbd/the-rspec- book
    152. 152. Resources • My Blog • http://alexjsharp.com • I’ll post these slides there • My github account • http://github.com/ajsharp • Questions? • ajsharp@frothlogic.com
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×