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

More Related Content

PDF
Unit Testing in Angular
PPTX
Angular Unit Testing
PPS
JUnit Presentation
PPT
Google test training
PDF
TypeScript Best Practices
PDF
Software Testing Techniques: An Overview
PPTX
PDF
Automation Testing using Selenium Webdriver
Unit Testing in Angular
Angular Unit Testing
JUnit Presentation
Google test training
TypeScript Best Practices
Software Testing Techniques: An Overview
Automation Testing using Selenium Webdriver

What's hot (20)

PPTX
ISTQB - What's testing
PDF
JavaScript guide 2020 Learn JavaScript
PDF
Software Testing Tools | Edureka
PDF
Selenium with Cucumber
PPTX
TypeScript: Basic Features and Compilation Guide
PPSX
Selenium WebDriver
PPTX
Unit Testing Concepts and Best Practices
PPTX
Test case techniques
PPT
Test Automation Framework Designs
PPTX
Html training slide
PPTX
Agile testing - Testing From Day 1
PPTX
Unit Testing And Mocking
PPTX
Test Automation and Selenium
PPT
Automated Testing vs Manual Testing
PDF
Test cases
PPTX
Introduction to Automation Testing
PPT
Test automation principles, terminologies and implementations
PPTX
Selenium WebDriver training
PDF
Unit and integration Testing
PDF
How To Write A Test Case In Software Testing | Edureka
ISTQB - What's testing
JavaScript guide 2020 Learn JavaScript
Software Testing Tools | Edureka
Selenium with Cucumber
TypeScript: Basic Features and Compilation Guide
Selenium WebDriver
Unit Testing Concepts and Best Practices
Test case techniques
Test Automation Framework Designs
Html training slide
Agile testing - Testing From Day 1
Unit Testing And Mocking
Test Automation and Selenium
Automated Testing vs Manual Testing
Test cases
Introduction to Automation Testing
Test automation principles, terminologies and implementations
Selenium WebDriver training
Unit and integration Testing
How To Write A Test Case In Software Testing | Edureka
Ad

Similar to Unit testing - An introduction (20)

PPTX
Unit Testing and TDD 2017
PPTX
Unit testing
PDF
Testing and TDD - KoJUG
PDF
What Is Unit Testing A Complete Guide With Examples.pdf
PPTX
Testing 101
PDF
How Unit Testing Strengthens Software Reliability
PPT
Automated testing overview
PDF
What Is Unit Testing_ A Complete Guide With Examples.pdf
PPTX
Making the Unstable Stable - An Intro To Testing
PPTX
Unit testing
PPTX
Skillwise Unit Testing
PPTX
Unit testing & TDD concepts with best practice guidelines.
PPTX
Test driven development
PDF
Unit testing (Exploring the other side as a tester)
PPTX
Unit Testing talk
PDF
Test driven development
PPTX
Unit Testing Full@
PDF
What is Unit Testing_ - A Complete Guide.pdf
PPTX
Unit testing
PPT
types of testing with descriptions and examples
Unit Testing and TDD 2017
Unit testing
Testing and TDD - KoJUG
What Is Unit Testing A Complete Guide With Examples.pdf
Testing 101
How Unit Testing Strengthens Software Reliability
Automated testing overview
What Is Unit Testing_ A Complete Guide With Examples.pdf
Making the Unstable Stable - An Intro To Testing
Unit testing
Skillwise Unit Testing
Unit testing & TDD concepts with best practice guidelines.
Test driven development
Unit testing (Exploring the other side as a tester)
Unit Testing talk
Test driven development
Unit Testing Full@
What is Unit Testing_ - A Complete Guide.pdf
Unit testing
types of testing with descriptions and examples
Ad

More from Alejandro Claro Mosqueda (6)

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

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
KodekX | Application Modernization Development
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Modernizing your data center with Dell and AMD
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
NewMind AI Monthly Chronicles - July 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Electronic commerce courselecture one. Pdf
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
KodekX | Application Modernization Development
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Advanced methodologies resolving dimensionality complications for autism neur...
Modernizing your data center with Dell and AMD
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
NewMind AI Monthly Chronicles - July 2025
The AUB Centre for AI in Media Proposal.docx
Empathic Computing: Creating Shared Understanding
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Reach Out and Touch Someone: Haptics and Empathic Computing
Building Integrated photovoltaic BIPV_UPV.pdf
cuic standard and advanced reporting.pdf
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Spectral efficient network and resource selection model in 5G networks
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows

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