Jason in a Nutshell

All about programming and whatever else comes to mind

From Testophobe to Testaholic

Posted by Jason Baker on December 7, 2008

I know what you’re saying.  “TDD – that’s one of those newfangled programming fads.  Who cares?”  The answer’s simple:  I do and you should.

But you’ve been told that before.  Many times.  I know this because I was like you once.  So I’ll spare you the lectureon why you should become a TDD fanatic and sacrafice three lambs a week to the gods of XP.  I can do this because if you give me an hour’s worth of your time, that will happen on its own.

So rather than extol the wonders of TDD, I’m going to teach you how to test testing.  Humor me on this.  If I’m wrong, I won’t bother you again.

You ever have one of those days that go something like this:  *Program crash* “Whoops!  Field x is now a member of object y!  Let me change that.”  *Program crash* “Oh darn, forgot to check for null.  Silly me.” *Program crash* “<insert other stupid bug>” ?

Well, here’s what you should try.  Make a unit test for that one piece of code that you seem to have so many problems with.  You don’t have to spend any time learning any mocking frameworks or unit test suites.  Just write a mini-program that will make sure it works.  Assuming that you’re into the whole modularity thing, it probably won’t take long (but it may involve some thought).

Now that you have that out of the way, run that test every time you compile your code.  I guarantee you you’re not going to have problems with it much longer.

And that’s all you have to do to start your journey to being “test infected.”  Easy eh?  But I already know what’s on your mind now.  TDD is more involved than just writing a unit test.  And what about this xUnit contraption you keep reading about?  All that will fall into place in time.

Eventually, you’ll feel that writing these tests will result in a lot of code duplication.  That’s when it’s time for you to learn an xUnit framework.  After that, you’ll find that you’re asking yourself “Do I have enough tests?”  and “How the heck do I test this?”  And that’s when you’ll adopt TDD.

You see, the idea behind TDD is that you write your tests before you write the code.  The main benefit isn’t that you get to make sure that your tests aren’t slanted towards working (although that helps).  The main advantage is that you will know that you have enough tests because you’ve already planned that up front.  And you know your classes will be testable for the same reason.

So, now it’s just up for you to try it out.  Don’t say I didn’t warn you though.  Chances are, you’ll become test infected just like I did!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: