SlideShare a Scribd company logo
Unit Testing
Chris Jimenez
What is a Unit test?
Unit testing is testing of units in your code
base
“
The unit in question could be a Class
or a Method...
What is NOT a unit test???
◦ Developer Testing. Compiling
and running the application to
see if it does what you want it
to do is not unit testing.
What is NOT a unit test???
◦ Smoke/ Quality Testing. Running
an automated test that chugs
through a laundry list of
procedures involving large
sections of your code base is
not a unit test. This is the way
the QA department does
testing.
What is NOT a unit test???
◦ Things that involve databases,
files, web services, device
drivers or other things that are
external to your code. These are
called integration tests and they
test a lot more than just your
code.
“
The fact that something is
automated does not make it a unit
test
What is the purpose of Unit Testing?
Testing is, at its core,
experimentation. We’re just so
used to the hypothesis that our
code will work and that tests will
confirm that to be the case. Or
NOT!
What is the purpose of Unit Testing?
Testing at the unit level is about
running experiments on classes
and methods and capturing those
experiments and their results via
code.
What is the purpose of Unit Testing?
So unit testing is part of quality
assurance, but it isn’t itself quality
assurance, per se. Rather, it’s a
way of documenting and locking in
the behavior of the finest-grained
units of your code – classes – in
isolation.
What is the purpose of Unit Testing?
By understanding exactly how all
of the smallest units of your code
behave, it is straightforward to
assemble them predictably into
larger and larger components and
thus construct a well-designed
system.
Some Basic
Best Practices
and Definitions
Basic Principles
◦ Unit tests are just methods that
are decorated with annotations
(Java) or attributes (C#) to
indicate to the unit test runner
that they are unit tests. Each
method is generally a test.
◦ A unit test runner is simply a
program that executes
compiled test code and
provides feedback on the
results.
Basic Principles
◦ Tests will generally either pass
or fail, but they might also time
out or be inconclusive
◦ Tests will generally either pass
or fail, but they might also time
out or be inconclusive,
depending on the tooling that
you use.
Basic Principles
◦ Unit tests are simple and
localized. They should not
involve multi-threading, file I/ O,
database connections, web
services, etc. If you’re writing
these things, you’re not writing
unit tests but rather integration
tests.
Basic Principles
◦ Unit tests are fast. Your entire
unit test suite should execute in
seconds or quicker.
◦ Unit tests run in isolation, and
the order in which they’re run
should not matter at all. If test
one has to run before test two,
you’re not writing unit tests.
Basic Principles
◦ Unit tests should not involve
setting or depending on global
application state such as public,
static, stateful methods,
singletons, etc.
Arrange, Act,
Assert.
Or AAA
Arrange
What this means is that you start
off the test by setting up the class
instance in the state you want it
(usually by instantiating it and
then whatever else you want),
Act
Executing your test
Assert
Verify that what happened is what
you expected to happen.
Lets write our
first Unit Test
WOW
◦ Kind of a dense looking code
there…
◦ How do we know if this works?
◦ Run the entire application to see
if its blows up?
◦ But this is a pretty simple
method in a pretty simple class.
Doesn’t it seem like there ought
to be a way to make sure it
works?
But never fear
◦ Kind of a dense looking code
there…
◦ How do we know if this works?
◦ Run the entire application to see
if its blows up?
◦ But this is a pretty simple
method in a pretty simple class.
Doesn’t it seem like there ought
to be a way to make sure it
works?
But never fear
Why do we want to do this?
◦ Well, might be that you or
someone else later starts
playing around with the
implementation of IsPrime().
◦ Maybe you want to make it
faster.
◦ Maybe you realize it doesn’t
handle negative numbers
properly and aim to correct it.
◦ ETC!
Donts
Donts
◦ Units test are granular!
◦ If something fails, you dont
know what
Donts
◦ Units test are granular!
◦ If something fails, you dont
know what!
“
“Unit tests don’t just magically
spring up like mushrooms after a
few days of rain. They’re more like
roses – you have to plan for them
from the start and carefully
cultivate an environment in which
they can thrive.”
How to get started!
◦ Test New Classes Only
◦ Test Existing Code by Extracting
Little Classes
◦ Just do it!
Things that are hard to test
◦ Calls static methods.
(is that they manipulate some kind
of global state.)
◦ Invokes singletons .
◦ Dispatches background workers
◦ Accesses files, connects to
databases, calls web services,
etc.
◦ Has classes that require crazy
amounts of instantiation.
Things that are hard to test
◦ For now just stay away from all
of these...
Design New
Code for
Testability
Designs that are bad for testing
◦ Active Record
This architecture tightly couples
your database to your domain
logic and your domain logic to
the rules for navigating through
domain objects.
◦ WinForms / Webforms / Code
behind stuff
Monolithic Code
◦ Picture two methods. One is a
700-line juggernaut with more
control flow statements than
you can keep track of without a
spreadsheet. The other is five
lines long, consisting only of an
initialize statement, a foreach,
and a return statement. With
which would you rather work?
Procedural Code
Procedural Code
◦ Stay away from this sort of
Code
◦ Unit testing is about
deconstructing things into
smaller possible chunks
Procedural Code
◦ Stop thinking in when and start
thinking in what
◦ If you were thinking about
“what” first here, you would
form a much different mental
model of a person’s day. You’d
say things to yourself like, “well,
during the course of a person’s
day, he probably wakes up, gets
dressed, eats breakfast – well,
actually eats one or more meals
Other Design Considerations for Your
New Classes
◦ Avoid using fields to
communicate between your
methods by setting flags and
tracking state. Favor having
methods that can be executed
at any time and in any order.
◦ Don’t instantiate things in your
constructor. Favor passing them
in.
Other Design Considerations for Your
New Classes
◦ Similarly, don’t have a lot of
code or do a lot of work in your
constructor. This will make your
class painful to setup for test.
◦ In your methods, accept
parameters that are as
decomposed as possible. For
instance, don’t accept a
Customer object if all you do
with it is read its SSN property.
The earlier you
start writing
your unit tests,
the better.
Other examples
Other examples
Still dont want to Unit Test?
◦ Improve code quality
◦ Document the code
◦ Exercise Code
◦ Promote good design
◦ Help break problems into
manageable chunks
◦ Expose problems eairler
◦ Its cheaper to find problems
◦ Allow easier refactoring without
hesitation
Still dont want to Unit Test?
◦ Improve code quality
◦ Document the code
◦ Exercise Code
◦ Promote good design
◦ Help break problems into
manageable chunks
◦ Expose problems eairler
◦ Its cheaper to find problems
◦ Allow easier refactoring without
hesitation
Still dont want to Unit Test?
◦ Its easier to write code with unit
tests
Other considerations
◦ Name Your Tests Clearly and Be
Wordy
◦ Dont name your test like:
Test24, CustomerTest,
◦ Use:
◦ “Customer_IsValid_Returns_Fals
e_When_Customer_SocialSecur
ityNumber_Is_Empty”.
Other considerations
◦ Make your test suit fast. A good
test suit runs in seconds
◦ Test code is nice code
◦ Have a single assert per case
◦ Dont share states between your
test
◦ Encourage Others and Keep
Them Invested
References
◦ Michael Feathers “The Deep
Synergy Between Testability
and Good Design.” Video
◦ Starting to unit test, not as hard
as you think (Book)
◦ The art of unit test (Second
Edition) Book!
Thanks!
ANY QUESTIONS?

More Related Content

What's hot

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
Tung Nguyen Thanh
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
CodeOps Technologies LLP
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
Pablo Villar
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sachithra Gayan
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
XPDays
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
Cory Foy
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
Terry Yin
 
Continuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonestContinuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonest
Shawn Jones
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
David Ehringer
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolatorMaslowB
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Mireia Sangalo
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
Wojciech Bulaty
 
Entaggle: an Agile Software Development Case Study
Entaggle: an Agile Software Development Case StudyEntaggle: an Agile Software Development Case Study
Entaggle: an Agile Software Development Case StudyElisabeth Hendrickson
 
Test driven development
Test driven developmentTest driven development
Test driven development
Nascenia IT
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
Anuar Nurmakanov
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOS
Derek Lee Boire
 

What's hot (20)

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy Code
 
Continuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonestContinuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonest
 
Test Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
Type mock isolator
Type mock isolatorType mock isolator
Type mock isolator
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
 
Entaggle: an Agile Software Development Case Study
Entaggle: an Agile Software Development Case StudyEntaggle: an Agile Software Development Case Study
Entaggle: an Agile Software Development Case Study
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Common Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOSCommon Challenges & Best Practices for TDD on iOS
Common Challenges & Best Practices for TDD on iOS
 

Viewers also liked

Deferred object
Deferred objectDeferred object
Deferred object
PiXeL16
 
The Internet own boy
The Internet own boyThe Internet own boy
The Internet own boy
PiXeL16
 
An introduction to Mobile Development (Spanish)
An introduction to Mobile Development (Spanish)An introduction to Mobile Development (Spanish)
An introduction to Mobile Development (Spanish)
PiXeL16
 
Indoor Positioning System with iBeacons
Indoor Positioning System with iBeaconsIndoor Positioning System with iBeacons
Indoor Positioning System with iBeacons
PiXeL16
 
WWDC 2014
WWDC 2014WWDC 2014
WWDC 2014
PiXeL16
 
DevOps and Chef
DevOps and ChefDevOps and Chef
DevOps and Chef
PiXeL16
 
iOS 7
iOS 7 iOS 7
iOS 7
PiXeL16
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
PiXeL16
 
WWDC 2016
WWDC 2016WWDC 2016
WWDC 2016
PiXeL16
 
Mobile architecture problems and solutions.
Mobile architecture problems and solutions.Mobile architecture problems and solutions.
Mobile architecture problems and solutions.
PiXeL16
 
Hooked - How to build habit forming products
Hooked - How to build habit forming products Hooked - How to build habit forming products
Hooked - How to build habit forming products
PiXeL16
 
Rest Introduction (Chris Jimenez)
Rest Introduction (Chris Jimenez)Rest Introduction (Chris Jimenez)
Rest Introduction (Chris Jimenez)PiXeL16
 

Viewers also liked (12)

Deferred object
Deferred objectDeferred object
Deferred object
 
The Internet own boy
The Internet own boyThe Internet own boy
The Internet own boy
 
An introduction to Mobile Development (Spanish)
An introduction to Mobile Development (Spanish)An introduction to Mobile Development (Spanish)
An introduction to Mobile Development (Spanish)
 
Indoor Positioning System with iBeacons
Indoor Positioning System with iBeaconsIndoor Positioning System with iBeacons
Indoor Positioning System with iBeacons
 
WWDC 2014
WWDC 2014WWDC 2014
WWDC 2014
 
DevOps and Chef
DevOps and ChefDevOps and Chef
DevOps and Chef
 
iOS 7
iOS 7 iOS 7
iOS 7
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and Python
 
WWDC 2016
WWDC 2016WWDC 2016
WWDC 2016
 
Mobile architecture problems and solutions.
Mobile architecture problems and solutions.Mobile architecture problems and solutions.
Mobile architecture problems and solutions.
 
Hooked - How to build habit forming products
Hooked - How to build habit forming products Hooked - How to build habit forming products
Hooked - How to build habit forming products
 
Rest Introduction (Chris Jimenez)
Rest Introduction (Chris Jimenez)Rest Introduction (Chris Jimenez)
Rest Introduction (Chris Jimenez)
 

Similar to Unit testing

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
michael.labriola
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Sahar Nofal
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
Dharmendra Prasad
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
Blue Elephant Consulting
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
Attila Bertók
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
Priya Sharma
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 
Test driven development
Test driven developmentTest driven development
Test driven development
namkha87
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven developmentEinar Ingebrigtsen
 
Testacular
TestacularTestacular
Testacular
James Ford
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
Abhijeet Vaikar
 
5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-TestingMary Clemons
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
Alejandro Claro Mosqueda
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingPeter Presnell
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
AmalEldhose2
 

Similar to Unit testing (20)

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
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
 
An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1An Introduction To Software Development - Test Driven Development, Part 1
An Introduction To Software Development - Test Driven Development, Part 1
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Driving application development through behavior driven development
Driving application development through behavior driven developmentDriving application development through behavior driven development
Driving application development through behavior driven development
 
Testacular
TestacularTestacular
Testacular
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
I Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application TestingI Smell A RAT- Rapid Application Testing
I Smell A RAT- Rapid Application Testing
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 

Recently uploaded

SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
kalichargn70th171
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 

Recently uploaded (20)

SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
A Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdfA Comprehensive Look at Generative AI in Retail App Testing.pdf
A Comprehensive Look at Generative AI in Retail App Testing.pdf
 
Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024Globus Compute wth IRI Workflows - GlobusWorld 2024
Globus Compute wth IRI Workflows - GlobusWorld 2024
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 

Unit testing

  • 2. What is a Unit test? Unit testing is testing of units in your code base
  • 3. “ The unit in question could be a Class or a Method...
  • 4. What is NOT a unit test??? ◦ Developer Testing. Compiling and running the application to see if it does what you want it to do is not unit testing.
  • 5. What is NOT a unit test??? ◦ Smoke/ Quality Testing. Running an automated test that chugs through a laundry list of procedures involving large sections of your code base is not a unit test. This is the way the QA department does testing.
  • 6. What is NOT a unit test??? ◦ Things that involve databases, files, web services, device drivers or other things that are external to your code. These are called integration tests and they test a lot more than just your code.
  • 7. “ The fact that something is automated does not make it a unit test
  • 8. What is the purpose of Unit Testing? Testing is, at its core, experimentation. We’re just so used to the hypothesis that our code will work and that tests will confirm that to be the case. Or NOT!
  • 9. What is the purpose of Unit Testing? Testing at the unit level is about running experiments on classes and methods and capturing those experiments and their results via code.
  • 10. What is the purpose of Unit Testing? So unit testing is part of quality assurance, but it isn’t itself quality assurance, per se. Rather, it’s a way of documenting and locking in the behavior of the finest-grained units of your code – classes – in isolation.
  • 11. What is the purpose of Unit Testing? By understanding exactly how all of the smallest units of your code behave, it is straightforward to assemble them predictably into larger and larger components and thus construct a well-designed system.
  • 13. Basic Principles ◦ Unit tests are just methods that are decorated with annotations (Java) or attributes (C#) to indicate to the unit test runner that they are unit tests. Each method is generally a test. ◦ A unit test runner is simply a program that executes compiled test code and provides feedback on the results.
  • 14. Basic Principles ◦ Tests will generally either pass or fail, but they might also time out or be inconclusive ◦ Tests will generally either pass or fail, but they might also time out or be inconclusive, depending on the tooling that you use.
  • 15. Basic Principles ◦ Unit tests are simple and localized. They should not involve multi-threading, file I/ O, database connections, web services, etc. If you’re writing these things, you’re not writing unit tests but rather integration tests.
  • 16. Basic Principles ◦ Unit tests are fast. Your entire unit test suite should execute in seconds or quicker. ◦ Unit tests run in isolation, and the order in which they’re run should not matter at all. If test one has to run before test two, you’re not writing unit tests.
  • 17. Basic Principles ◦ Unit tests should not involve setting or depending on global application state such as public, static, stateful methods, singletons, etc.
  • 19. Arrange What this means is that you start off the test by setting up the class instance in the state you want it (usually by instantiating it and then whatever else you want),
  • 21. Assert Verify that what happened is what you expected to happen.
  • 22. Lets write our first Unit Test
  • 23.
  • 24. WOW ◦ Kind of a dense looking code there… ◦ How do we know if this works? ◦ Run the entire application to see if its blows up? ◦ But this is a pretty simple method in a pretty simple class. Doesn’t it seem like there ought to be a way to make sure it works?
  • 25. But never fear ◦ Kind of a dense looking code there… ◦ How do we know if this works? ◦ Run the entire application to see if its blows up? ◦ But this is a pretty simple method in a pretty simple class. Doesn’t it seem like there ought to be a way to make sure it works?
  • 27. Why do we want to do this? ◦ Well, might be that you or someone else later starts playing around with the implementation of IsPrime(). ◦ Maybe you want to make it faster. ◦ Maybe you realize it doesn’t handle negative numbers properly and aim to correct it. ◦ ETC!
  • 28. Donts
  • 29. Donts ◦ Units test are granular! ◦ If something fails, you dont know what
  • 30. Donts ◦ Units test are granular! ◦ If something fails, you dont know what!
  • 31. “ “Unit tests don’t just magically spring up like mushrooms after a few days of rain. They’re more like roses – you have to plan for them from the start and carefully cultivate an environment in which they can thrive.”
  • 32. How to get started! ◦ Test New Classes Only ◦ Test Existing Code by Extracting Little Classes ◦ Just do it!
  • 33. Things that are hard to test ◦ Calls static methods. (is that they manipulate some kind of global state.) ◦ Invokes singletons . ◦ Dispatches background workers ◦ Accesses files, connects to databases, calls web services, etc. ◦ Has classes that require crazy amounts of instantiation.
  • 34. Things that are hard to test ◦ For now just stay away from all of these...
  • 36. Designs that are bad for testing ◦ Active Record This architecture tightly couples your database to your domain logic and your domain logic to the rules for navigating through domain objects. ◦ WinForms / Webforms / Code behind stuff
  • 37. Monolithic Code ◦ Picture two methods. One is a 700-line juggernaut with more control flow statements than you can keep track of without a spreadsheet. The other is five lines long, consisting only of an initialize statement, a foreach, and a return statement. With which would you rather work?
  • 39. Procedural Code ◦ Stay away from this sort of Code ◦ Unit testing is about deconstructing things into smaller possible chunks
  • 40. Procedural Code ◦ Stop thinking in when and start thinking in what ◦ If you were thinking about “what” first here, you would form a much different mental model of a person’s day. You’d say things to yourself like, “well, during the course of a person’s day, he probably wakes up, gets dressed, eats breakfast – well, actually eats one or more meals
  • 41. Other Design Considerations for Your New Classes ◦ Avoid using fields to communicate between your methods by setting flags and tracking state. Favor having methods that can be executed at any time and in any order. ◦ Don’t instantiate things in your constructor. Favor passing them in.
  • 42. Other Design Considerations for Your New Classes ◦ Similarly, don’t have a lot of code or do a lot of work in your constructor. This will make your class painful to setup for test. ◦ In your methods, accept parameters that are as decomposed as possible. For instance, don’t accept a Customer object if all you do with it is read its SSN property.
  • 43. The earlier you start writing your unit tests, the better.
  • 46. Still dont want to Unit Test? ◦ Improve code quality ◦ Document the code ◦ Exercise Code ◦ Promote good design ◦ Help break problems into manageable chunks ◦ Expose problems eairler ◦ Its cheaper to find problems ◦ Allow easier refactoring without hesitation
  • 47. Still dont want to Unit Test? ◦ Improve code quality ◦ Document the code ◦ Exercise Code ◦ Promote good design ◦ Help break problems into manageable chunks ◦ Expose problems eairler ◦ Its cheaper to find problems ◦ Allow easier refactoring without hesitation
  • 48. Still dont want to Unit Test? ◦ Its easier to write code with unit tests
  • 49. Other considerations ◦ Name Your Tests Clearly and Be Wordy ◦ Dont name your test like: Test24, CustomerTest, ◦ Use: ◦ “Customer_IsValid_Returns_Fals e_When_Customer_SocialSecur ityNumber_Is_Empty”.
  • 50. Other considerations ◦ Make your test suit fast. A good test suit runs in seconds ◦ Test code is nice code ◦ Have a single assert per case ◦ Dont share states between your test ◦ Encourage Others and Keep Them Invested
  • 51. References ◦ Michael Feathers “The Deep Synergy Between Testability and Good Design.” Video ◦ Starting to unit test, not as hard as you think (Book) ◦ The art of unit test (Second Edition) Book!