One of the biggest problems in adopting unit testing is the getting through the initial phase. Once the team gets the hang of it, it has the momentum to continue. At this time, the developers already understand the value of unit tests, and they have the motivation to write more, and write better.
But that’s after getting over the start-up hump. Before that, there’s a risk of falling into the unit testing death spiral of doom.
You’re eager to start. You’ve read all about unit testing and you know it’s the right thing to do. So you start writing tests, and running them. But you’re not a master yet, so these tests don’t run quickly. At first you don’t notice, since there’s only a handful of them. But as you continue to write more and more, you notice that every time you run them, you need to wait five minutes for them to complete.
And as you write more tests, these test runs begin to look like short coffee breaks. That’s ok one, two, maybe five times a day (if that’s decaffeinated coffee), but it’s starting to feel a little unproductive.
That’s the beginning of the death spiral of doom.
So what do you do? Two things. First you cut down the amount of coffee. And instead of running the tests all the time, you run them more sparingly. There’s a price for feeling more productive, though: Because you run the tests less, you find out if you broke something later than before. Then you need to understand what happened in the last 3 hour session that could have done this. And now you need to debug.
It feels like you’re working for your tests, instead of them working for you. Each time you run them, you need to go back a few hours back to fix something.
And this is when it hits you: Unit tests are more work than they are worth. And you stop running them. And of course, stop writing them, since what’s the point? You’ll never try it again. Or recommend to your friends. Unit tests are now etched in your memory as a traumatic experience you’re not willing to go through again.
Welcome! You’ve reached doom.
The thing is, it’s very easy to go around this problem if you recognize the initial symptoms: Being hyper-alert from the coffee and slow tests.
The key to avoiding the death spiral is making sure the tests run quickly. When they do, even hundreds of them run at less than a few seconds. You can run them all the time. Even in a space of a few minutes.
Think about it – you add a few lines of code, a test and you run. Everything’s ok? Continue incrementally. Something’s broken? You’re a couple of CTRL-Z’s away from returning to the safe zone. This method builds your confidence in the code, and you progress a lot faster. And there’s no going back to those cowboy coding sessions.