1 min read

On writing tests

Looking for inspiration from old philosophical texts on why and when it is a good time to write tests.
On writing tests
"If not now... when?" — Graffiti in El Call, Ciutat Vella, Barcelona

Inspired by a quote from the Chapters of Fundamental Principles (often translated as Ethics of the Fathers), a book of the Mishna, second century CE:

If I am not for me, who will be for me?
And when I am only for myself, what am I?
And if not now, when?
Pirqei Avoth (Chapters of Fundamental Principles), 1:14

If I am not writing tests for my own benefit, who will?

No one will do what is best for you better than yourself. Software tests are, first of all, an exercise of self-interest and self-preservation. They are a contract with your future self, to force you to play according to an explicit set of rules and goalposts that your past self has defined. Do take action — no one will do it for you.

If I write tests only for myself, what am I?

Testing shines when we do it within the context of a team project, or when several projects run concurrently on the same system. We write tests and run them defensively, to make sure we are not working against our teammates. We write them to let requirements define red lines that no one's code should cross. Writing tests makes us better citizens of our projects.

If I am not writing tests now, when will I?

The first best time to start writing tests is at the very beginning of the project, right after that pesky "initial commit" to the project's code repository. The second best time to start writing tests is now, whenever that is. Whatever you can come up with is a good start — you can refine it later. After all, a codebase is never complete.

Now... will you?