Good Unit Tests Ask For Quality
Code
Florin Coros
www.rabs.ro
florin.coros@rabs.ro
@florincoros
www.florincoros.wordpress.com
About me
RABS, Co-Founder
@florincoros
Agenda
• Good Unit Tests
• Code Quality
• How we get to code quality
• Demo: writing good UT & improving the code
Defining GOOD Unit Tests
Definition:
A unit test is an automated piece of code that
invokes the method or class being tested and
then checks some assumptions about the
logical behavior of that method or class.
A unit test is almost always written using a unit-
testing framework.
It’s fully automated, trustworthy, readable, and
maintainable.
Properties of GOOD Unit Tests
It is very small
It is automated and repeatable
It can be written easily
It runs quickly
It’s
readable, maintainable, trustwort
hy and fully automated
It should run in isolation
It should test ONE thing
If it fails you should know exactly
where the bug is
Good Unit Tests - easy to write
less than 5 minutes to implement
few lines of code. Less than 10 -15
see from a glimpse what the test checks
Good Unit Tests - Run in Isolation
Isolate the code under test
from the rest of your system,
by creating seams, to be able
to plug fakes (stubs & mocks)
which you can control in
your test
“There is no object-oriented
problem that cannot be
solved by adding a layer of
indirection, except, of
course, too many layers of
indirection.”
Good Unit Test - test ONE thing
Check only ONE thing in
your tests. If the test fails
you know exactly where the
problem is. You do not need
to do step-by-step
debugging
Dependencies and Fakes
An external dependency is an object in your system that
your code under test interacts with, and over which you
have no control. (Common examples are file
systems, threads, memory, time, and so on.)
A stub is a controllable replacement for an existing
dependency (or collaborator) in the system. By using a
stub, you can test your code without dealing with the
dependency directly.
A 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. There’s usually no more
than one mock per test.
Why Code Quality?
It works. Don’t touch it!
From Good Unit Tests to Code Quality
B
C
f()
g()
h()
h’()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
From Good Unit Tests to Code Quality
Screen
Keyboard
translate()
write()
read()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
From Good Unit Tests to Code Quality
IText
Output
IText
Input
translate()
write()
read()
Depend on abstractions not on implementation
details (DIP)
Use IoC for instantiating objects, or Factory
design patterns
Visible dependencies for our classes
Low coupling and high coherence
Extensibility, Reusability (OCP)
Design Patterns
Integration tests:
HIGH COSTS & LOW BENEFITS
From Good Unit Tests to Code Quality
OOD Principles
SOLID
DRY
IoC & DI
LowCoupling
High Coherence
…..…
Design Patterns
Composite
Chain Of Responsibility
Decorator
…….
Can you TEST it?
Good Enough
GOOD Unit Tests
Small
In Isolation
Test ONE Thing
Easy to implement
Reusability
Extensibility
Maintainability
Thank you!

More Related Content

PPS
Unit Testing
PPTX
Unit tests benefits
PPTX
Mock driven development using .NET
PPTX
Type mock isolator
PPTX
Unit Testing And Mocking
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Understanding Unit Testing
PPTX
An Introduction to Unit Testing
Unit Testing
Unit tests benefits
Mock driven development using .NET
Type mock isolator
Unit Testing And Mocking
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Understanding Unit Testing
An Introduction to Unit Testing

What's hot (19)

PPT
Unit testing
PPTX
Unit Testing
PPTX
A Brief Introduction to Test-Driven Development
PPTX
Test Driven Development
PPT
Unit Testing
PPTX
Unit testing solid fundamentals
PPTX
Software Quality via Unit Testing
PPTX
Unit Testing Concepts and Best Practices
PPTX
Unit testing - the hard parts
PDF
Unit and integration Testing
PPT
TDD In Practice
PDF
Unit testing (workshop)
PPTX
PDF
Unit Testing Fundamentals
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
PPS
Why Unit Testingl
PPTX
Unit tests & TDD
PDF
Test Driven Development
PDF
Test Driven Development
Unit testing
Unit Testing
A Brief Introduction to Test-Driven Development
Test Driven Development
Unit Testing
Unit testing solid fundamentals
Software Quality via Unit Testing
Unit Testing Concepts and Best Practices
Unit testing - the hard parts
Unit and integration Testing
TDD In Practice
Unit testing (workshop)
Unit Testing Fundamentals
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Why Unit Testingl
Unit tests & TDD
Test Driven Development
Test Driven Development
Ad

Viewers also liked (13)

PPTX
Mecanismos
PPT
Presentación para inversionistas de burger laboratory
PDF
Publicidade
PDF
Volevo essere l'Uomo Ragno
PDF
Abb matlab5650
PPTX
Asbestos to banned
DOC
PDF
62687016 woodward-identity-difference
DOCX
Planeación de clase del 18 al 21 de octubre
PDF
Inyección de código
PPTX
Film poster analysis
PDF
omv attestation de stage
PPT
Registros de pozo_1.ppt;filename= utf-8''registros de pozo 1
Mecanismos
Presentación para inversionistas de burger laboratory
Publicidade
Volevo essere l'Uomo Ragno
Abb matlab5650
Asbestos to banned
62687016 woodward-identity-difference
Planeación de clase del 18 al 21 de octubre
Inyección de código
Film poster analysis
omv attestation de stage
Registros de pozo_1.ppt;filename= utf-8''registros de pozo 1
Ad

Similar to Good Unit Tests Ask For Quality Code (20)

PDF
Unit Tesing in iOS
PDF
Unit testing, principles
PPTX
Unit Testing Full@
PDF
Unit test documentation
PPTX
1.1 Chapter_22_ Unit Testing-testing (1).pptx
PDF
Unit testing - An introduction
PDF
Becoming a better programmer - unit testing
PPTX
Building unit tests correctly with visual studio 2013
PDF
How to improve your unit tests?
PPTX
Unit testing
PDF
Unit testing Agile OpenSpace
PDF
What Is Unit Testing A Complete Guide With Examples.pdf
PDF
What Is Unit Testing_ A Complete Guide With Examples.pdf
PPTX
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
PDF
Unit testing - A&BP CC
PPTX
Unit Testing in Swift
PDF
Presentation on Unit Test with Python and Pytest
ODP
Embrace Unit Testing
PDF
How Unit Testing Strengthens Software Reliability
PDF
Unit testing (Exploring the other side as a tester)
Unit Tesing in iOS
Unit testing, principles
Unit Testing Full@
Unit test documentation
1.1 Chapter_22_ Unit Testing-testing (1).pptx
Unit testing - An introduction
Becoming a better programmer - unit testing
Building unit tests correctly with visual studio 2013
How to improve your unit tests?
Unit testing
Unit testing Agile OpenSpace
What Is Unit Testing A Complete Guide With Examples.pdf
What Is Unit Testing_ A Complete Guide With Examples.pdf
Integration and Unit Testing in Java using Test Doubles like mocks and stubs
Unit testing - A&BP CC
Unit Testing in Swift
Presentation on Unit Test with Python and Pytest
Embrace Unit Testing
How Unit Testing Strengthens Software Reliability
Unit testing (Exploring the other side as a tester)

More from Florin Coros (10)

PPTX
Designing a Distributed System for Long-Term Development
PPTX
Implementing Clean Architecture - Conference Talk
PDF
Implementing Clean Architecture
PDF
Implementing Clean Architecture
PDF
Enforce Consistentcy with Clean Architecture
PPTX
‘Cloud Ready’ Design through Application Software Infrastructure
PDF
Enforce Consistency through Application Infrastructure
PPTX
Enforce Consistency through Application Infrastructure
PPTX
Enforce Consistency through Application Infrastructure
PDF
Cloud Ready Design through Application Software Infrastructure
Designing a Distributed System for Long-Term Development
Implementing Clean Architecture - Conference Talk
Implementing Clean Architecture
Implementing Clean Architecture
Enforce Consistentcy with Clean Architecture
‘Cloud Ready’ Design through Application Software Infrastructure
Enforce Consistency through Application Infrastructure
Enforce Consistency through Application Infrastructure
Enforce Consistency through Application Infrastructure
Cloud Ready Design through Application Software Infrastructure

Recently uploaded (20)

PDF
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PPTX
Benefits of Physical activity for teenagers.pptx
PPTX
The various Industrial Revolutions .pptx
PDF
Five Habits of High-Impact Board Members
PDF
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
PPT
What is a Computer? Input Devices /output devices
DOCX
search engine optimization ppt fir known well about this
PDF
A review of recent deep learning applications in wood surface defect identifi...
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
Univ-Connecticut-ChatGPT-Presentaion.pdf
PDF
Architecture types and enterprise applications.pdf
PDF
Unlock new opportunities with location data.pdf
PDF
STKI Israel Market Study 2025 version august
PPTX
Tartificialntelligence_presentation.pptx
PPTX
O2C Customer Invoices to Receipt V15A.pptx
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
Chapter 5: Probability Theory and Statistics
How ambidextrous entrepreneurial leaders react to the artificial intelligence...
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Taming the Chaos: How to Turn Unstructured Data into Decisions
Benefits of Physical activity for teenagers.pptx
The various Industrial Revolutions .pptx
Five Habits of High-Impact Board Members
Hybrid horned lizard optimization algorithm-aquila optimizer for DC motor
What is a Computer? Input Devices /output devices
search engine optimization ppt fir known well about this
A review of recent deep learning applications in wood surface defect identifi...
A comparative study of natural language inference in Swahili using monolingua...
Enhancing emotion recognition model for a student engagement use case through...
Univ-Connecticut-ChatGPT-Presentaion.pdf
Architecture types and enterprise applications.pdf
Unlock new opportunities with location data.pdf
STKI Israel Market Study 2025 version august
Tartificialntelligence_presentation.pptx
O2C Customer Invoices to Receipt V15A.pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
Chapter 5: Probability Theory and Statistics

Good Unit Tests Ask For Quality Code

  • 1. Good Unit Tests Ask For Quality Code Florin Coros www.rabs.ro florin.coros@rabs.ro @florincoros www.florincoros.wordpress.com
  • 3. Agenda • Good Unit Tests • Code Quality • How we get to code quality • Demo: writing good UT & improving the code
  • 4. Defining GOOD Unit Tests Definition: A unit test is an automated piece of code that invokes the method or class being tested and then checks some assumptions about the logical behavior of that method or class. A unit test is almost always written using a unit- testing framework. It’s fully automated, trustworthy, readable, and maintainable.
  • 5. Properties of GOOD Unit Tests It is very small It is automated and repeatable It can be written easily It runs quickly It’s readable, maintainable, trustwort hy and fully automated It should run in isolation It should test ONE thing If it fails you should know exactly where the bug is
  • 6. Good Unit Tests - easy to write less than 5 minutes to implement few lines of code. Less than 10 -15 see from a glimpse what the test checks
  • 7. Good Unit Tests - Run in Isolation Isolate the code under test from the rest of your system, by creating seams, to be able to plug fakes (stubs & mocks) which you can control in your test “There is no object-oriented problem that cannot be solved by adding a layer of indirection, except, of course, too many layers of indirection.”
  • 8. Good Unit Test - test ONE thing Check only ONE thing in your tests. If the test fails you know exactly where the problem is. You do not need to do step-by-step debugging
  • 9. Dependencies and Fakes An external dependency is an object in your system that your code under test interacts with, and over which you have no control. (Common examples are file systems, threads, memory, time, and so on.) A stub is a controllable replacement for an existing dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly. A 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. There’s usually no more than one mock per test.
  • 11. It works. Don’t touch it!
  • 12. From Good Unit Tests to Code Quality B C f() g() h() h’() Depend on abstractions not on implementation details (DIP) Use IoC for instantiating objects, or Factory design patterns Visible dependencies for our classes Low coupling and high coherence Extensibility, Reusability (OCP) Design Patterns Integration tests: HIGH COSTS & LOW BENEFITS
  • 13. From Good Unit Tests to Code Quality Screen Keyboard translate() write() read() Depend on abstractions not on implementation details (DIP) Use IoC for instantiating objects, or Factory design patterns Visible dependencies for our classes Low coupling and high coherence Extensibility, Reusability (OCP) Design Patterns Integration tests: HIGH COSTS & LOW BENEFITS
  • 14. From Good Unit Tests to Code Quality IText Output IText Input translate() write() read() Depend on abstractions not on implementation details (DIP) Use IoC for instantiating objects, or Factory design patterns Visible dependencies for our classes Low coupling and high coherence Extensibility, Reusability (OCP) Design Patterns Integration tests: HIGH COSTS & LOW BENEFITS
  • 15. From Good Unit Tests to Code Quality OOD Principles SOLID DRY IoC & DI LowCoupling High Coherence …..… Design Patterns Composite Chain Of Responsibility Decorator ……. Can you TEST it? Good Enough GOOD Unit Tests Small In Isolation Test ONE Thing Easy to implement Reusability Extensibility Maintainability

Editor's Notes

  • #3: My name is Florin CorosI live in ClujAnd I am under 35I like to go to ROCK concertsI like to travel with my girl-friendI like to play GO. GO is a game that thought me to look a few steps ahead, which turned to be very useful for me…I tweet quite a lotI am a Software Architect at ISDCI am a big fan of Uncle BobI am one of the cofounders of RABS
  • #5: 5’You need to make a clear distinction between an integration tests, and a unit testIt can be written easily and runs quickly.It’s fully automated, trustworthy, readable, and maintainable.Quality Attributes of Good Unit TestsReadableMaintainableTrustworthy
  • #6: You need to make a clear distinction between an integration tests, and a unit testIt can be written easily and runs quickly.It’s fully automated, trustworthy, readable, and maintainable.Quality Attributes of Good Unit TestsReadableMaintainableTrustworthy
  • #7: 8’
  • #8: Flying people into space presents interesting challenges to engineersand astronauts, one of the more difficult being how to make sure theastronaut is ready to go into space and operate all the machinery. A fullintegration test for a space shuttle would require being in space, and that’sobviously not a safe way to test astronauts. That’s why NASA has fullsimulators that mimic the surroundings of a space shuttle’s control deck,which removes the external dependency of having to be in outer space.
  • #9: 10’
  • #10: You need to make a clear distinction between an integration tests, and a unit testIt can be written easily and runs quickly.It’s fully automated, trustworthy, readable, and maintainable.Quality Attributes of Good Unit TestsReadableMaintainableTrustworthy
  • #11: 11’ – Focusonly on the benefits. Short storyThe main benefit of code quality is that it REDUCE THE COST OF CHANGEChange is certain!Most of the time in our job we change existent code. We change code to add new features, because req change, because we missunderstood something, …The easier is to change it  the easier our job is  the more efficient we are  it costs us lessWe read a lot of code to find where certain behavior is implemented  we need to find our way in the codeRelated benefits - predictability of our changes or of how much it costs to add new features - efficiency: “The only way to go fast is to go well” - wellness and happiness
  • #12: It is working don’t touch it!Fragile code structureCHALLENGE: How to reach to a code structure which is not fragile and we can change easily and with higher predictability
  • #13: 12’SOLID at workDesign patterns at workEach time you depend on something on which you have no control in your test you are doing an integration test!Integration tests are to be avoided because: - hard to write & maintain, depend on configurations  HIGH COSTS - not knowing where the cause of failure is (which code, is it bad config?), no benefits to your code quality  LOW BENEFITS
  • #14: SOLID at workDesign patterns at workEach time you depend on something on which you have no control in your test you are doing an integration test!Integration tests are to be avoided because: - hard to write & maintain, depend on configurations  HIGH COSTS - not knowing where the cause of failure is (which code, is it bad config?), no benefits to your code quality  LOW BENEFITS
  • #15: SOLID at workDesign patterns at workEach time you depend on something on which you have no control in your test you are doing an integration test!Integration tests are to be avoided because: - hard to write & maintain, depend on configurations  HIGH COSTS - not knowing where the cause of failure is (which code, is it bad config?), no benefits to your code quality  LOW BENEFITS
  • #16: 14’All the discussion about whether the design principles and design patterns are being followed are summarized to ONE question