Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
EMBRACING THE RED BAR
A TECHNIQUE FOR SAFELY
REFACTORING YOUR TEST CODE
M. Scott Ford (@mscottford) | Chief Code Whisperer...
CREDITS
LIZ KEOGH ( ) -
MICHAEL FEATHERS ( ) -
@LUNIVORE ORIGINAL ARTICLE
@MFEATHERS ORIGINAL ARTICLE
JARGON ALERT
WHAT'S A RED BAR?
GREEN BAR: ALL TESTS PASSING
RED BAR: SOME TESTS FAILING
BEWARE OF OVERCONFIDENCE
A CAUTIONARY TALE
TEST CODE VS PRODUCTION CODE
KEEP YOUR TEST CODE CLEAN
NORMAL TDD CYCLE
NSURES THAT YOUR TESTS WILL FAIL IF YOUR PRODUCTION CODE BREAK
ALLOWS YOU TO REFACTOR YOUR PRODUCTION CODE WITH IMPUNITY
RED-BAR CYCLE
RED, REFACTOR, GREEN
ENSURES THAT YOUR TEST CODE CONTINUES
TO DETECT THE FAILURE CONDITION WHILE
REFACTORING THE TEST CODE
RED
MUTATE THE PRODUCTION CODE
Produces a red bar
Ensures that your test can fail
REFACTOR
REFACTOR YOUR TESTS
Continue to see the red bar
Ensures that your tests are still failing
GREEN
REVERSE THE PRODUCTION CODE MUTATION
Bar should switch to green
Your test should now pass
 
 
 
 
 
THINGS THAT CAN GO WRONG
RED
I want to refactor a test, but I can't make it fail
REFACTOR
I'm refactoring a test, and it's started passing
GREEN
I reverted my production change, and my tests are still failing
CALCULATOR EXAMPLE
STARTING POINT
class Calculator
def add(left, right)
end
end
FAILING TEST
describe 'additon' do
specify 'adding two numbers that result in 4' do
calculator = Calculator.new
result = c...
WATCH IT FAIL
MAKING IT PASS
class Calculator
def add(left, right)
return 4
end
end
WATCH IT PASS
MAKE THE TESTS BETTER
describe 'additon' do
specify 'adding two numbers that result in 4' do
calculator = Calculator.new
r...
WATCH THEM FAIL
MAKE THEM PASS
class Calculator
def add(left, right)
return left + right
end
end
WATCH THEM PASS
LET'S REFACTOR OUR TESTS
BREAK THE PRODUCTION CODE
class Calculator
def add(left, right)
return 0 # was: left + right
end
end
WATCH THE TESTS FAIL
APPLY THE REFACTORING
describe 'additon' do
let(:calculator) { Calculator.new }
specify 'adding two numbers that result in...
VERIFY THAT THE TESTS STILL FAIL
RESTORE PRODUCTION CODE
class Calculator
def add(left, right)
return left + right
end
end
WATCH THE TESTS PASS
 
 
 
 
 
QUESTIONS?
CONTACT INFO
M. Scott Ford
Twitter: @mscottford
Blog: http://corgibytes.com/blog
 
Upcoming SlideShare
Loading in …5
×

Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite

462 views

Published on

Presentation originally developed for a presentation at AgileDC 2016.

Presentation abstract:

Does your team treat test code differently than production code? Do you let your test code accumulate duplication and complexity that you'd normally attempt to squash in your production code? Have your tests become brittle? Are you worried that they aren't providing you the same value they used to? Have you strongly considered dumping your test suite and starting over? Are you afraid that if you refactor your test code, you'll introduce false positives?

If you said yes to any of those questions, then this talk is for you.
We'll explore the technique of "refactoring against the red bar" (http://butunclebob.com/ArticleS.MichaelFeathers.RefactoringAgainstTheRedBar), and how you can employ this technique to confidently refactor your test code. No longer do you need to let your test code have a lower standard of quality than your production code.

Published in: Software
  • Be the first to comment

Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite

  1. 1. EMBRACING THE RED BAR A TECHNIQUE FOR SAFELY REFACTORING YOUR TEST CODE M. Scott Ford (@mscottford) | Chief Code Whisperer and CTO | Corgibytes (@corgibytes)
  2. 2. CREDITS LIZ KEOGH ( ) - MICHAEL FEATHERS ( ) - @LUNIVORE ORIGINAL ARTICLE @MFEATHERS ORIGINAL ARTICLE
  3. 3. JARGON ALERT WHAT'S A RED BAR?
  4. 4. GREEN BAR: ALL TESTS PASSING
  5. 5. RED BAR: SOME TESTS FAILING
  6. 6. BEWARE OF OVERCONFIDENCE A CAUTIONARY TALE
  7. 7. TEST CODE VS PRODUCTION CODE
  8. 8. KEEP YOUR TEST CODE CLEAN
  9. 9. NORMAL TDD CYCLE
  10. 10. NSURES THAT YOUR TESTS WILL FAIL IF YOUR PRODUCTION CODE BREAK ALLOWS YOU TO REFACTOR YOUR PRODUCTION CODE WITH IMPUNITY
  11. 11. RED-BAR CYCLE
  12. 12. RED, REFACTOR, GREEN ENSURES THAT YOUR TEST CODE CONTINUES TO DETECT THE FAILURE CONDITION WHILE REFACTORING THE TEST CODE
  13. 13. RED MUTATE THE PRODUCTION CODE Produces a red bar Ensures that your test can fail
  14. 14. REFACTOR REFACTOR YOUR TESTS Continue to see the red bar Ensures that your tests are still failing
  15. 15. GREEN REVERSE THE PRODUCTION CODE MUTATION Bar should switch to green Your test should now pass
  16. 16.           THINGS THAT CAN GO WRONG
  17. 17. RED I want to refactor a test, but I can't make it fail
  18. 18. REFACTOR I'm refactoring a test, and it's started passing
  19. 19. GREEN I reverted my production change, and my tests are still failing
  20. 20. CALCULATOR EXAMPLE
  21. 21. STARTING POINT class Calculator def add(left, right) end end
  22. 22. FAILING TEST describe 'additon' do specify 'adding two numbers that result in 4' do calculator = Calculator.new result = calculator.add(2, 2) expect(result).to eq(4) end end
  23. 23. WATCH IT FAIL
  24. 24. MAKING IT PASS class Calculator def add(left, right) return 4 end end
  25. 25. WATCH IT PASS
  26. 26. MAKE THE TESTS BETTER describe 'additon' do specify 'adding two numbers that result in 4' do calculator = Calculator.new result = calculator.add(2, 2) expect(result).to eq(4) end specify 'adding two numbers that result in 5' do calculator = Calculator.new result = calculator.add(2, 3) expect(result).to eq(5) end end
  27. 27. WATCH THEM FAIL
  28. 28. MAKE THEM PASS class Calculator def add(left, right) return left + right end end
  29. 29. WATCH THEM PASS
  30. 30. LET'S REFACTOR OUR TESTS
  31. 31. BREAK THE PRODUCTION CODE class Calculator def add(left, right) return 0 # was: left + right end end
  32. 32. WATCH THE TESTS FAIL
  33. 33. APPLY THE REFACTORING describe 'additon' do let(:calculator) { Calculator.new } specify 'adding two numbers that result in 4' do result = calculator.add(2, 2) expect(result).to eq(4) end specify 'adding two numbers that result in 5' do result = calculator.add(2, 3) expect(result).to eq(5) end end
  34. 34. VERIFY THAT THE TESTS STILL FAIL
  35. 35. RESTORE PRODUCTION CODE class Calculator def add(left, right) return left + right end end
  36. 36. WATCH THE TESTS PASS
  37. 37.           QUESTIONS?
  38. 38. CONTACT INFO M. Scott Ford Twitter: @mscottford Blog: http://corgibytes.com/blog  

×