Tips on Writing Good Tests

Tips on Writing Good Tests

I'd rather curve-fit my code to my tests, rather than curve-fit my tests to my code.

Learning Test Driven Development (writing tests before writing code) has been really difficult for me.

  • It feels unnatural.
  • It feels awkward.
  • It adds mental overhead/cognitive load.
  • And I struggle with figuring out what to test.

I'm still learning TDD but here are some of my tips.

1. Test behaviours, not implementation.

Testing for implementation is like saying "get me an apple, and these are the steps you need to take". And testing for behaviours is like saying "give me an apple, but I don't care how you get it".

So for example, let's say that I drive down to a farm and pluck an apple of a tree.

Implementation Testing

  • Did David visit a farm? ✅
  • Is there one less apple on the tree? ✅
  • Did the end user get an apple? ✅

Behaviour Testing

  • Did the end user get an apple? ✅

A New Way to Get Apples

Now, let's say I discover that supermarkets exist and now I start getting my apples from there.

  • Did David visit a farm? ❌
  • Is there one less apple on the tree? ❌
  • Did the end user get an apple? ✅

Now I can't deploy my application until I rewrite these broken tests. That's a big problem.


2. Test what the users sees.

The user doesn't care how you get the apple but they do care if they get the apple.

It's a very useful rule of thumb when determining if something is a behaviour or if it is an implementation detail.

Keep in mind that, there are two users: the End User and the Developer User.

Kent C. Dodds has written a brilliant blog post on this topic called "Avoid the Test User". But to summarise, End Users consume your application and Developer Users consume your APIs.

When you test for implementation, you create a third user, the Test User.

And, that's all I can think of for now.


At the end of the day, I want to able to write some damn-good software. And I want to be damn-good at TDD.

P.S - Since my audience skews towards beginners, it's really, really important not to worry about this stuff until later if you are still learning the ropes. I'd consider this an advanced topic.

You have a limited amount of Cognitive Bandwidth. Don't spend your limited bandwidth on unnecessary Cognitive Load. Use it wisely. 😉