Monday, January 3, 2011

Are you test infected?

That was the words a Finnish colleague used when first introducing me to unit testing using jUnit. It was back in 2002 when I had the pleasure of working with Capgemini Finland in a Helsinki-based project for about 18 months. The thought of writing test code to test my production code seemed good in theory since it would inevitably help me to find some bugs. But, wouldn’t it be at least twice as much code to write? And wouldn’t writing twice the amount of code make the whole project take twice the time to complete? I wasn’t in any way convinced that the practise of writing unit tests would make me a better developer.

I might be skeptical but I try hard not to be ignorant to new ideas. I tried writing a few tests and saw them pass without modifying my production code at all. I didn’t find any bugs, so what was the benefit of this? It didn’t mean I didn’t have any bugs in my code, it was just that since my design was not made with unit tests in mind I found it to hard to write any tests that really drove out the bugs. Instead we found them in systems testing and, like we were used to, had ourselves some late nights in-front of the debugger.

Since then there has been a good deal of writing and discussions on Test Driven Development (TDD) and I’ve been experimenting with both jUnit and TDD. Over the years I have come to solve more and more complex problems in my java designs and for some years I’ve been very reluctant to write or change any code without writing a test first to prove to myself what has to be done and that my code meets the requirement without introducing new bugs. Thanks to the excellent jUnit support in NetBeans, my IDE of choice, it isn’t a huge effort to write some extra code just to be sure the production code really works.

Now I’m happy about being test infected since I have proved to myself over and over again that I save time by writing tests as I design and code. In my experience it doesn’t take more than a few hours of coding, even on a very small project, to have a test fail unexpectedly and as soon as it does it means you have found a bug that you at some late point might have had to spend hours to debug.

So, if you are not yet test infected make sure to take a look at the xUnit framework for your language of choice. And if you are, please share your TDD experiences by commenting on this post.

No comments:

Post a Comment