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

EMBRACING THE RED BARA TECHNIQUE FOR SAFELY

REFACTORING YOUR TEST CODEM. Scott Ford (@mscottford) | Chief Code Whisperer and CTO | Corgibytes (@corgibytes)

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

CREDITSLIZ KEOGH ( ) -

MICHAEL FEATHERS ( ) -

@LUNIVORE ORIGINAL ARTICLE

@MFEATHERS ORIGINAL ARTICLE

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

JARGON ALERTWHAT'S A RED BAR?

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

GREEN BAR: ALL TESTS PASSING

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

RED BAR: SOME TESTS FAILING

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

BEWARE OF OVERCONFIDENCE

A CAUTIONARY TALE

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

TEST CODE VS PRODUCTION CODE

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

KEEP YOUR TEST CODE CLEAN

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

NORMAL TDD CYCLE

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

ENSURES THAT YOUR TESTS WILL FAIL IF YOUR PRODUCTION CODE BREAKS

ALLOWS YOU TO REFACTOR YOUR PRODUCTION CODE WITH IMPUNITY

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

RED-BAR CYCLE

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

RED, REFACTOR, GREENENSURES THAT YOUR TEST CODE CONTINUES TO DETECT THE FAILURE CONDITION WHILE

REFACTORING THE TEST CODE

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

REDMUTATE THE PRODUCTION CODE

Produces a red barEnsures that your test can fail

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

REFACTOR

REFACTOR YOUR TESTS

Continue to see the red barEnsures that your tests are still failing

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

GREENREVERSE THE PRODUCTION CODE MUTATION

Bar should switch to greenYour test should now pass

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

 

 

 

 

 

THINGS THAT CAN GO WRONG

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

REDI want to refactor a test, but I can't make it fail

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

REFACTOR

I'm refactoring a test, and it's started passing

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

GREENI reverted my production change, and my tests are still failing

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

CALCULATOR EXAMPLE

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

STARTING POINT

class Calculator def add(left, right) end end

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

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

WATCH IT FAIL

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

WATCH IT FAIL

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

MAKING IT PASS

class Calculator def add(left, right) return 4 end end

WATCH IT PASS

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

WATCH IT PASS

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

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

WATCH THEM FAIL

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

WATCH THEM FAIL

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

MAKE THEM PASS

class Calculator def add(left, right) return left + right end end

WATCH THEM PASS

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

WATCH THEM PASS

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

LET'S REFACTOR OUR TESTS

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

BREAK THE PRODUCTION CODEclass Calculator def add(left, right) return 0 # was: left + right end end

WATCH THE TESTS FAIL

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

WATCH THE TESTS FAIL

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

APPLY THE REFACTORINGdescribe '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

VERIFY THAT THE TESTS STILL FAIL

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

VERIFY THAT THE TESTS STILL FAIL

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

RESTORE PRODUCTION CODEclass Calculator def add(left, right) return left + right end end

WATCH THE TESTS PASS

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

WATCH THE TESTS PASS

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

 

 

 

 

 

QUESTIONS?

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

CONTACT INFO

M. Scott FordTwitter: @mscottfordBlog: http://corgibytes.com/blog

 


Top Related