www.marclittlemore.com
MARC LITTLEMORE
7 TIPS FOR WRITING
GREAT UNIT TESTS
www.marclittlemore.com
@marclittlemore
github.com/marcl
Slide 2
Slide 2 text
WHAT IS A UNIT?
Slide 3
Slide 3 text
A unit is the
smallest component
that it makes sense to test
Slide 4
Slide 4 text
Class
Single method
Function
Slide 5
Slide 5 text
Your team can decide
what makes sense
to be your unit under test
Slide 6
Slide 6 text
WHY DO WE NEED
UNIT TESTS?
Slide 7
Slide 7 text
Unit tests are
extremely important
to your code quality
Slide 8
Slide 8 text
WHY DO WE NEED UNIT TESTS?
DEVELOPER UNDERSTANDING
Helps to express the
expectations of the code
Slide 9
Slide 9 text
WHY DO WE NEED UNIT TESTS?
DOCUMENTS FEATURES
Tells future-you why
the code is written
Slide 10
Slide 10 text
WHY DO WE NEED UNIT TESTS?
AIDS IN REFACTOR
If tests pass then developer
refactor has not changed
functionality
Slide 11
Slide 11 text
WHY DO WE NEED UNIT TESTS?
HELPS WITH DESIGN
Define your API
Slide 12
Slide 12 text
WHY DO WE NEED UNIT TESTS?
QUALITY ASSURANCE
Helps the team to ship
quality code
Slide 13
Slide 13 text
Unit tests should be the
first line of defence
for your project
Slide 14
Slide 14 text
7 TIPS FOR WRITING
GREAT UNIT TESTS
Slide 15
Slide 15 text
MAKE TEST NAMES
UNDERSTANDABLE
1.
Slide 16
Slide 16 text
Test names should be
human readable
with a clear description
Slide 17
Slide 17 text
Assume you’ll have
forgotten
what the test does
by tomorrow!
Slide 18
Slide 18 text
Write test names for
your fellow developers
to understand
Slide 19
Slide 19 text
Unit tests should become
living documentation
for features in your project
Slide 20
Slide 20 text
BAD TEST NAMES
Slide 21
Slide 21 text
BAD TEST NAMES
“multiply money by 1.2”
“adds 30 to balance”
“is username null”
Slide 22
Slide 22 text
Name your tests
with the why
and
not the how
Slide 23
Slide 23 text
BETTER TEST NAMES
Slide 24
Slide 24 text
GOOD TEST NAMES
“add expected tax to purchase price”
“should increase balance when deposit
is made”
“should set username to null when user
is not signed in”
Slide 25
Slide 25 text
ISOLATION
OF DEPENDENCIES
2.
Slide 26
Slide 26 text
Unit tests should
be isolated from
external influences
Slide 27
Slide 27 text
Unit tests should
test only the code
that you’ve written
Slide 28
Slide 28 text
Replace production
dependencies
Slide 29
Slide 29 text
Stub or mock
external services
Slide 30
Slide 30 text
STUB OR MOCK
Slide 31
Slide 31 text
STUB: CALLS TO SERVICES
3rd party APIs e.g. Twitter
Calls to own services
Slide 32
Slide 32 text
STUB: SERIALISATION
Avoid serialising data
Stub data stores
Return fake objects
Slide 33
Slide 33 text
STUB: STATE
Initialise pre-conditions by
mocking or stubbing data
in test setup
Slide 34
Slide 34 text
ONE REASON
TO FAIL
3.
Slide 35
Slide 35 text
Try to avoid
multiple expectations
in tests
Slide 36
Slide 36 text
Give only
one reason to fail
per test
Slide 37
Slide 37 text
Makes the tests
much easier to
reason about
Slide 38
Slide 38 text
MAKE TESTS FAST
4.
Slide 39
Slide 39 text
Unit tests should
execute as
quickly as possible
Slide 40
Slide 40 text
Unit tests should
be small in scope
by stubbing dependencies
Slide 41
Slide 41 text
Unit tests should
run in seconds
not minutes
Slide 42
Slide 42 text
INDEPENDENT
OF ENVIRONMENT
5.
Slide 43
Slide 43 text
Avoid dependencies
on specific machines
or environments
Slide 44
Slide 44 text
Avoid
“...well it works on
my machine...”
Slide 45
Slide 45 text
Stub initial state
Mock external data
Avoid environment
dependencies
Slide 46
Slide 46 text
Each test should
be independent
Slide 47
Slide 47 text
Each test should
run in isolation
Slide 48
Slide 48 text
Don’t rely on results
of previous tests
Slide 49
Slide 49 text
Each test should
have no side effects
e.g. adding to database
Slide 50
Slide 50 text
The team will lose faith
if your unit tests
begin to fail
Slide 51
Slide 51 text
TEST UNITS
COMPLETELY
6.
Slide 52
Slide 52 text
Some unit tests
are better than none
Slide 53
Slide 53 text
Test the
happy paths
Slide 54
Slide 54 text
But also test
the unhappy paths
Slide 55
Slide 55 text
And also test
the edge cases
Slide 56
Slide 56 text
Try to test
all the things!
Slide 57
Slide 57 text
FOUND A NEW BUG?
Slide 58
Slide 58 text
ADD A NEW UNIT TEST!
Slide 59
Slide 59 text
MAKE TESTING
EASY
7.
Slide 60
Slide 60 text
Make unit testing
really easy!
Slide 61
Slide 61 text
RUN TESTS
From the terminal
From the task runner
Automatically
Slide 62
Slide 62 text
ADD GIT HOOKS
Run tests before code can be
pushed to remote repositories
Slide 63
Slide 63 text
ADD TO CI SERVERS
Don’t deploy code until
all tests are passing
Slide 64
Slide 64 text
ASK THESE QUESTIONS
OF YOUR UNIT TESTS
Slide 65
Slide 65 text
WHY DO WE NEED UNIT TESTS?
1. What is being tested?
2. What should the code do?
Slide 66
Slide 66 text
WHY DO WE NEED UNIT TESTS?
3. What is the actual output?
4. What is the expected
output?
Slide 67
Slide 67 text
7 TIPS FOR WRITING
GREAT UNIT TESTS
1. MAKE TEST NAMES
UNDERSTANDABLE
2. ISOLATION OF DEPENDENCIES
3. ONE REASON TO FAIL
4. MAKE TESTS FAST
5. INDEPENDENT OF ENVIRONMENT
6. TEST UNITS COMPLETELY
7. MAKE TESTING EASY
Slide 68
Slide 68 text
DOWNLOAD THESE SLIDES
www.marclittlemore.com
THANKS FOR WATCHING
Any questions?
@marclittlemore
github.com/marcl
Slide 69
Slide 69 text
IMAGE CREDITS
Digital background image - Designed by Freepik
All other images - CC0 from Unsplash or Pexels