SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
EMBRACING THE RED BAR
A TECHNIQUE FOR SAFELY
REFACTORING YOUR TEST CODE
M. Scott Ford (@mscottford) | Chief Code Whisperer and CTO | Corgibytes (@corgibytes)
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 = calculator.add(2, 2)
expect(result).to eq(4)
end
end
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
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
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 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
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
 

Mais conteúdo relacionado

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

Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
Practical unit testing 2014
Practical unit testing 2014Practical unit testing 2014
Practical unit testing 2014Andrew Fray
 
Moving from Testing to Quality by Jeff Van Fleet
Moving from Testing to Quality by Jeff Van FleetMoving from Testing to Quality by Jeff Van Fleet
Moving from Testing to Quality by Jeff Van FleetQA or the Highway
 
Pairwise testing technique-Made easy
Pairwise testing technique-Made easyPairwise testing technique-Made easy
Pairwise testing technique-Made easySamee Ahmed Indikar
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014alexandre freire
 
Test Smart, not hard
Test Smart, not hardTest Smart, not hard
Test Smart, not hardDiUS
 
ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTSALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTSKate Campbell
 
CIS 115 Effective Communication - tutorialrank.com
CIS 115  Effective Communication - tutorialrank.comCIS 115  Effective Communication - tutorialrank.com
CIS 115 Effective Communication - tutorialrank.comBartholomew18
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxssuser2f67c91
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your DatabaseDavid Wheeler
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiElsayed Hemayed
 
TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)Steve Stedman
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PITRafał Leszko
 

Semelhante a Embracing the Red Bar: A Technique for Safely Refactoring Your Test Suite (20)

CST2403 NOTES
CST2403 NOTESCST2403 NOTES
CST2403 NOTES
 
TDD
TDDTDD
TDD
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
Week 5
Week 5Week 5
Week 5
 
Week 5
Week 5Week 5
Week 5
 
TDD in Powershell
TDD in PowershellTDD in Powershell
TDD in Powershell
 
Practical unit testing 2014
Practical unit testing 2014Practical unit testing 2014
Practical unit testing 2014
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
 
Moving from Testing to Quality by Jeff Van Fleet
Moving from Testing to Quality by Jeff Van FleetMoving from Testing to Quality by Jeff Van Fleet
Moving from Testing to Quality by Jeff Van Fleet
 
SD & D Testing
SD & D TestingSD & D Testing
SD & D Testing
 
Pairwise testing technique-Made easy
Pairwise testing technique-Made easyPairwise testing technique-Made easy
Pairwise testing technique-Made easy
 
Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014Como NÃO testar o seu projeto de Software. DevDay 2014
Como NÃO testar o seu projeto de Software. DevDay 2014
 
Test Smart, not hard
Test Smart, not hardTest Smart, not hard
Test Smart, not hard
 
ALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTSALGORITHMS AND FLOWCHARTS
ALGORITHMS AND FLOWCHARTS
 
CIS 115 Effective Communication - tutorialrank.com
CIS 115  Effective Communication - tutorialrank.comCIS 115  Effective Communication - tutorialrank.com
CIS 115 Effective Communication - tutorialrank.com
 
ICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptxICT_Seminar_flow_charts_for_2013_Nov.pptx
ICT_Seminar_flow_charts_for_2013_Nov.pptx
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your Database
 
Csci101 lect07 algorithms_ii
Csci101 lect07 algorithms_iiCsci101 lect07 algorithms_ii
Csci101 lect07 algorithms_ii
 
TSQL Functions (SQL Server)
TSQL Functions (SQL Server)TSQL Functions (SQL Server)
TSQL Functions (SQL Server)
 
Mutation Testing with PIT
Mutation Testing with PITMutation Testing with PIT
Mutation Testing with PIT
 

Mais de M. Scott Ford

Lessons Learned Migrating from Jekyll to Next.pptx
Lessons Learned Migrating from Jekyll to Next.pptxLessons Learned Migrating from Jekyll to Next.pptx
Lessons Learned Migrating from Jekyll to Next.pptxM. Scott Ford
 
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYear
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYearPyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYear
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYearM. Scott Ford
 
Communication is Just as Important as Code
Communication is Just as Important as CodeCommunication is Just as Important as Code
Communication is Just as Important as CodeM. Scott Ford
 
MenderCon 2021 - Keynote
MenderCon 2021 - KeynoteMenderCon 2021 - Keynote
MenderCon 2021 - KeynoteM. Scott Ford
 
Moving a Monolith to Kubernetes
Moving a Monolith to KubernetesMoving a Monolith to Kubernetes
Moving a Monolith to KubernetesM. Scott Ford
 
MenderCon 2020 Keynote Presentation
MenderCon 2020 Keynote PresentationMenderCon 2020 Keynote Presentation
MenderCon 2020 Keynote PresentationM. Scott Ford
 
Makers and menders - Finding Talent for Legacy Code Projects
Makers and menders - Finding Talent for Legacy Code ProjectsMakers and menders - Finding Talent for Legacy Code Projects
Makers and menders - Finding Talent for Legacy Code ProjectsM. Scott Ford
 
A deep dive into measuring dependency freshness with lib year
A deep dive into measuring dependency freshness with lib yearA deep dive into measuring dependency freshness with lib year
A deep dive into measuring dependency freshness with lib yearM. Scott Ford
 
Building a Bridge to a Legacy Application: How Hard Can That Be?
Building a Bridge to a Legacy Application: How Hard Can That Be?Building a Bridge to a Legacy Application: How Hard Can That Be?
Building a Bridge to a Legacy Application: How Hard Can That Be?M. Scott Ford
 
Important metrics for Measuring Code Health
Important metrics for Measuring Code HealthImportant metrics for Measuring Code Health
Important metrics for Measuring Code HealthM. Scott Ford
 

Mais de M. Scott Ford (10)

Lessons Learned Migrating from Jekyll to Next.pptx
Lessons Learned Migrating from Jekyll to Next.pptxLessons Learned Migrating from Jekyll to Next.pptx
Lessons Learned Migrating from Jekyll to Next.pptx
 
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYear
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYearPyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYear
PyGeekle 2022 - A Deep Dive into Measuring Dependency Freshness with LibYear
 
Communication is Just as Important as Code
Communication is Just as Important as CodeCommunication is Just as Important as Code
Communication is Just as Important as Code
 
MenderCon 2021 - Keynote
MenderCon 2021 - KeynoteMenderCon 2021 - Keynote
MenderCon 2021 - Keynote
 
Moving a Monolith to Kubernetes
Moving a Monolith to KubernetesMoving a Monolith to Kubernetes
Moving a Monolith to Kubernetes
 
MenderCon 2020 Keynote Presentation
MenderCon 2020 Keynote PresentationMenderCon 2020 Keynote Presentation
MenderCon 2020 Keynote Presentation
 
Makers and menders - Finding Talent for Legacy Code Projects
Makers and menders - Finding Talent for Legacy Code ProjectsMakers and menders - Finding Talent for Legacy Code Projects
Makers and menders - Finding Talent for Legacy Code Projects
 
A deep dive into measuring dependency freshness with lib year
A deep dive into measuring dependency freshness with lib yearA deep dive into measuring dependency freshness with lib year
A deep dive into measuring dependency freshness with lib year
 
Building a Bridge to a Legacy Application: How Hard Can That Be?
Building a Bridge to a Legacy Application: How Hard Can That Be?Building a Bridge to a Legacy Application: How Hard Can That Be?
Building a Bridge to a Legacy Application: How Hard Can That Be?
 
Important metrics for Measuring Code Health
Important metrics for Measuring Code HealthImportant metrics for Measuring Code Health
Important metrics for Measuring Code Health
 

Último

Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLionel Briand
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorTier1 app
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITmanoharjgpsolutions
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...Bert Jan Schrijver
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonApplitools
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsChristian Birchler
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfDrew Moseley
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogueitservices996
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingShane Coughlan
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...OnePlan Solutions
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identityteam-WIBU
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfmaor17
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...OnePlan Solutions
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxAndreas Kunz
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsJean Silva
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringHironori Washizaki
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencessuser9e7c64
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldRoberto Pérez Alcolea
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZABSYZ Inc
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalLionel Briand
 

Último (20)

Large Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and RepairLarge Language Models for Test Case Evolution and Repair
Large Language Models for Test Case Evolution and Repair
 
Effectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryErrorEffectively Troubleshoot 9 Types of OutOfMemoryError
Effectively Troubleshoot 9 Types of OutOfMemoryError
 
Best Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh ITBest Angular 17 Classroom & Online training - Naresh IT
Best Angular 17 Classroom & Online training - Naresh IT
 
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
JavaLand 2024 - Going serverless with Quarkus GraalVM native images and AWS L...
 
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + KobitonLeveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
Leveraging AI for Mobile App Testing on Real Devices | Applitools + Kobiton
 
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving CarsSensoDat: Simulation-based Sensor Dataset of Self-driving Cars
SensoDat: Simulation-based Sensor Dataset of Self-driving Cars
 
Comparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdfComparing Linux OS Image Update Models - EOSS 2024.pdf
Comparing Linux OS Image Update Models - EOSS 2024.pdf
 
Ronisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited CatalogueRonisha Informatics Private Limited Catalogue
Ronisha Informatics Private Limited Catalogue
 
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full RecordingOpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
OpenChain AI Study Group - Europe and Asia Recap - 2024-04-11 - Full Recording
 
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
Revolutionizing the Digital Transformation Office - Leveraging OnePlan’s AI a...
 
Post Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on IdentityPost Quantum Cryptography – The Impact on Identity
Post Quantum Cryptography – The Impact on Identity
 
Zer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdfZer0con 2024 final share short version.pdf
Zer0con 2024 final share short version.pdf
 
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
Tech Tuesday Slides - Introduction to Project Management with OnePlan's Work ...
 
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptxUI5ers live - Custom Controls wrapping 3rd-party libs.pptx
UI5ers live - Custom Controls wrapping 3rd-party libs.pptx
 
Strategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero resultsStrategies for using alternative queries to mitigate zero results
Strategies for using alternative queries to mitigate zero results
 
Machine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their EngineeringMachine Learning Software Engineering Patterns and Their Engineering
Machine Learning Software Engineering Patterns and Their Engineering
 
Patterns for automating API delivery. API conference
Patterns for automating API delivery. API conferencePatterns for automating API delivery. API conference
Patterns for automating API delivery. API conference
 
Keeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository worldKeeping your build tool updated in a multi repository world
Keeping your build tool updated in a multi repository world
 
Salesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZSalesforce Implementation Services PPT By ABSYZ
Salesforce Implementation Services PPT By ABSYZ
 
Precise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive GoalPrecise and Complete Requirements? An Elusive Goal
Precise and Complete Requirements? An Elusive Goal
 

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