Red Green Repeat Adventures of a Spec Driven Junkie

Why I love tests - externalizing state

tl;dr: With tests, a program’s state is externalized in a separate file ready to be run at your whim.

Before testing

I have never released code into the wild without testing it first, even at some basic level. Even for hello world, that simple program in almost every programming language introductory page is basically just a test to see if a system has everything for you to program.

If every time a programmer produces new code, they always test part of their program. “Does the code do what I think it does? Am I making headway? Am I done?” This is a process that is continuously going on all the time. Write code, test it. If it passes, write more code. If it doesn’t pass, figure out why. Repeat.

If that’s the manual process that is happening for every programmer of every day… why not have the testing automatically done for you? Computers are VERY VERY good at doing things repetitively. Take advantage of this!

My breaking point

For me, I hate testing my code, especially if I have to ‘manually test’ every single part. I don’t mind testing out one part, the part I am working on. But the part where it once failed under this condition? Or how about the ‘happy path’? Does login and logout work? Do passwords work? As a project grows, there’s a LOT of different states to manage!

So, with tests, all these different states can be automated. Setup, test, teardown. No need to keep a cheatsheet to do things. (The cheatsheet is the test and it’s testing itself!)

Life with externalized state

When I started test driven development, I found automated tests lifted this weight of manually testing off my shoulders. I can write an automated test for the feature (or bug!) that I can run every single time with a short command instead of thinking of how to setup the test.

It’s basically externalizing the state of a program.

With this, I can keep my mind free from thinking of all these different states of a system. I write the test that sets up the state, it tests, and I’m done. As long as all the tests pass when I add new features to the program, I am very confident in my code and I can focus on the next thing.