SlideShare a Scribd company logo
1 of 40
Treat your unit tests as production code
Sandor DARGO (Amadeus)
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 1
Merci aux Sponsors !
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 2
Agenda
Define unit tests
You and unit tests
Test vs Production code
Problems with unit tests
Some solutions (no silver bullet)
Conclusion
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 3
Who am I?
Sándor DARGÓ
Software developer in Amadeus
Enthusiastic blogger http://sandordargo.com
Passionate traveller
Curious home baker and cook (@sourdad.baker)
Happy father of two
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 4
What is a unit test?
- Automated and repeatable
- Written in the language of the code
- Exercises a small part of the program (a unit)
Ideally, they are
- Independent
- Isolated
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 5
What is a (good) unit test?
“A unit test is a piece of code that invokes a unit of work and checks one
specific end result of that unit of work. If the assumptions on the end result
turn out to be wrong, the unit test has failed. A unit test’s scope can span as
little as a method or as much as multiple classes.” - The Art of Unit Testing
by Roy Osherove
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 6
Are you confident in your unit test
suite?
Are they all green?
Are they all executed?
Do they all assert something?
Do they all assert something meaningful?
Do they all assert a single logical thing?
Are they maintained?
Do you delete unit tests?
How frequently do you have to update them?
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 7
Differences between test and
production code
Differences between test and
production code
Form groups
You have 10 minutes to collect them
The pessimist realistic
“Production code is written in a hurry and is most often tested by users
because there wasn't time for QA and you were under pressure to
release.
Who gets time to write test code?”
Different “raison d’etre”
“Production code is is written to be robust (ideally, but not always the
case).”
“Test code is written just as a poc (proof of concept).”
“The difference is in purposes. Production code is for fulfilling the
contract. Test code is for verify this contract.”
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 11
Some more technical differences
“Production code is DRY vs test code is WET.”
“Production code is generic vs test code is specific”
“Production code has to be fast enough, test code must be super fast.”
A tool for write production code
and documentation
“Production code is only what is necessary to get the test code to pass.
And then refactored to make it clean.”
“Test code is easily readable and rather blunt. When someone wants to
know how the system works, you should be able to point them to the
tests that prove the functionality.”
What is common in the previous
answers?
Tests are specific
They document something
Production behaviour
Contract
No mention of poor architecture, poor coding practices. Except for the
WET part.
Why are those differences?
The rest makes sense:
Differences in purposes
Not in implementation details
Do we have a good reason to have worse quality?
Problems with unit tests
Collect your challenges
Form groups
You have 10 minutes to collect them
Common problems with test code
Show its value
Dependency injection
What to test? (public vs private; GUI)
Target code coverage? Does it matter?
TDD or not, test first or not?
Unit test vs integration test?
Test legacy code
Mocking
Common problems with test code
Missing assertions
Multiple assertions
Tests invoking other tests
Logic inside test code
Lack of coverage
Slowness (DB, I/O access)
Dependency between tests
Coupling, fragile test code
Common problems with test code
We have to write them
External dependencies
Fragile test code
Tests? Like production! You have
to write them!
Is it optional?
Client doesn’t want to pay for it?
Manager doesn’t want it?
Don’t know how to write it?
External dependencies
External dependencies
Cannot instantiate code under test
Non-deterministic results
Slow execution time
Cut the external dependencies
DB and IO operations make tests
Dependent
Non-deterministic
Error-prone
Slow
The banana, monkey, jungle
problem
“The problem with object-oriented languages is they’ve got all this
implicit environment that they carry around with them. You wanted a
banana but what you got was a gorilla holding the banana and the
entire jungle.”
Code example
Welcome to the jungle!
class Alarm {
public:
Alarm();
void check();
bool isAlarmOn();
protected:
Sensor m_sensor;
double m_lowPressureTreshold, m_highPressureTreshold;
bool m_alarmOn;
};
double Sensor::popNextPressurePsiValue() { return random();}
Bring your own device
class Alarm {
public:
Alarm(Sensor* sensor, double lowP, double highP);
void check();
bool isAlarmOn();
protected:
Sensor* m_sensor;
double m_lowPressureTreshold, m_highPressureTreshold;
bool m_alarmOn;
};
Fragile test code
Do you have the Fragile Test
Problem?
Test code is highly coupled to production code
If code changes, tests are broken
Cannot refactor
We must keep refactoring
Refactoring is part of TDD
Adding tests after the fact will also require refactoring
What happens when you refactor?
class SUT:
def operation_a(): pass
def operation_b(): pass
class TestSUT(unittest.TestCase):
def test_operation_a(): pass
def test_operation_b(): pass
What happens when you refactor?
class SUT:
def operation_a(): pass
def sub_operation_a(): pass
def operation_b(): pass
def sub_operation_b(): pass
class TestSUT(unittest.TestCase):
def test_operation_a(): pass
def test_operation_b(): pass
def test_sub_operation_a():
pass
def test_sub_operation_b():
pass
What happens when you refactor?
class SUT:
def operation_a(): pass
def operation_b(): pass
def sub_operation_a(): pass
def sub_operation_b(): pas
def common_sub_operation():
pass
class TestSUT(unittest.TestCase):
def test_operation_a(): pass
def test_operation_b(): pass
def test_sub_operation_a(): pass
def test_sub_operation_b(): pass
def test_common_sub_operation():
pass
What happens when you refactor?
class SUT(CommonSUT):
def operation_a(): pass
def operation_b(): pass
def common_sub_operation():
pass
class CommonSUT:
def sub_operation(): pass
class TestSUT(unittest.TestCase):
def test_operation_a(): pass
def test_operation_b(): pass
def test_common_sub_operation():
pass
class TestCommonSUT(unittest.TestCase):
def test_sub_operation(): pass
Test contravariance
“The structure of your tests should not be a mirror of the structure of
your code. The fact that you have a class named X should not
automatically imply that you have a test class named XTest.” - Uncle
Bob
What else can you do?
Test behavior
Don’t mimic production code structure
Let test code evolve on its own
Don’t be a micromanager
Should we test private?
No, only the API
Should we test every single aspect?
Don’t test every bits of implementation
Conclusion
Unit testing is difficult, but learnable
Different aims, similar best practices from production code
Unit testing is not optional
Maintainable UTs go down to coupling
References
Code Simplicity - Max Kanat-Alexander
The Art of Unit Testing - Roy Osherove
Culture Code
https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html
https://hackernoon.com/how-to-deal-with-test-and-production-code-
c64acd9a062
https://painlessdesign.wordpress.com/2015/06/27/how-to-avoid-fragile-unit-
tests/
https://www.developer.com/tech/5-simple-tips-to-more-robust-unit-tests.html
https://enterprisecraftsmanship.com/posts/unit-testing-private-methods/
Treat your unit tests as production code
Sandor DARGO (Amadeus)
17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 40

More Related Content

Recently uploaded

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...Jittipong Loespradit
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationJuha-Pekka Tolvanen
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2
 
tonesoftg
tonesoftgtonesoftg
tonesoftglanshi9
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benonimasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 

Recently uploaded (20)

WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
WSO2CON 2024 - WSO2's Digital Transformation Journey with Choreo: A Platforml...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 

Featured

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 

Featured (20)

Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 

Treat Your Unit Tests As Production Code at Soiree Du Test Logiciel 2019

  • 1. Treat your unit tests as production code Sandor DARGO (Amadeus) 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 1
  • 2. Merci aux Sponsors ! 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 2
  • 3. Agenda Define unit tests You and unit tests Test vs Production code Problems with unit tests Some solutions (no silver bullet) Conclusion 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 3
  • 4. Who am I? Sándor DARGÓ Software developer in Amadeus Enthusiastic blogger http://sandordargo.com Passionate traveller Curious home baker and cook (@sourdad.baker) Happy father of two 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 4
  • 5. What is a unit test? - Automated and repeatable - Written in the language of the code - Exercises a small part of the program (a unit) Ideally, they are - Independent - Isolated 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 5
  • 6. What is a (good) unit test? “A unit test is a piece of code that invokes a unit of work and checks one specific end result of that unit of work. If the assumptions on the end result turn out to be wrong, the unit test has failed. A unit test’s scope can span as little as a method or as much as multiple classes.” - The Art of Unit Testing by Roy Osherove 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 6
  • 7. Are you confident in your unit test suite? Are they all green? Are they all executed? Do they all assert something? Do they all assert something meaningful? Do they all assert a single logical thing? Are they maintained? Do you delete unit tests? How frequently do you have to update them? 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 7
  • 8. Differences between test and production code
  • 9. Differences between test and production code Form groups You have 10 minutes to collect them
  • 10. The pessimist realistic “Production code is written in a hurry and is most often tested by users because there wasn't time for QA and you were under pressure to release. Who gets time to write test code?”
  • 11. Different “raison d’etre” “Production code is is written to be robust (ideally, but not always the case).” “Test code is written just as a poc (proof of concept).” “The difference is in purposes. Production code is for fulfilling the contract. Test code is for verify this contract.” 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 11
  • 12. Some more technical differences “Production code is DRY vs test code is WET.” “Production code is generic vs test code is specific” “Production code has to be fast enough, test code must be super fast.”
  • 13. A tool for write production code and documentation “Production code is only what is necessary to get the test code to pass. And then refactored to make it clean.” “Test code is easily readable and rather blunt. When someone wants to know how the system works, you should be able to point them to the tests that prove the functionality.”
  • 14. What is common in the previous answers? Tests are specific They document something Production behaviour Contract No mention of poor architecture, poor coding practices. Except for the WET part.
  • 15. Why are those differences? The rest makes sense: Differences in purposes Not in implementation details Do we have a good reason to have worse quality?
  • 17. Collect your challenges Form groups You have 10 minutes to collect them
  • 18. Common problems with test code Show its value Dependency injection What to test? (public vs private; GUI) Target code coverage? Does it matter? TDD or not, test first or not? Unit test vs integration test? Test legacy code Mocking
  • 19. Common problems with test code Missing assertions Multiple assertions Tests invoking other tests Logic inside test code Lack of coverage Slowness (DB, I/O access) Dependency between tests Coupling, fragile test code
  • 20. Common problems with test code We have to write them External dependencies Fragile test code
  • 21. Tests? Like production! You have to write them!
  • 22. Is it optional? Client doesn’t want to pay for it? Manager doesn’t want it? Don’t know how to write it?
  • 24. External dependencies Cannot instantiate code under test Non-deterministic results Slow execution time
  • 25. Cut the external dependencies DB and IO operations make tests Dependent Non-deterministic Error-prone Slow
  • 26. The banana, monkey, jungle problem “The problem with object-oriented languages is they’ve got all this implicit environment that they carry around with them. You wanted a banana but what you got was a gorilla holding the banana and the entire jungle.”
  • 27. Code example Welcome to the jungle! class Alarm { public: Alarm(); void check(); bool isAlarmOn(); protected: Sensor m_sensor; double m_lowPressureTreshold, m_highPressureTreshold; bool m_alarmOn; }; double Sensor::popNextPressurePsiValue() { return random();} Bring your own device class Alarm { public: Alarm(Sensor* sensor, double lowP, double highP); void check(); bool isAlarmOn(); protected: Sensor* m_sensor; double m_lowPressureTreshold, m_highPressureTreshold; bool m_alarmOn; };
  • 29. Do you have the Fragile Test Problem? Test code is highly coupled to production code If code changes, tests are broken Cannot refactor
  • 30. We must keep refactoring Refactoring is part of TDD Adding tests after the fact will also require refactoring
  • 31. What happens when you refactor? class SUT: def operation_a(): pass def operation_b(): pass class TestSUT(unittest.TestCase): def test_operation_a(): pass def test_operation_b(): pass
  • 32. What happens when you refactor? class SUT: def operation_a(): pass def sub_operation_a(): pass def operation_b(): pass def sub_operation_b(): pass class TestSUT(unittest.TestCase): def test_operation_a(): pass def test_operation_b(): pass def test_sub_operation_a(): pass def test_sub_operation_b(): pass
  • 33. What happens when you refactor? class SUT: def operation_a(): pass def operation_b(): pass def sub_operation_a(): pass def sub_operation_b(): pas def common_sub_operation(): pass class TestSUT(unittest.TestCase): def test_operation_a(): pass def test_operation_b(): pass def test_sub_operation_a(): pass def test_sub_operation_b(): pass def test_common_sub_operation(): pass
  • 34. What happens when you refactor? class SUT(CommonSUT): def operation_a(): pass def operation_b(): pass def common_sub_operation(): pass class CommonSUT: def sub_operation(): pass class TestSUT(unittest.TestCase): def test_operation_a(): pass def test_operation_b(): pass def test_common_sub_operation(): pass class TestCommonSUT(unittest.TestCase): def test_sub_operation(): pass
  • 35. Test contravariance “The structure of your tests should not be a mirror of the structure of your code. The fact that you have a class named X should not automatically imply that you have a test class named XTest.” - Uncle Bob
  • 36. What else can you do? Test behavior Don’t mimic production code structure Let test code evolve on its own
  • 37. Don’t be a micromanager Should we test private? No, only the API Should we test every single aspect? Don’t test every bits of implementation
  • 38. Conclusion Unit testing is difficult, but learnable Different aims, similar best practices from production code Unit testing is not optional Maintainable UTs go down to coupling
  • 39. References Code Simplicity - Max Kanat-Alexander The Art of Unit Testing - Roy Osherove Culture Code https://blog.cleancoder.com/uncle-bob/2017/10/03/TestContravariance.html https://hackernoon.com/how-to-deal-with-test-and-production-code- c64acd9a062 https://painlessdesign.wordpress.com/2015/06/27/how-to-avoid-fragile-unit- tests/ https://www.developer.com/tech/5-simple-tips-to-more-robust-unit-tests.html https://enterprisecraftsmanship.com/posts/unit-testing-private-methods/
  • 40. Treat your unit tests as production code Sandor DARGO (Amadeus) 17/10/19 3ème édition Soirée du Test Logiciel Sophia #STLS2019 40