Share
Embed
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
Transcript
84 slides
-
This slide is intentionally blank Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Let's talk about drawing Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
How to draw Spider-Man Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Step 1 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |
http://www.tablexi.com | @noelrap -
Step 2 Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |
http://www.tablexi.com | @noelrap -
That's not enough information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
In fact, those inital steps seem pointless Noel Rappin, Beyond
Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
That just need to be erased Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
The relationship between steps is complex Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
We've had a debate about TDD Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
TDD is easy! isn't it? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
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 -
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 -
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 -
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 -
There's still missing information Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
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 -
"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 -
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 -
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 -
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 -
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 -
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 -
We could write the whole algorithm Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
Instead, let's take a small step Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
This makes the test pass Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
This is OK Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com
| http://www.tablexi.com | @noelrap -
Because it is not permanent Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
It's sometimes called "sliming the test" Noel Rappin, Beyond Red/Green/Refactor,
WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Okay, now what? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com
| http://www.tablexi.com | @noelrap -
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 -
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 -
Normally a good option Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
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 -
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 -
(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 -
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 -
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 -
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 -
Why is that better? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
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 -
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 -
TDD should reduce cognitive load Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
A plan: Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |
http://www.tablexi.com | @noelrap -
Start with a broad test Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Find a piece that can be encapsulated Noel Rappin, Beyond
Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Test a happy-path solution Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Try to break it Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015.
http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Where did design come in? Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails
2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
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 -
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 -
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 -
I refactor to keep my design clean Noel Rappin, Beyond
Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com | http://www.tablexi.com | @noelrap -
Sarah Mei Noel Rappin, Beyond Red/Green/Refactor, WindyCityRails 2015. http://www.noelrappin.com |
http://www.tablexi.com | @noelrap -
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 -
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