SlideShare a Scribd company logo
1 of 19
Confidential & Proprietary
www.leadingedje.com
What to Avoid
When Writing
Unit Tests
Bob Fornal
“These tests should never have been
written. They provide no or little
value.” -ME
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
A QA Engineer walks into a bar.
● Orders a beer.
● Orders 0 beers.
● Orders 999,999,999,999 beers.
● Orders a lizard.
● Orders -1 beers.
● Orders a ueicbksjdhd.
The first real customer walks in and asks where the
bathroom is. The bar bursts into flames, killing
everyone.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
What Are Unit Tests?
● Unit Tests exercise small parts of the application (code-under-test).
● … in complete isolation.
● … actual behavior versus expected behavior.
● Unit Tests should be fast, simple, and stable.
● Unit Tests reflect the specifications.
● … can act as documentation.
● Unit Tests are a safety net.
● … provide immediate feedback about code changes.
● … find and fix bugs earlier.
● … contribute to higher code quality and better architecture.
● … faster detection of code smells.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Talk Details
Code Repository: https://github.com/bob-fornal/what-to-avoid-when-writing-unit-tests
I can be found at …
● https://linqapp.com/conference
● Twitter: @rfornal
● Articles https://dev.to/rfornal
● LinkedIn: https://www.linkedin.com/in/rfornal/
Social Media Project …
#100DaysOfCode or #100Devs … in one of these Twitter threads, pick three people in
your field that haven’t had a response and cheer them on.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Testing code has been
described as an "art form."
It is, but it should not be.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Tests Should Not Be "Well-Factored"
Let’s examine some examples …
● Keep the Reader in the test.
● Dare to violate the DRY Principle.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Keep the Reader in the Test
Given
A function that returns a score.
Problem
The unit test has has some part of it abstracted in a way that makes reading the test
difficult.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Dare to Violate the DRY Principle
“Duplication of Code Logic”
Given
A function that adjusts a score.
Problem
The unit test has has some part of it abstracted in a way that makes reading the test
difficult.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Poisoning the Codebase: Non-Deterministic Factors
Given
A function that returns a category, the time of day.
Problem
The function under test is tightly coupled to a concrete data source, violating the
Single Responsibility Principle. The test cannot easily test the functionality.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Poisoning the Codebase: Side-Effects
Given
A function that updates the time of last motion and triggers something off or on.
Problem
The function under test has too many side effects that become hard to test.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Testing Too Much
Given
A function that does too many things.
Problem
The function under test has too possible paths to handle easily.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Bad Test Double
“Testing the Mock”
Given
A service is used to get some data.
Problem
Replacing the service (test double) does not correctly take into account changes in
the service or returned data.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
False Positives
Given
A function that processes some time delay.
Problem
The unit test does not get to the code inside the setTimeout and “does not fail” which
equates to a pass.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Excessive Setup
Given
A unit test that take a lot of setup code to run.
Problem
Excessive setup is more of a CODE SMELL than something where code can be shown
that is incorrect versus correct.
Just be aware that this is a case where care should be taken to examine why the
setup is so lengthy.
Solution
● If there is a legitimate need, increase documentation.
● If the code under test does not adhere to the Single Responsibility Principle,
maybe a refactor would be appropriate to make it more testable.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Testing Private Functionality Directly
Given
A class with private functionality that is difficult to test indirectly.
Problem
How do we test private functionality.
Solution
1. Indirectly
2. Make it public, directly.
3. Abstract it into a new service, directly.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Unit Recommendations
1. Unit Tests should be Readable.
2. One Assert per Test Method.
3. Avoid Test Interdependence.
4. Keep it Short, Sweet, and Visible.
5. Add them to the Build.
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
Confidential & Proprietary
www.leadingedje.com
What to Avoid When
Writing Unit Tests

More Related Content

Similar to What to Avoid When Writing Unit Tests - Robert Fornal

Automating good coding practices
Automating good coding practicesAutomating good coding practices
Automating good coding practicesKevin Peterson
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...FalafelSoftware
 
Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Van Huong
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flexmichael.labriola
 
Agile principles and practices
Agile principles and practicesAgile principles and practices
Agile principles and practicesVipin Jose
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular SlidesJim Lynch
 
Behaviour Driven Development: Oltre i limiti del possibile
Behaviour Driven Development: Oltre i limiti del possibileBehaviour Driven Development: Oltre i limiti del possibile
Behaviour Driven Development: Oltre i limiti del possibileIosif Itkin
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Testsradin reth
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?Dmitriy Nesteryuk
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildAndrei Sebastian Cîmpean
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven DevelopmentAmir Barylko
 
Write testable code in java, best practices
Write testable code in java, best practicesWrite testable code in java, best practices
Write testable code in java, best practicesMarian Wamsiedel
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the UntestableMark Baker
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy CodeEyal Golan
 
Implementing Test Automation in Agile Projects
Implementing Test Automation in Agile ProjectsImplementing Test Automation in Agile Projects
Implementing Test Automation in Agile ProjectsMichael Palotas
 
How to test a Mainframe Application
How to test a Mainframe ApplicationHow to test a Mainframe Application
How to test a Mainframe ApplicationMichael Erichsen
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit TestingSadaaki Emura
 

Similar to What to Avoid When Writing Unit Tests - Robert Fornal (20)

Automating good coding practices
Automating good coding practicesAutomating good coding practices
Automating good coding practices
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
 
TDD and Getting Paid
TDD and Getting PaidTDD and Getting Paid
TDD and Getting Paid
 
Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Agile principles and practices
Agile principles and practicesAgile principles and practices
Agile principles and practices
 
Automated Testing in Angular Slides
Automated Testing in Angular SlidesAutomated Testing in Angular Slides
Automated Testing in Angular Slides
 
Behaviour Driven Development: Oltre i limiti del possibile
Behaviour Driven Development: Oltre i limiti del possibileBehaviour Driven Development: Oltre i limiti del possibile
Behaviour Driven Development: Oltre i limiti del possibile
 
Clean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit TestsClean code: understanding Boundaries and Unit Tests
Clean code: understanding Boundaries and Unit Tests
 
TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?TDD — Are you sure you properly test code?
TDD — Are you sure you properly test code?
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven Development
 
Test Automation
Test AutomationTest Automation
Test Automation
 
Write testable code in java, best practices
Write testable code in java, best practicesWrite testable code in java, best practices
Write testable code in java, best practices
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
Working with Legacy Code
Working with Legacy CodeWorking with Legacy Code
Working with Legacy Code
 
Implementing Test Automation in Agile Projects
Implementing Test Automation in Agile ProjectsImplementing Test Automation in Agile Projects
Implementing Test Automation in Agile Projects
 
How to test a Mainframe Application
How to test a Mainframe ApplicationHow to test a Mainframe Application
How to test a Mainframe Application
 
What is Unit Testing
What is Unit TestingWhat is Unit Testing
What is Unit Testing
 

More from QA or the Highway

KrishnaToolComparisionPPT.pdf
KrishnaToolComparisionPPT.pdfKrishnaToolComparisionPPT.pdf
KrishnaToolComparisionPPT.pdfQA or the Highway
 
Ravi Lakkavalli - World Quality Report.pptx
Ravi Lakkavalli - World Quality Report.pptxRavi Lakkavalli - World Quality Report.pptx
Ravi Lakkavalli - World Quality Report.pptxQA or the Highway
 
Caleb Crandall - Testing Between the Buckets.pptx
Caleb Crandall - Testing Between the Buckets.pptxCaleb Crandall - Testing Between the Buckets.pptx
Caleb Crandall - Testing Between the Buckets.pptxQA or the Highway
 
Thomas Haver - Mobile Testing.pdf
Thomas Haver - Mobile Testing.pdfThomas Haver - Mobile Testing.pdf
Thomas Haver - Mobile Testing.pdfQA or the Highway
 
Thomas Haver - Example Mapping.pdf
Thomas Haver - Example Mapping.pdfThomas Haver - Example Mapping.pdf
Thomas Haver - Example Mapping.pdfQA or the Highway
 
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdfJoe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdfQA or the Highway
 
Sarah Geisinger - Continious Testing Metrics That Matter.pdf
Sarah Geisinger - Continious Testing Metrics That Matter.pdfSarah Geisinger - Continious Testing Metrics That Matter.pdf
Sarah Geisinger - Continious Testing Metrics That Matter.pdfQA or the Highway
 
Jeff Sing - Quarterly Service Delivery Reviews.pdf
Jeff Sing - Quarterly Service Delivery Reviews.pdfJeff Sing - Quarterly Service Delivery Reviews.pdf
Jeff Sing - Quarterly Service Delivery Reviews.pdfQA or the Highway
 
Leandro Melendez - Chihuahua Load Tests.pdf
Leandro Melendez - Chihuahua Load Tests.pdfLeandro Melendez - Chihuahua Load Tests.pdf
Leandro Melendez - Chihuahua Load Tests.pdfQA or the Highway
 
Rick Clymer - Incident Management.pdf
Rick Clymer - Incident Management.pdfRick Clymer - Incident Management.pdf
Rick Clymer - Incident Management.pdfQA or the Highway
 
Robert Fornal - ChatGPT as a Testing Tool.pptx
Robert Fornal - ChatGPT as a Testing Tool.pptxRobert Fornal - ChatGPT as a Testing Tool.pptx
Robert Fornal - ChatGPT as a Testing Tool.pptxQA or the Highway
 
Federico Toledo - Extra-functional testing.pdf
Federico Toledo - Extra-functional testing.pdfFederico Toledo - Extra-functional testing.pdf
Federico Toledo - Extra-functional testing.pdfQA or the Highway
 
Andrew Knight - Managing the Test Data Nightmare.pptx
Andrew Knight - Managing the Test Data Nightmare.pptxAndrew Knight - Managing the Test Data Nightmare.pptx
Andrew Knight - Managing the Test Data Nightmare.pptxQA or the Highway
 
Melissa Tondi - Automation We_re Doing it Wrong.pdf
Melissa Tondi - Automation We_re Doing it Wrong.pdfMelissa Tondi - Automation We_re Doing it Wrong.pdf
Melissa Tondi - Automation We_re Doing it Wrong.pdfQA or the Highway
 
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdfJeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdfQA or the Highway
 
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptxDesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptxQA or the Highway
 
Damian Synadinos - Word Smatter.pdf
Damian Synadinos - Word Smatter.pdfDamian Synadinos - Word Smatter.pdf
Damian Synadinos - Word Smatter.pdfQA or the Highway
 
Lee Barnes - What Successful Test Automation is.pdf
Lee Barnes - What Successful Test Automation is.pdfLee Barnes - What Successful Test Automation is.pdf
Lee Barnes - What Successful Test Automation is.pdfQA or the Highway
 
Jordan Powell - API Testing with Cypress.pptx
Jordan Powell - API Testing with Cypress.pptxJordan Powell - API Testing with Cypress.pptx
Jordan Powell - API Testing with Cypress.pptxQA or the Highway
 
Carlos Kidman - Exploring AI Applications in Testing.pptx
Carlos Kidman - Exploring AI Applications in Testing.pptxCarlos Kidman - Exploring AI Applications in Testing.pptx
Carlos Kidman - Exploring AI Applications in Testing.pptxQA or the Highway
 

More from QA or the Highway (20)

KrishnaToolComparisionPPT.pdf
KrishnaToolComparisionPPT.pdfKrishnaToolComparisionPPT.pdf
KrishnaToolComparisionPPT.pdf
 
Ravi Lakkavalli - World Quality Report.pptx
Ravi Lakkavalli - World Quality Report.pptxRavi Lakkavalli - World Quality Report.pptx
Ravi Lakkavalli - World Quality Report.pptx
 
Caleb Crandall - Testing Between the Buckets.pptx
Caleb Crandall - Testing Between the Buckets.pptxCaleb Crandall - Testing Between the Buckets.pptx
Caleb Crandall - Testing Between the Buckets.pptx
 
Thomas Haver - Mobile Testing.pdf
Thomas Haver - Mobile Testing.pdfThomas Haver - Mobile Testing.pdf
Thomas Haver - Mobile Testing.pdf
 
Thomas Haver - Example Mapping.pdf
Thomas Haver - Example Mapping.pdfThomas Haver - Example Mapping.pdf
Thomas Haver - Example Mapping.pdf
 
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdfJoe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
Joe Colantonio - Actionable Automation Awesomeness in Testing Farm.pdf
 
Sarah Geisinger - Continious Testing Metrics That Matter.pdf
Sarah Geisinger - Continious Testing Metrics That Matter.pdfSarah Geisinger - Continious Testing Metrics That Matter.pdf
Sarah Geisinger - Continious Testing Metrics That Matter.pdf
 
Jeff Sing - Quarterly Service Delivery Reviews.pdf
Jeff Sing - Quarterly Service Delivery Reviews.pdfJeff Sing - Quarterly Service Delivery Reviews.pdf
Jeff Sing - Quarterly Service Delivery Reviews.pdf
 
Leandro Melendez - Chihuahua Load Tests.pdf
Leandro Melendez - Chihuahua Load Tests.pdfLeandro Melendez - Chihuahua Load Tests.pdf
Leandro Melendez - Chihuahua Load Tests.pdf
 
Rick Clymer - Incident Management.pdf
Rick Clymer - Incident Management.pdfRick Clymer - Incident Management.pdf
Rick Clymer - Incident Management.pdf
 
Robert Fornal - ChatGPT as a Testing Tool.pptx
Robert Fornal - ChatGPT as a Testing Tool.pptxRobert Fornal - ChatGPT as a Testing Tool.pptx
Robert Fornal - ChatGPT as a Testing Tool.pptx
 
Federico Toledo - Extra-functional testing.pdf
Federico Toledo - Extra-functional testing.pdfFederico Toledo - Extra-functional testing.pdf
Federico Toledo - Extra-functional testing.pdf
 
Andrew Knight - Managing the Test Data Nightmare.pptx
Andrew Knight - Managing the Test Data Nightmare.pptxAndrew Knight - Managing the Test Data Nightmare.pptx
Andrew Knight - Managing the Test Data Nightmare.pptx
 
Melissa Tondi - Automation We_re Doing it Wrong.pdf
Melissa Tondi - Automation We_re Doing it Wrong.pdfMelissa Tondi - Automation We_re Doing it Wrong.pdf
Melissa Tondi - Automation We_re Doing it Wrong.pdf
 
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdfJeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
Jeff Van Fleet and John Townsend - Transition from Testing to Leadership.pdf
 
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptxDesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
DesiradhaRam Gadde - Testers _ Testing in ChatGPT-AI world.pptx
 
Damian Synadinos - Word Smatter.pdf
Damian Synadinos - Word Smatter.pdfDamian Synadinos - Word Smatter.pdf
Damian Synadinos - Word Smatter.pdf
 
Lee Barnes - What Successful Test Automation is.pdf
Lee Barnes - What Successful Test Automation is.pdfLee Barnes - What Successful Test Automation is.pdf
Lee Barnes - What Successful Test Automation is.pdf
 
Jordan Powell - API Testing with Cypress.pptx
Jordan Powell - API Testing with Cypress.pptxJordan Powell - API Testing with Cypress.pptx
Jordan Powell - API Testing with Cypress.pptx
 
Carlos Kidman - Exploring AI Applications in Testing.pptx
Carlos Kidman - Exploring AI Applications in Testing.pptxCarlos Kidman - Exploring AI Applications in Testing.pptx
Carlos Kidman - Exploring AI Applications in Testing.pptx
 

Recently uploaded

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 

Recently uploaded (20)

AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 

What to Avoid When Writing Unit Tests - Robert Fornal

  • 1. Confidential & Proprietary www.leadingedje.com What to Avoid When Writing Unit Tests Bob Fornal “These tests should never have been written. They provide no or little value.” -ME
  • 2. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com A QA Engineer walks into a bar. ● Orders a beer. ● Orders 0 beers. ● Orders 999,999,999,999 beers. ● Orders a lizard. ● Orders -1 beers. ● Orders a ueicbksjdhd. The first real customer walks in and asks where the bathroom is. The bar bursts into flames, killing everyone.
  • 3. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com What Are Unit Tests? ● Unit Tests exercise small parts of the application (code-under-test). ● … in complete isolation. ● … actual behavior versus expected behavior. ● Unit Tests should be fast, simple, and stable. ● Unit Tests reflect the specifications. ● … can act as documentation. ● Unit Tests are a safety net. ● … provide immediate feedback about code changes. ● … find and fix bugs earlier. ● … contribute to higher code quality and better architecture. ● … faster detection of code smells.
  • 4. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Talk Details Code Repository: https://github.com/bob-fornal/what-to-avoid-when-writing-unit-tests I can be found at … ● https://linqapp.com/conference ● Twitter: @rfornal ● Articles https://dev.to/rfornal ● LinkedIn: https://www.linkedin.com/in/rfornal/ Social Media Project … #100DaysOfCode or #100Devs … in one of these Twitter threads, pick three people in your field that haven’t had a response and cheer them on.
  • 5. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Testing code has been described as an "art form." It is, but it should not be.
  • 7. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Tests Should Not Be "Well-Factored" Let’s examine some examples … ● Keep the Reader in the test. ● Dare to violate the DRY Principle.
  • 8. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Keep the Reader in the Test Given A function that returns a score. Problem The unit test has has some part of it abstracted in a way that makes reading the test difficult.
  • 9. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Dare to Violate the DRY Principle “Duplication of Code Logic” Given A function that adjusts a score. Problem The unit test has has some part of it abstracted in a way that makes reading the test difficult.
  • 10. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Poisoning the Codebase: Non-Deterministic Factors Given A function that returns a category, the time of day. Problem The function under test is tightly coupled to a concrete data source, violating the Single Responsibility Principle. The test cannot easily test the functionality.
  • 11. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Poisoning the Codebase: Side-Effects Given A function that updates the time of last motion and triggers something off or on. Problem The function under test has too many side effects that become hard to test.
  • 12. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Testing Too Much Given A function that does too many things. Problem The function under test has too possible paths to handle easily.
  • 13. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Bad Test Double “Testing the Mock” Given A service is used to get some data. Problem Replacing the service (test double) does not correctly take into account changes in the service or returned data.
  • 14. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com False Positives Given A function that processes some time delay. Problem The unit test does not get to the code inside the setTimeout and “does not fail” which equates to a pass.
  • 15. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Excessive Setup Given A unit test that take a lot of setup code to run. Problem Excessive setup is more of a CODE SMELL than something where code can be shown that is incorrect versus correct. Just be aware that this is a case where care should be taken to examine why the setup is so lengthy. Solution ● If there is a legitimate need, increase documentation. ● If the code under test does not adhere to the Single Responsibility Principle, maybe a refactor would be appropriate to make it more testable.
  • 16. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Testing Private Functionality Directly Given A class with private functionality that is difficult to test indirectly. Problem How do we test private functionality. Solution 1. Indirectly 2. Make it public, directly. 3. Abstract it into a new service, directly.
  • 17. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com Unit Recommendations 1. Unit Tests should be Readable. 2. One Assert per Test Method. 3. Avoid Test Interdependence. 4. Keep it Short, Sweet, and Visible. 5. Add them to the Build.
  • 19. Confidential & Proprietary www.leadingedje.com Confidential & Proprietary www.leadingedje.com What to Avoid When Writing Unit Tests

Editor's Notes

  1. Good Unit Tests are a critical part of the development process and vital to the lifecycle of the code-under-test. Previous employer … No tests, there wasn’t enough time. Flaky code. Didn’t stand the test of time. Another … Script language story 180,000 lines of code with 97% Unit Test Coverage Unit Tests in-depth. Used TDD when bugs found before release. 3 Bug Cards in 365-days, two were missed requirement issues. Others … 2,000+ Unit Tests against Promises (JavaScript) that passed, even when the tests should have failed. No or limited Unit Testing because of monolithic code (3-6,000 line functions). Let me reiterate, Good Unit Tests are a critical part of the development process and vital to the lifecycle of the code-under-test.
  2. Unit tests exercise very small parts of the application in complete isolation They comparing the code-under-test’s actual behavior with the expected behavior. The “complete isolation” part means that, when unit testing, you don’t typically connect your application with external dependencies such as databases, the filesystem, or HTTP services. That allows unit tests to be fast and more stable since they won’t fail due to problems with those external services. Each unit test is like a specification or example of how that part behaves in a specific scenario. By executing the suite of tests, developers can get immediate feedback when they change some codebase.
  3. Testing code has been described as an "art form." It is, but it should not be. There are many good patterns that should be followed when writing tests. There are also many BAD patterns that developers should be aware of so that they can be avoided (or cleaned up).
  4. Good production code achieves encapsulation. It allows the reader to navigate large systems with ease, diving down into the details or rising to a higher level of abstraction, as needed. Test code is a different beast. A good unit test is often small enough that a developer can conceptualize all the logic at one time. Adding layers of abstraction to test code increases its complexity. Tests are a diagnostic tool, so they should be as simple and obvious as possible.
  5. Well-Factored Code. Code that is easy to read and reason about. Code that can be tested easily. Well Designed and often has a high level of complex.
  6. Think of rulers. They have existed in the same form for hundreds of years. They are uncomplicated and easy to interpret. Suppose we invented a new ruler that measured in “abstract ruler units.” To convert from “ruler units” to inches or centimeters, you would use a separate conversion chart. If I offered such a ruler to a carpenter, they’d smack me in the face with it. This layer of abstraction is unnecessary to a tool that gives clear, unambiguous information. THE BROKEN CODE HERE IS NOT A BAD TEST When you write a test, think about the next developer who will see the test break. They don’t want to read your entire test suite, and they certainly don’t want to read a whole inheritance tree of test utilities. If a test breaks, the reader should be able to diagnose the problem by reading the test function in a straight line from top to bottom. If they have to jump out of the test to read ancillary code, the test has not done its job.
  7. For strict adherents to the principle of DRY (“don’t repeat yourself”), the code we will see is horrifying. We blatantly repeat ourselves; we copied lines from the previous test verbatim. Worse, I’m arguing that my DRY-violating tests are better than tests that are free of repeated code. How can this be? If you can achieve clear tests without duplicating code, that’s ideal, but remember that nonredundant code is the means, not the ends. The end goal is clear, simple tests. Before blindly applying DRY to your tests, think about what will make the problem obvious when a test fails. Refactoring may reduce duplication, but it also increases complexity and potentially obscures information when things break.
  8. In this case, tightly coupled data can make testing challenging. Assumption that the code-under-test uses the current date to generate a string value. The current date is a concrete data source. The code gets the date AND generates a string based on the date.
  9. Assume a class that turns light on or off at certain times OR with motion. Tight coupling: calculated date and time. Tied directly to an additional Switch class.
  10. Often, functions have switch statements or if-branching and as they get larger the code is not refactored leaving too many paths through to test easily.
  11. This is one that I see all the time. A mock service is created that allows functionality to be “mocked.” In reality, we are testing the mock in this case. A better pattern is to create a mock service FROM the original service being injected. Then add mock data or spy on functionality from within the service.
  12. These occur when … Forgetting to EXPECT (some test suites warn of this). When testing ASYNCHRONOUS activity. Promises that DON’T FAIL. Jasmine used to use DONE … DONE has become unreliable in certain scenarios (see the commented out code). JavaScript allows for async/await and try/catch. These are better patterns and more readable, as well.
  13. This is a challenging one to show examples of … There is code. Let’s simply talk about the issues. This is more of a code smell … that something can be wrong in the test setup or in the code-under-test. Code-under-test should be examined to see if a refactor can help. There can be legitimate needs for larger setups.
  14. There is a challenging one to show a FIXED version of … There is code. Private code can be tested via exposed functionality. Private code can be made public (JavaScript: everything is accessible at some level). Private code can be abstracted into a service … The code has to be public for injection. The code can be tested directly since it is public.
  15. I get asked about the first one often. Even in my tests, I assert more than once. I recommend checking to see if the functionality can be refactored to allow for ONE TEST per METHOD. Test Order shouldn’t matter. Unit Tests absolutely should be a part of the pipeline.
  16. <!-- HTML Credit Code for Can Stock Photo --> <a href="https://www.canstockphoto.com">(c) Can Stock Photo / catalby</a>