Why Do I Write Unit Tests?

There are many articles and blogs out there explaining the advantages of AUT. Here is my small take on the question “why do I write unit tests?”

1) I really want to keep my boss (and my wife) happy. When he’s happy, he lets me be. When he is happy he doesn’t feel the need to ask questions which I need to answer. And most important, when he is happy, (And I am lucky) on that yearly review I might just score that nice fat raise I’m waiting for.

So, how to make a boss happy? luckily for programmers that’s real simple. We just need to finish a little bit more the we committed to, a couple of days before the deadline with zero defects. Although I’m not sure AUT will help me achieve the first two (actually I feel they do help on this, I’m just not sure), actually testing the code is probably mandatory if I ever want to achieve the last.

2) I really like doing new things all the time. However, in order to start something new, I first must finish what I’m working on now. And before I can say I’m finished I really should test it. Having a nice set of tests allow me to say: “look, here are my 20 test cases for these 2 simple methods. The code is tested. What’s next?”

3) The last thing I want are incoming defects. I hate it when the QA guy (or even worse a customer) pop that horrified “you have this bug” email. Not only that this does not make my boss happy, usually those guys have the really weird notion that I should actually fix the bug. In most case this kind of prevent me from finishing what I’m doing NOW and starting a new thing. I really do need to properly test things if I ever wish to avoid this.

4) I like writing code. I’m not too keen on manual testing. I’m a programmer not a tester. Writing code is what I do for living, it would be a real shame if I didn’t enjoy it. Still, I really do need to test my code properly. Did I’ve just managed to convince everyone that I can test my code by writing more code? cool!!! I can work with that.

5) I’m lazy. I’ve just finished this 4 weeks coding sprint. Oops now I need to make sure its tested. There’s a real chance I’ll really attempt to manually test it, once!. If my boss really push me hard (and I mean hard), I might be willing to attempt that again. You might as well shot me in the head before asking me to repeat it for the 3rd time. I am however willing to invest some effort in automating this process (after all its just writing some more code).

6) I have a real short memory. Although I can remember my childhood phone number and various other useless pieces of information, in a couple of weeks most likely that the guy sitting next to me will remember more on what this code I’m writing now does. I’ll will be really amazed (shocked actually) if I will remember how to manually test it all over again. Writing the test down is the only sure way I know to remember how to test things. (With some luck, reading the tests will also jump me started on what I was thinking when at that time).

But seriously, after doing TDD for some time now it really helped me achieve all these things.

Done right AUT will:

1) Reduce the defect rate. Which both tend to keep my boss happy and reduce the times I need to go back to fix them.

2) Help me gain enough confidence in my work. Confidence which allows me to move onwards without self doubts and fear.

3) Turn the tedious task of manual testing, into something I can repeat over and over again. (It’s actually fun from time to time to fix a defect and be amazed that ALL the other tests are showing green.)

4) Document my work in a manner which helps me (or anyone else) understand what needs to be done when coming back later on, and has a good chance to stay updated as time go by.

There are many other reasons as why do writing automated tests is really good. But at the end of the day these are the reason why I do it. (oh and the fact that everyone here would make fun of me if I didn’t).

TOP