Pragmatic unit testing : An introduction

Towers of Hanoi Often when I talk to customers about unit testing I get similar reactions. Typically unit testing is recognized as a good thing; it’s something that should be done and it improves quality. But together with this positive view there are always a number of reasons why they are not unit testing. Most of these reasons are perfectly understandable. “I don’t understand unit testing”. “Why is unit testing valuable to me?”. “I can’t work out where to start”. “How do I unit test this code?”. “It’s going to take me longer to write if I write tests too”. If I’m honest, when I first met unit testing that’s how I felt too. So why is this? Why, when we agree that unit testing is a good thing, is it so hard to actually start doing it?

I think a large part of the reason is that most developers work with existing code. We rarely get the luxury of starting something new, wiping the slate clean of all the old practices and applying some new ones afresh. Maintaining code over time is simply harder than writing new code. Ironically, that’s one of the reasons why unit testing is important! To make matters more difficult most of the unit testing advice and examples, in books and on the Internet, focuses on writing new code, not introducing unit testing to existing code. That’s simply because it’s more complex, depends on many factors, and typically needs to introduce other topics that can end up causing confusion.

Hence my reason for writing this post. Over a series of posts, I want to try and provide some realistic and pragmatic guidance on introducing unit testing to existing code, and in doing so stimulate a conversation on the subject. I know there’s a need for this guidance, and I’m hoping that there are also developers out there who’d like to share their actual experiences, good or bad. I’m not claiming to be an authority on this subject, only an interested developer who likes unit testing. Hopefully by the end of the series there will be more developers who like unit testing, and have a better understanding and more confidence to start.

What others are saying

 avatar
Bill
Permalink to this comment 13 August 2008 13:13

I look forward to this series. When I imagine restrospectively adding unit tests to the tons of existing code we've got my brain freezes up!

Comments are closed for this post