Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Beyond Red/Green/Refactor: Testing as a mindset

6b767d8a4c9910e007c122d81eb4de73?s=47 Noel Rappin
September 18, 2015

Beyond Red/Green/Refactor: Testing as a mindset

“Red. Green. Refactor” is a great slogan for Test-Driven Development. It’s less great as a guide to actual TDD practice. The slogan provides no guidance for two very important questions in a TDD process: “What do I test next?” and “How do I break my problem into testable pieces”.

You may have struggled with TDD on these two issues, leading to giving up, or winding up with “uncanny valley” tests that are hard to maintain but don’t provide support for change. Using a puzzle problem, we’ll show how to expand on Red Green Refactor, so you can take advantage of the code-quality benefit of TDD.

6b767d8a4c9910e007c122d81eb4de73?s=128

Noel Rappin

September 18, 2015
Tweet

More Decks by Noel Rappin

Other Decks in Technology

Transcript

  1. This slide is intentionally blank Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  2. Let's talk about drawing Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  3. How to draw Spider-Man Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  4. Step 1 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |

    http://www.tablexi.com | @noelrap
  5. Step 2 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |

    http://www.tablexi.com | @noelrap
  6. Done! Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com

    | @noelrap
  7. That's not enough information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  8. In fact, those inital steps seem pointless Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  9. You end up with all these unnecssary lines Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  10. That just need to be erased Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  11. Maybe the person who taught you just wants to sell

    you erasers Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  12. Drawing with lines and ovals Doesn't work Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  13. The relationship between steps is complex Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  14. Beyond Red/Green/Refactor Testing as a Mindset Noel Rappin, Table XI

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  15. We've had a debate about TDD Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  16. I learned that I have been a bad TDD advocate

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  17. TDD is easy! isn't it? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  18. Red Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com

    | @noelrap
  19. Green Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com

    | @noelrap
  20. Refactor Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com

    | @noelrap
  21. Red/Green/Refactor is a slogan not an instruction manual Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  22. I may not have made the motivations for TDD clear

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  23. I didn't want to scare people away from TDD Noel

    Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  24. Like Object-Oriented design, small problems don't show TDD well Noel

    Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  25. Fully implemented TDD solutions are boring to read about Noel

    Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  26. I may have diminished the value of TDD Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  27. Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com |

    @noelrap
  28. There's still missing information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  29. How do I know what ovals and lines to make?

    How do I know when each step is done? What if I want to draw Ms. Marvel instead? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  30. Why are ovals and lines a good way to draw

    Spider-Man? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  31. How do I decide what to test next? How do

    I know when I'm done? What do I refactor? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  32. Why is test-first a good way to write software? Noel

    Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  33. Encourages you to think about the problem In small, focused

    steps In a way that reinforces specific good practices Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  34. What is sometimes called "outside-in" testing is a special case

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  35. Write a test Make it pass with an incomplete implementation

    Write a more focused test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  36. Broad tests that catch lots of errors Focused tests that

    drive design and show where the error is Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  37. A lot of problems seem easy to acceptance test But

    hard to unit test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  38. "What to test next" is a design question Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  39. Given a sequence of digits 1 to 9, such as

    [1, 1, 2, 3, 8] 4 The sum is 15 4 At least one digit appears exactly twice 4 No digit appears more than twice 4 Order is irrelevant Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  40. code at https://github.com/noelrappin/spec_sequence Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com

    | http://www.tablexi.com | @noelrap
  41. Here's what might be the most obivous test: it "knows

    a valid sequence" do expect(Sequence.new(1, 1, 2, 3, 8)).to be_valid end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  42. And then we would make that test pass... Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  43. Wait, How would we make that test pass? Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  44. We could write the whole algorithm Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  45. But that is not letting the test drive the design

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  46. Instead, let's take a small step Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  47. class Sequence def init(*items) @items = items end def valid?

    true end end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  48. This makes the test pass Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  49. This is OK Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com

    | http://www.tablexi.com | @noelrap
  50. Because it is not permanent Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  51. It's sometimes called "sliming the test" Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  52. Okay, now what? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com

    | http://www.tablexi.com | @noelrap
  53. First option: Stay at same abstraction level Write a test

    case that fails Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  54. Broken code it "knows a valid sequence" do expect(Sequence.new(1, 1,

    2, 3, 9)).not_to be_valid end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  55. Normally a good option Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  56. In this case, though, it leads to another round of

    "how do we make this pass?" Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  57. Rule of thumb: If it's not clear how you might

    make a test pass, there's probably a smaller test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  58. Option 2 Go to a less abstract or smaller piece

    Write a failing test there Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  59. Searching for that smaller test is where design happens Noel

    Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  60. (Sometimes, that smaller test is not worth writing) Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  61. A more focused test might be: Part of the job

    One of the cases An error case Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  62. In this case: easy to target the individual clauses (sum

    of 15, has a pair, etc..) Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  63. A smaller test it "can tell if the sum is

    15" do expect(Sequence.new(7, 8).to be_correct_sum) end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  64. Why is that better? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  65. Because it's small enough that I can write it without

    thinking too hard Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  66. Like so def correct_sum? items.inject(:+) == 15 end Noel Rappin,

    Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  67. I don't need to slime this test Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  68. Also, I can negate it easily it "can tell if

    the sum is not 15" do expect(Sequence.new(7, 8, 1).not_to be_correct_sum) end Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  69. TDD should reduce cognitive load Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  70. So from here, I can do 4 has a digit

    pair 4 does not have a digit trio 4 is unique Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  71. Once those are in place, the not_valid test is simpler

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  72. A plan: Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |

    http://www.tablexi.com | @noelrap
  73. Start with a broad test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  74. Find a piece that can be encapsulated Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  75. Test a happy-path solution Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  76. Try to break it Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  77. Where did design come in? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  78. I design my functionality as I write the end-to-end test

    Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  79. I design my code structure as I break the code

    up Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  80. I design my internal API as I write the happy-path

    test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  81. I refactor to keep my design clean Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  82. Sarah Mei Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |

    http://www.tablexi.com | @noelrap
  83. code at https://github.com/noelrappin/spec_sequence Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com

    | http://www.tablexi.com | @noelrap
  84. Noel Rappin Table XI (tablexi.com) @noelrap 4 http://www.noelrappin.com/trdd 4 http://pragprog.com/book/nrtest2/rails-4-test-

    prescriptions 4 http://www.patreon.com/noelrap Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap