How to get started with Unit Testing? Part #1

lazlojuly
lazlojuly
Feb 16, 2018 · 4 min read
Image for post
Image for post
It’s done! Only adding tests now.

I guess many of us can relate to a situation depicted above.
A place where, unit testing is considered as a chore.

Something, that needs to be done as an addition to implementation as opposed to being vital part of the development process.

I think this mainly happens because unit testing demands a discipline to hold test code to the same standard as the production code.

Am I saying we should do TDD religiously?

Oh, no. No way!

TDD should be taken as an approach rather than as a rigid framework.

Try to write a small test first, even if it is just in plain English.
TDD forces us to think about what we really want from the unit; inputs, outputs, interface etc. resulting in much cleaner and organised codebase.

Also, remember tests do not need to to be perfect, neither does production code. Use TDD iteratively to get quick valuable feedback during development.

In code we trust!

Once we made sure the they work, we can trust them!

Image for post
Image for post
In code we trust!

How to identify a unit?

We can consider a bunch of functions as one unit or test a single function as a separate unit. Can take a class and call it a unit or write tests for framework provided abstractions (Angular or React components).

Image for post
Image for post
Test a group of functions as one unit vs. single function as separate unit

So there is no rule, but there is pragmatism.

We can use a very simple rule:

If a unit proves difficult to test, then it’s very likely that it needs to be broken down into smaller components.

Image for post
Image for post
Unit composition FTW!

Unit testing encourages modularity.

It encourages practices that are even noted in the popular (and now at least 40 years old) Unix philosophy:

“Write programs that do one thing and do it well.”

“Write programs to work together.”

Test behaviour vs. testing implementation

Test behaviour not implementation

This a great advice but can be confusing. It’s great for units that do not use dependencies or for end-to-end tests where testing implementation is a no no.

Image for post
Image for post
Testing behaviour vs. implementation

However, in many cases we can’t just blackbox our units.

This is because units interact.

Image for post
Image for post
Collaborator units require mocks and spies during testing

Software components don’t usually exist in isolation.
In a real application many units are created to collaborate with each other.

So when we test a unit that was designed to use another unit as a dependency, we need make sure that the dependent unit is being used correctly.

This, often results in various stubbing and spying practices which are definitely the weakest and most debated parts of unit testing.

Travel the paths!

Image for post
Image for post
Travel the paths!

Okaaay, but what if the unit has way too many variable results?

Well, that means that the component could and should be broken down into smaller units until it’s a fully testable.

Summary

  • Hold test code to the same standard as the production code.
  • Test all the paths (including the unhappy / erroneous paths)
  • Hard to test? Break it down!

Part #2 — Let’s code!

Image for post
Image for post
Part 2 — coming soon!

Thank you for reading the first part of my “How to get started with Unit Testing?” series. I aim to publish the second part (covering realistic code examples!) in just a couple of weeks time.

Follow me here on Medium or on Twitter to get notified as soon as the followup parts are online.

Useful resources on testing

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store