SlideShare a Scribd company logo
Unit Testing
Sucks
… and it is your our fault
[Php vigo][talk] unit testing sucks ( and it's your fault )
[Php vigo][talk] unit testing sucks ( and it's your fault )
Testing
“Testing sucks!”
- Everybody
Testing
Testing
Testing
Testing don’t suck: tests
that suck, suck.
We care about clean code
... but we forgot something
Writing better tests
Tips and tricks
for writing
better tests
Subtle Differences
#1: Test code
is different
from other
code
4 rules of Simple Design
Passes its tests
Minimizes duplication
Maximizes clarity
Has no superfluous parts
Rules for tests?
Trustworthy
Express the intent
Flexible
Tips and Tricks
#2: Choose
better test
names
What are we testing here?
Choose good names
I don’t want to read the whole test case to know
what is trying to test.
Please.
Pretty please.
Problems
What are we testing here?
Tips and Tricks
#3: Avoid logic
inside tests
Avoid logic inside tests
Avoid logic inside tests
Conditionals: You don’t know which path the test
will take
Loops: you could be sharing state between tests
If tests have logic, who test the tests?
Problems
Tips and Tricks
#4: Highlight
the important
stuff
Be concise
Too much information in your tests make them
hard to read
Think about what details could you leave out
Assert messages help us highlight behaviour
Problems
Assertions
Use the name of the test for
explaining the feature that you are
testing.
Assertions
Use the name of the test for
explaining the feature that you are
testing.
Then use assert messages to clarify
what's the example under test and
why is that tested.
Highlight the important
Highlight the important
Tips and Tricks
#5: Builders to
the rescue
Building same objects
Building same objects
We depend on the stability of the constructor /
setter methods. If they change, we are screwed.
We are forced to pass all the parameters, even if
they are not important for the test.
Problems
Building same objects
Building same objects
Tips and Tricks
#6: Building your
own DSL
No semantic
Single level of abstraction
Single Level
of Abstraction Principle
Single level of abstraction
Creating your DSL
Creating your DSL
Creating your DSL
Creating your DSL
Tips and Tricks
#7: Don’t hide
the important
stuff
Rules for tests
Trustworthy
Express the intent
Flexible
Rules for tests
Trustworthy
Express the intent
Flexible
Duplication
Expressing the intent
"The refactoring technique
‘Extract Method’ is not
about saving lines of code
to make it shorter, but
about make the code more
readable"
Typing isn’t the bottleneck
"Programming is not
about typing... it’s about
thinking"
- Rich Hickey
Usual suspects
Set Up
Data Providers
Techniques
Usual suspects
Set Up
Data Providers
Problems with Set Up
Move important code out of sight
Now you have to remember that the code was
there
Since code in setUp is generic for all test cases, you
lose context on why is that code there
Problems
Data Providers
Explicit Set Up™
Usual suspects
Set Up
Data Providers
Problems with Providers
Tend to make tests more complex
The most meaningful parts of the test (input &
output) are now outside the test
Usually end up more complex than you expected
Problems
Data Providers
Solutions
Use Explicit Set Up ™ for the important stuff
Don’t mix different test cases in one provider
Avoid logic inside providers
Tips and Tricks
#8: Mocking,
the right way
Test Doubles
Test Doubles
Test Doubles
Test doubles are objects
that we create to help us
test our system
Test Doubles
Dummy
Fake
Stub
Spy
Mock
Test Doubles
Dummy
Fake
Stub
Spy
Mock
Test Doubles
Dummy
Dummy
Dummy
Use the real object unless
its instantiation has side
effects or is expensive
somehow
Test Doubles
Stub
Test Doubles
Mock
Components
Value
object
Entity Service
Test Doubles
Avoid test doubles for
Value Objects
Test Doubles
Avoid test doubles for
Value Objects
Think twice before using
test doubles for Entities
Command Query Separation
Command
Query
Command
Commands change the
state of a system but do not
return a value
Query
Queries return a value and
don’t change the observable
state of the system
Free of side effects
Test Doubles
Use stubs to force a query to return
a specific value so you guide the
execution path of the test
Test Doubles
Use stubs to force a query to return
a specific value so you guide the
execution path of the test
Use mocks to assert that a
command to another object has
been sent
Without Test Doubles
is
this still
unit testing?
Without Test Doubles
What’s a
unit?
Unit Test
Unit test
Isolated Test
Isolated test
Decoupling
Don’t modify your tests
when refactoring.
It would couple the test to
the current implementation
Tips and Tricks
#9: How much
testing is
enough?
How much to test
"Write tests until
fear is
transformed
into boredom"
How much to test
Confidence
Communication
Success!
How much to test
Confidence & communication
are the goals
High Code coverage is not a
goal: is a side effect
#10: Think. Don’t
blindly follow
rules (like these)
Tips and Tricks
Everything is a trade-off
Conclusions
Testing makes you go faster.
Throw some love to your tests.
If you find yourself going slower,
identify why.
TDD makes all of this much easier.
Jose Armesto
@fiunchinho

More Related Content

PDF
Testing
PPTX
How to pass ArchiMate 3.1 Practitioner exam
PPTX
Every code coverage tool is measuring the wrong thing (on purpose)
PDF
Meetup 06/2015 - @testsetup
PPTX
ELW_Symantec_WriterTraining_Three Tutorials_28Jan2011
PPTX
Key learnings from my refactor journey.
PPTX
Unit testing
PPSX
Debugging by induction
Testing
How to pass ArchiMate 3.1 Practitioner exam
Every code coverage tool is measuring the wrong thing (on purpose)
Meetup 06/2015 - @testsetup
ELW_Symantec_WriterTraining_Three Tutorials_28Jan2011
Key learnings from my refactor journey.
Unit testing
Debugging by induction

What's hot (7)

PDF
Leaping over the Boundaries of Boundary Value Analysis
ODP
Start Your Own Bug Squad
PPT
An insight to test driven development and unit testing
PDF
How not to suck at unit tests
PPT
Using tdd to understand legacy codes
DOCX
Codifying Knowledge in Tests
PPTX
Load Test Correlation
Leaping over the Boundaries of Boundary Value Analysis
Start Your Own Bug Squad
An insight to test driven development and unit testing
How not to suck at unit tests
Using tdd to understand legacy codes
Codifying Knowledge in Tests
Load Test Correlation
Ad

Similar to [Php vigo][talk] unit testing sucks ( and it's your fault ) (20)

PDF
Test-Driven Development
PPTX
An Introduction to Developer Testing
ODP
Effective unit testing
PPTX
2016 10-04: tdd++: tdd made easier
PDF
An Introduction to Test Driven Development
PDF
Writing Tests Effectively
PDF
Unit testing - An introduction
PPTX
An Introduction to unit testing
PPTX
TDD Training
PDF
Test Driven Development - Workshop
PPTX
Unit Testing Full@
PPTX
Unit Testing and TDD 2017
PDF
Testing and TDD - KoJUG
PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PDF
Keeping code clean
PDF
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
PDF
Unit Testing Best Practices
PDF
Effective Unit Testing
PDF
Testing practicies not only in scala
PPTX
Test driven development
Test-Driven Development
An Introduction to Developer Testing
Effective unit testing
2016 10-04: tdd++: tdd made easier
An Introduction to Test Driven Development
Writing Tests Effectively
Unit testing - An introduction
An Introduction to unit testing
TDD Training
Test Driven Development - Workshop
Unit Testing Full@
Unit Testing and TDD 2017
Testing and TDD - KoJUG
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Keeping code clean
Developers’ mDay u Banjoj Luci - Milan Popović, PHP Srbija – Testimony (about...
Unit Testing Best Practices
Effective Unit Testing
Testing practicies not only in scala
Test driven development
Ad

More from PHP Vigo (8)

PDF
Blackfire.io PHPVigo Talk
PDF
Diariomotor Presentación PHPVigo
PPTX
PHPVigo #09: Preprocesadores CSS/SASS por Sergio Carracedo
PDF
Deployer Despliegue rápido de apps
PDF
Primeros pasos Symfony PHPVigo
PDF
Pablo Arias: Joomla como herramienta para el desarrollo web
PDF
Jesús Amieiro: Git para el día a día
PDF
Presentación del grupo PHPVigo - Meetup #1
Blackfire.io PHPVigo Talk
Diariomotor Presentación PHPVigo
PHPVigo #09: Preprocesadores CSS/SASS por Sergio Carracedo
Deployer Despliegue rápido de apps
Primeros pasos Symfony PHPVigo
Pablo Arias: Joomla como herramienta para el desarrollo web
Jesús Amieiro: Git para el día a día
Presentación del grupo PHPVigo - Meetup #1

Recently uploaded (20)

PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPTX
bas. eng. economics group 4 presentation 1.pptx
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PDF
Structs to JSON How Go Powers REST APIs.pdf
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
web development for engineering and engineering
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
Construction Project Organization Group 2.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
Strings in CPP - Strings in C++ are sequences of characters used to store and...
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Well-logging-methods_new................
PDF
PPT on Performance Review to get promotions
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
Digital Logic Computer Design lecture notes
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
bas. eng. economics group 4 presentation 1.pptx
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Structs to JSON How Go Powers REST APIs.pdf
Lecture Notes Electrical Wiring System Components
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
web development for engineering and engineering
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
Construction Project Organization Group 2.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Strings in CPP - Strings in C++ are sequences of characters used to store and...
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Well-logging-methods_new................
PPT on Performance Review to get promotions
Foundation to blockchain - A guide to Blockchain Tech
Digital Logic Computer Design lecture notes
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

[Php vigo][talk] unit testing sucks ( and it's your fault )