SlideShare a Scribd company logo
Unit Testing
A (not so) short introduction

Company

LOGO
Alejandro Claro Mosqueda
What is all this stuff?!

AAA
QA key questions

Business Facing

Are we building the right product?

Are we building the product right?
Technology/Implementation Facing

Quality Assurance is more than look for bugs!
Accidental
Defects?

Essential
Defects?

Quality Assurance is more than look for bugs!

Critique Product

Supporting the team

QA key questions
Brian Marick’s test categorization
Test size classification
Ok! But what is a unit test?
A unit test is an automated piece of code that invokes the method being
tested and then checks some assumptions about the logical behavior of
that method.

It can be written easily and runs quickly.
It’s fully automated, trustworthy, readable, isolated, and maintainable.
What are they good for?
 Unit tests prove that your code actually works (at a function/method level).
 You get some kind of “low-level regression-test” suite.
 You can improve the design without breaking it.

 They demonstrate concrete progress.
 Unit tests are a form of sample code.
 It forces you to plan before you code.
 It reduces the cost of bugs.
 It's even better than code inspections.
What are they not for?
 It's not for testing correct inter-operation of multiple subsystems.
 It should be a stand-alone test which is not related to other
subsystems.

 Test against external resources (data base, LDAP, third party libraries, etc).
 Introducing dependencies on external resources or data turns unit tests
into integration tests.
 In many cases you can't write a unit test to reproduce bug appeared in
production.
 It's not regression testing.
Disadvantages
 Big time investment.

For the simple case you lose about 20% of the actual implementation, but for
complicated cases you lose much more.
 Design Impacts.

Sometimes the high-level design is not clear at the start and evolves as you go
along - this will force you to completely redo your test which will generate a big
time lose.
Best Practices!
 Make sure your tests test one thing and one thing only.
 Each unit test should be independent from the other.
 Keep consistent conventions (e.g. AAA). Readability is important for tests.
 Name your unit tests clearly and consistently.
 Separate you concerns. Extract layers to improve the design.

 Avoid unnecessary preconditions.
 Fake behavior with mocks or stubs to concentrate on test scenario.
 Check code coverage during testing.
 Don’t unit-test configuration settings.
 Tests should run automatically to provide continuous feedback. Keep the bar green to

keep the code clean!
Isolate!
State testing asserts properties on an object.
Interaction testing is testing how an object sends input to or receives input from other
objects — how that object interacts with other objects.
Stub is a controllable replacement for an existing dependency in the system. By using a
stub, you can test your code without dealing with the dependency directly.
Mock object is a fake object in the system that decides whether the unit test has passed
or failed. It does so by verifying whether the object under test interacted as expected
with the fake object.
Naming by Unit Of Work

MethodUnderTest_Input_ExpectedOutput
MethodUnderTest_LogicalAction_ExpectedChangeInBehavior
MethodUnderTest_ActionOrInput_ExepectedCallToThirdParty

Addition_PositiveNumbers_ReturnsSum()
Addition_WhenCalled_ResetsTheNextSum()
Addition_NegativeNumbers_CallsLogger()
Arrange-Act-Assert (AAA) Pattern
Arrange: setup everything needed for the running the tested code. This includes any
initialization of dependencies, mocks and data needed for the test to run.
Act: Invoke the code under test.
Assert: Specify the pass criteria for the test, which fails it if not met.
Bad Practices and anti-patterns
 Static methods. Static methods are death to unit testability because they cannot
be mocked or stubbed.

 Relying on external resources.

 Unit test for GUI.
 Constrained test order.

 Hidden test call.

 Mutable shared-state.

 Multiple asserts.
Test fixture (Harness)
Test fixture refers to the fixed state used as a baseline for running tests. The
purpose of a test fixture is to ensure that there is a well known and fixed
environment in which tests are run so that results are repeatable.
Test fixture process
When do you write the test?

After/During coding
 Focus on code.
 Thinking about algorithm.

 More refactoring.
 Easier initially.
When do you write the test?

Before coding (TDD, BDD)
 Focus on requirements.

 Thinking about how code will be
consumed.
 Stop coding when reqs met.
 Harder initially.
Test driven development (TDD)
“TDD is a design process, not a testing process.”
Acceptance vs Unit testing
ATs and UTs are the same though, right?
 Both can be written before implementation.
 Both should be automated by containing assertions for validating expected
results.
 Both typically use fixture setup and teardown code to run before and after the
execution of the tests.
 Both can be implemented using a framework.
Black-box and White-box testing
Careful - They are NOT the same!
Acceptance test is owned and defined by the customer to verify that a
story is complete and correct.

 ATs must be defined in a language familiar to the customer:
 Generic enough to capture requirements
 Abstract enough to be maintainable
 Simple enough to be easily understood
 ATs should be defined and run using a tool accessible to the customer.
 ATs encourage the customer to consider the all aspects of user
experience.
 ATs test interaction of all layers in the system.
 ATs require external systems to be operating correctly.
Acceptance and Unit testing relation

setup

ATD

AT
impl
teardown
setup

ATD

AT
impl

Application Interface

Test
Implementation

Acceptance Test Client

Acceptance
Test Definition

Units
Implementation

Unit Test
Fixture

Setup
impl

UT

impl

impl

UT

UT
impl

teardown

UT

setup

ATD

impl

AT
impl

UT

impl

UT

teardown

Mock

Teardown
Acceptance Test Driven Development (ATDD)
Let’s talk!
Motivation is what gets you starting; practice is what
keeps you going
Ad

More Related Content

What's hot (20)

Best practices for test automation
Best practices for test automationBest practices for test automation
Best practices for test automation
David Tzemach
 
What is Regression Testing? | Edureka
What is Regression Testing? | EdurekaWhat is Regression Testing? | Edureka
What is Regression Testing? | Edureka
Edureka!
 
Agile test-management-test-rail-lastest
Agile test-management-test-rail-lastestAgile test-management-test-rail-lastest
Agile test-management-test-rail-lastest
Onur Baskirt
 
Unit testing
Unit testing Unit testing
Unit testing
Mani Kanth
 
05 junit
05 junit05 junit
05 junit
mha4
 
Testing
TestingTesting
Testing
Sonali Chauhan
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
Maveryx
 
Testing techniques
Testing techniquesTesting techniques
Testing techniques
RaginiRohatgi
 
Performance testing
Performance testing Performance testing
Performance testing
BugRaptors
 
Presentation On Software Testing Bug Life Cycle
Presentation On Software Testing Bug Life CyclePresentation On Software Testing Bug Life Cycle
Presentation On Software Testing Bug Life Cycle
Rajon
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
MUHAMMAD FARHAN ASLAM
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
Prashant Cholachagudd
 
Testing fundamentals
Testing fundamentalsTesting fundamentals
Testing fundamentals
Raviteja Chowdary Adusumalli
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
Yura Nosenko
 
Test Automation
Test AutomationTest Automation
Test Automation
nikos batsios
 
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Ankit Prajapati
 
Introduction to White box testing
Introduction to White box testingIntroduction to White box testing
Introduction to White box testing
Aliaa Monier Ismaail
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
Pekka Klärck
 
Manual Testing.
Manual Testing.Manual Testing.
Manual Testing.
Dhanasekaran Nagarajan
 
Best practices for test automation
Best practices for test automationBest practices for test automation
Best practices for test automation
David Tzemach
 
What is Regression Testing? | Edureka
What is Regression Testing? | EdurekaWhat is Regression Testing? | Edureka
What is Regression Testing? | Edureka
Edureka!
 
Agile test-management-test-rail-lastest
Agile test-management-test-rail-lastestAgile test-management-test-rail-lastest
Agile test-management-test-rail-lastest
Onur Baskirt
 
05 junit
05 junit05 junit
05 junit
mha4
 
Data Driven Testing
Data Driven TestingData Driven Testing
Data Driven Testing
Maveryx
 
Performance testing
Performance testing Performance testing
Performance testing
BugRaptors
 
Presentation On Software Testing Bug Life Cycle
Presentation On Software Testing Bug Life CyclePresentation On Software Testing Bug Life Cycle
Presentation On Software Testing Bug Life Cycle
Rajon
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Whitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applicationsWhitebox testing of Spring Boot applications
Whitebox testing of Spring Boot applications
Yura Nosenko
 
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Software Testing - Part 1 (Techniques, Types, Levels, Methods, STLC, Bug Life...
Ankit Prajapati
 
Robot Framework Introduction
Robot Framework IntroductionRobot Framework Introduction
Robot Framework Introduction
Pekka Klärck
 

Similar to Unit testing - An introduction (20)

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_trivedi
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
Priya Sharma
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
Deepak Singhvi
 
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
 
Effective unit testing
Effective unit testingEffective unit testing
Effective unit testing
Roberto Casadei
 
Test driven development
Test driven developmentTest driven development
Test driven development
namkha87
 
Testing In Software Engineering
Testing In Software EngineeringTesting In Software Engineering
Testing In Software Engineering
kiansahafi
 
Unit testing
Unit testingUnit testing
Unit testing
Murugesan Nataraj
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
medsherb
 
What Is Unit Testing_ A Complete Guide With Examples.pdf
What Is Unit Testing_ A Complete Guide With Examples.pdfWhat Is Unit Testing_ A Complete Guide With Examples.pdf
What Is Unit Testing_ A Complete Guide With Examples.pdf
Steve Wortham
 
Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009
Grig Gheorghiu
 
What Is Unit Testing A Complete Guide With Examples.pdf
What Is Unit Testing A Complete Guide With Examples.pdfWhat Is Unit Testing A Complete Guide With Examples.pdf
What Is Unit Testing A Complete Guide With Examples.pdf
Jace Reed
 
Testing 3: Types Of Tests That May Be Required
Testing 3: Types Of Tests That May Be RequiredTesting 3: Types Of Tests That May Be Required
Testing 3: Types Of Tests That May Be Required
ArleneAndrews2
 
Testing
TestingTesting
Testing
Mohammed
 
Types of testing
Types of testingTypes of testing
Types of testing
Sonam Agarwal
 
Unit testing
Unit testingUnit testing
Unit testing
PiXeL16
 
Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
Deepak Singhvi
 
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
 
Test driven development
Test driven developmentTest driven development
Test driven development
namkha87
 
Testing In Software Engineering
Testing In Software EngineeringTesting In Software Engineering
Testing In Software Engineering
kiansahafi
 
Basic software-testing-concepts
Basic software-testing-conceptsBasic software-testing-concepts
Basic software-testing-concepts
medsherb
 
What Is Unit Testing_ A Complete Guide With Examples.pdf
What Is Unit Testing_ A Complete Guide With Examples.pdfWhat Is Unit Testing_ A Complete Guide With Examples.pdf
What Is Unit Testing_ A Complete Guide With Examples.pdf
Steve Wortham
 
Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009Agile Testing Pasadena JUG Aug2009
Agile Testing Pasadena JUG Aug2009
Grig Gheorghiu
 
What Is Unit Testing A Complete Guide With Examples.pdf
What Is Unit Testing A Complete Guide With Examples.pdfWhat Is Unit Testing A Complete Guide With Examples.pdf
What Is Unit Testing A Complete Guide With Examples.pdf
Jace Reed
 
Testing 3: Types Of Tests That May Be Required
Testing 3: Types Of Tests That May Be RequiredTesting 3: Types Of Tests That May Be Required
Testing 3: Types Of Tests That May Be Required
ArleneAndrews2
 
Unit testing
Unit testingUnit testing
Unit testing
PiXeL16
 
Ad

More from Alejandro Claro Mosqueda (6)

Metodos de monte carlo en mecánica estadistica
Metodos de monte carlo en mecánica estadisticaMetodos de monte carlo en mecánica estadistica
Metodos de monte carlo en mecánica estadistica
Alejandro Claro Mosqueda
 
Introducción a los solitones - Presentación de algunas soluciones solitónicas
Introducción a los solitones - Presentación de algunas soluciones solitónicasIntroducción a los solitones - Presentación de algunas soluciones solitónicas
Introducción a los solitones - Presentación de algunas soluciones solitónicas
Alejandro Claro Mosqueda
 
Tunelaje de solitones
Tunelaje de solitonesTunelaje de solitones
Tunelaje de solitones
Alejandro Claro Mosqueda
 
Algoritmos aproximados - El problema de la mochila 0-1
Algoritmos aproximados - El problema de la mochila 0-1Algoritmos aproximados - El problema de la mochila 0-1
Algoritmos aproximados - El problema de la mochila 0-1
Alejandro Claro Mosqueda
 
Software design - Write solid software with the ideal chalk
Software design - Write solid software with the  ideal chalkSoftware design - Write solid software with the  ideal chalk
Software design - Write solid software with the ideal chalk
Alejandro Claro Mosqueda
 
Lean thinking and the agile culture
Lean thinking and the agile cultureLean thinking and the agile culture
Lean thinking and the agile culture
Alejandro Claro Mosqueda
 
Metodos de monte carlo en mecánica estadistica
Metodos de monte carlo en mecánica estadisticaMetodos de monte carlo en mecánica estadistica
Metodos de monte carlo en mecánica estadistica
Alejandro Claro Mosqueda
 
Introducción a los solitones - Presentación de algunas soluciones solitónicas
Introducción a los solitones - Presentación de algunas soluciones solitónicasIntroducción a los solitones - Presentación de algunas soluciones solitónicas
Introducción a los solitones - Presentación de algunas soluciones solitónicas
Alejandro Claro Mosqueda
 
Algoritmos aproximados - El problema de la mochila 0-1
Algoritmos aproximados - El problema de la mochila 0-1Algoritmos aproximados - El problema de la mochila 0-1
Algoritmos aproximados - El problema de la mochila 0-1
Alejandro Claro Mosqueda
 
Software design - Write solid software with the ideal chalk
Software design - Write solid software with the  ideal chalkSoftware design - Write solid software with the  ideal chalk
Software design - Write solid software with the ideal chalk
Alejandro Claro Mosqueda
 
Ad

Recently uploaded (20)

UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
Build With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdfBuild With AI - In Person Session Slides.pdf
Build With AI - In Person Session Slides.pdf
Google Developer Group - Harare
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of ExchangesJignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah Innovator
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 
UiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer OpportunitiesUiPath Agentic Automation: Community Developer Opportunities
UiPath Agentic Automation: Community Developer Opportunities
DianaGray10
 
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptxWebinar - Top 5 Backup Mistakes MSPs and Businesses Make   .pptx
Webinar - Top 5 Backup Mistakes MSPs and Businesses Make .pptx
MSP360
 
How to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabberHow to Install & Activate ListGrabber - eGrabber
How to Install & Activate ListGrabber - eGrabber
eGrabber
 
Financial Services Technology Summit 2025
Financial Services Technology Summit 2025Financial Services Technology Summit 2025
Financial Services Technology Summit 2025
Ray Bugg
 
Does Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should KnowDoes Pornify Allow NSFW? Everything You Should Know
Does Pornify Allow NSFW? Everything You Should Know
Pornify CC
 
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Optima Cyber - Maritime Cyber Security - MSSP Services - Manolis Sfakianakis ...
Mike Mingos
 
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Kit-Works Team Study_아직도 Dockefile.pdf_김성호
Wonjun Hwang
 
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
RTP Over QUIC: An Interesting Opportunity Or Wasted Time?
Lorenzo Miniero
 
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
On-Device or Remote? On the Energy Efficiency of Fetching LLM-Generated Conte...
Ivano Malavolta
 
Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?Shoehorning dependency injection into a FP language, what does it take?
Shoehorning dependency injection into a FP language, what does it take?
Eric Torreborre
 
Bepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firmBepents tech services - a premier cybersecurity consulting firm
Bepents tech services - a premier cybersecurity consulting firm
Benard76
 
Mastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B LandscapeMastering Testing in the Modern F&B Landscape
Mastering Testing in the Modern F&B Landscape
marketing943205
 
IT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information TechnologyIT484 Cyber Forensics_Information Technology
IT484 Cyber Forensics_Information Technology
SHEHABALYAMANI
 
AI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of DocumentsAI Agents at Work: UiPath, Maestro & the Future of Documents
AI Agents at Work: UiPath, Maestro & the Future of Documents
UiPathCommunity
 
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of ExchangesJignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah - The Innovator and Czar of Exchanges
Jignesh Shah Innovator
 
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Hybridize Functions: A Tool for Automatically Refactoring Imperative Deep Lea...
Raffi Khatchadourian
 
machines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdfmachines-for-woodworking-shops-en-compressed.pdf
machines-for-woodworking-shops-en-compressed.pdf
AmirStern2
 
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptxDevOpsDays SLC - Platform Engineers are Product Managers.pptx
DevOpsDays SLC - Platform Engineers are Product Managers.pptx
Justin Reock
 
AsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API DesignAsyncAPI v3 : Streamlining Event-Driven API Design
AsyncAPI v3 : Streamlining Event-Driven API Design
leonid54
 

Unit testing - An introduction

  • 1. Unit Testing A (not so) short introduction Company LOGO Alejandro Claro Mosqueda
  • 2. What is all this stuff?! AAA
  • 3. QA key questions Business Facing Are we building the right product? Are we building the product right? Technology/Implementation Facing Quality Assurance is more than look for bugs!
  • 4. Accidental Defects? Essential Defects? Quality Assurance is more than look for bugs! Critique Product Supporting the team QA key questions
  • 5. Brian Marick’s test categorization
  • 7. Ok! But what is a unit test? A unit test is an automated piece of code that invokes the method being tested and then checks some assumptions about the logical behavior of that method. It can be written easily and runs quickly. It’s fully automated, trustworthy, readable, isolated, and maintainable.
  • 8. What are they good for?  Unit tests prove that your code actually works (at a function/method level).  You get some kind of “low-level regression-test” suite.  You can improve the design without breaking it.  They demonstrate concrete progress.  Unit tests are a form of sample code.  It forces you to plan before you code.  It reduces the cost of bugs.  It's even better than code inspections.
  • 9. What are they not for?  It's not for testing correct inter-operation of multiple subsystems.  It should be a stand-alone test which is not related to other subsystems.  Test against external resources (data base, LDAP, third party libraries, etc).  Introducing dependencies on external resources or data turns unit tests into integration tests.  In many cases you can't write a unit test to reproduce bug appeared in production.  It's not regression testing.
  • 10. Disadvantages  Big time investment. For the simple case you lose about 20% of the actual implementation, but for complicated cases you lose much more.  Design Impacts. Sometimes the high-level design is not clear at the start and evolves as you go along - this will force you to completely redo your test which will generate a big time lose.
  • 11. Best Practices!  Make sure your tests test one thing and one thing only.  Each unit test should be independent from the other.  Keep consistent conventions (e.g. AAA). Readability is important for tests.  Name your unit tests clearly and consistently.  Separate you concerns. Extract layers to improve the design.  Avoid unnecessary preconditions.  Fake behavior with mocks or stubs to concentrate on test scenario.  Check code coverage during testing.  Don’t unit-test configuration settings.  Tests should run automatically to provide continuous feedback. Keep the bar green to keep the code clean!
  • 12. Isolate! State testing asserts properties on an object. Interaction testing is testing how an object sends input to or receives input from other objects — how that object interacts with other objects. Stub is a controllable replacement for an existing dependency in the system. By using a stub, you can test your code without dealing with the dependency directly. Mock object is a fake object in the system that decides whether the unit test has passed or failed. It does so by verifying whether the object under test interacted as expected with the fake object.
  • 13. Naming by Unit Of Work MethodUnderTest_Input_ExpectedOutput MethodUnderTest_LogicalAction_ExpectedChangeInBehavior MethodUnderTest_ActionOrInput_ExepectedCallToThirdParty Addition_PositiveNumbers_ReturnsSum() Addition_WhenCalled_ResetsTheNextSum() Addition_NegativeNumbers_CallsLogger()
  • 14. Arrange-Act-Assert (AAA) Pattern Arrange: setup everything needed for the running the tested code. This includes any initialization of dependencies, mocks and data needed for the test to run. Act: Invoke the code under test. Assert: Specify the pass criteria for the test, which fails it if not met.
  • 15. Bad Practices and anti-patterns  Static methods. Static methods are death to unit testability because they cannot be mocked or stubbed.  Relying on external resources.  Unit test for GUI.  Constrained test order.  Hidden test call.  Mutable shared-state.  Multiple asserts.
  • 16. Test fixture (Harness) Test fixture refers to the fixed state used as a baseline for running tests. The purpose of a test fixture is to ensure that there is a well known and fixed environment in which tests are run so that results are repeatable.
  • 18. When do you write the test? After/During coding  Focus on code.  Thinking about algorithm.  More refactoring.  Easier initially.
  • 19. When do you write the test? Before coding (TDD, BDD)  Focus on requirements.  Thinking about how code will be consumed.  Stop coding when reqs met.  Harder initially.
  • 20. Test driven development (TDD) “TDD is a design process, not a testing process.”
  • 21. Acceptance vs Unit testing ATs and UTs are the same though, right?  Both can be written before implementation.  Both should be automated by containing assertions for validating expected results.  Both typically use fixture setup and teardown code to run before and after the execution of the tests.  Both can be implemented using a framework.
  • 23. Careful - They are NOT the same! Acceptance test is owned and defined by the customer to verify that a story is complete and correct.  ATs must be defined in a language familiar to the customer:  Generic enough to capture requirements  Abstract enough to be maintainable  Simple enough to be easily understood  ATs should be defined and run using a tool accessible to the customer.  ATs encourage the customer to consider the all aspects of user experience.  ATs test interaction of all layers in the system.  ATs require external systems to be operating correctly.
  • 24. Acceptance and Unit testing relation setup ATD AT impl teardown setup ATD AT impl Application Interface Test Implementation Acceptance Test Client Acceptance Test Definition Units Implementation Unit Test Fixture Setup impl UT impl impl UT UT impl teardown UT setup ATD impl AT impl UT impl UT teardown Mock Teardown
  • 25. Acceptance Test Driven Development (ATDD)
  • 26. Let’s talk! Motivation is what gets you starting; practice is what keeps you going
  翻译: