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

Beyond Red/Green/Refactor: Testing as a mindset

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.

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. That's not enough information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

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

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  6. 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
  7. That just need to be erased Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  8. 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
  9. 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
  10. The relationship between steps is complex Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  11. 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
  12. We've had a debate about TDD Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  13. 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
  14. TDD is easy! isn't it? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. There's still missing information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. "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
  32. 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
  33. 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
  34. 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
  35. 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
  36. We could write the whole algorithm Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  37. 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
  38. Instead, let's take a small step Noel Rappin, Beyond Red/Green/Refactor,

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  39. 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
  40. This makes the test pass Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

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

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

    WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  43. 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
  44. 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
  45. Normally a good option Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  46. 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
  47. 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
  48. 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
  49. 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
  50. (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
  51. 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
  52. 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
  53. 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
  54. Why is that better? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.

    http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  55. 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
  56. 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
  57. 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
  58. 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
  59. TDD should reduce cognitive load Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  60. 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
  61. 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
  62. Start with a broad test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails

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

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

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

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

    2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  67. 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
  68. 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
  69. 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
  70. I refactor to keep my design clean Noel Rappin, Beyond

    Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap
  71. 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