Here is my top tip to help you write better tests, be they unit tests, integration tests, functional tests or unicorn tests:

Always write a failing test first.

There are three steps to TDD. You may have heard of the 🚦 traffic lights*:

  • ❤️ Red: write a failing test
  • 💛 Amber: make the test pass
  • 💚 Green: improve the code you’ve just written, refactor

Or maybe red test -> green test -> refactor**. I believe that this first step, to have red test, is important it you’re following Test-Driven-Development and writing tests first, or whether you’re writing tests after you’ve already implemented your feature.

The only way you can truly be sure that your code works is to use it to fix a broken test.

When you’re getting cocky/confident in your testing ability, you may start writing one or more tests, run them against your code and watch them go green first time. This lulls you into a false sense of achievement, for all you know your code could be returning nulls and failing silently. Instead, you need to have a failing test first and then fix it and commit it.

I need an example to convince you.

In summary, break your tests! Then fix them and commit them.

* I have no reference for this, maybe I made it up. ** A red test is a failing one and a green test is a passing one, because that’s the colours used by test runners to indicate this everywhere.