SlideShare uma empresa Scribd logo
1 de 52
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?

Mais conteúdo relacionado

Mais procurados

TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentTung 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 iOSPablo 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 2010guest5639fa9
 
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 MoqXPDays
 
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 DataCory Foy
 
Adding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeTerry Yin
 
Continuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonestContinuous Integration: Finding problems soonest
Continuous Integration: Finding problems soonestShawn 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 DevelopmentMireia Sangalo
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPECWojciech 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 developmentNascenia IT
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo 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 iOSDerek Lee Boire
 

Mais procurados (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
 

Destaque

Deferred object
Deferred objectDeferred object
Deferred objectPiXeL16
 
The Internet own boy
The Internet own boyThe Internet own boy
The Internet own boyPiXeL16
 
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 iBeaconsPiXeL16
 
WWDC 2014
WWDC 2014WWDC 2014
WWDC 2014PiXeL16
 
DevOps and Chef
DevOps and ChefDevOps and Chef
DevOps and ChefPiXeL16
 
REST with Eve and Python
REST with Eve and PythonREST with Eve and Python
REST with Eve and PythonPiXeL16
 
WWDC 2016
WWDC 2016WWDC 2016
WWDC 2016PiXeL16
 
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
 

Destaque (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)
 

Semelhante a 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 Flexmichael.labriola
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingSahar 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 testingDharmendra 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 1Blue Elephant Consulting
 
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 developmentnamkha87
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
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
 
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
 
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.pptxAmalEldhose2
 

Semelhante a 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
 

Último

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
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
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
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
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 

Último (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
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-...
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
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...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 

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!