To unit test or not to unit test?

Unit testing is just obviously the right thing to do, right? Not everyone agrees. Since at least one who does not actually cuts an awful lot of great code, he is well worth listening to. After all, it’s all about the code. I don’t actually write programs for a living (any more), so hats off to those who do. The inimitable Wil Shipley writes that Unit Testing ‘teh suck’ (between you and me, I’m still not really sure what this means above ‘sucks bigtime’). In the ensuing flamewar which can be read in the comments, there are some excellent points, e.g.:

• Unit testing gives you massive ROI. The earlier you test in your development lifecycle, the less you build on buggy code and the less you rework you need to do.
• Unit tests are less necessary for genius coders. If you are truly brilliant and write flawless code, unit tests are an overhead. The reality is that most of us are not this, even when we think we are.
• For a single app that is written once from scratch, which does not re-use code and which will not be re-used, and which will never have later patches or versions, unit testing does not add much value. For everything else, every time you write code without a unit test, you are building up a debt. That debt will need to be paid off with regression testing. At some point in the not so far future, those payments are going to be crippling you so much that it’s almost impossible to code. NOTE: this does not mean you have to unit test it, but you will have it test it. Wi’s alternative is that you a) try your very best to break your app and b) get it into a beta program with real users.
• It’s easier to code unit tests for some types of app that it is others. GUI code can be very tough to add unit tests for, unless you use a model-view-controller model, or more generally, ensure that UI is a very light wrapper. The best test of the usability and usefulness of a GUI is some tough, try to crash it in any way you can, beta testing. There are GUI testers out there, but YMMV.
• Just doing unit testing is not enough, you have to do it right. Ken Anderson wrote “I find that a number of programmers who think following the unit test mantra is the ”Right Thing“ are mislead [sic] and write freakin dumb unit tests. It is not worthwhile to write a unit test for something that will be absolutely obvious when running the application (does this button actually call this method when I click it – kind of like, if I stick this pencil in my ear, will it hurt?). Unit tests should be used for more complex tests that are difficult to verify easily, like, does the result of this complex calculation match the known good quantities I’ve calculated outside the application environment.”

Bill Bumgarner wrote a response to Wil, which shows how, for infrastructure code, unit testing has gone from being fad through a hype into a day-in-day-out technique of many great teams. He explains how Apple’s Core Data could not and would not have been finished without a lot more time or a lot more coders than they had.

Technorati Tags: , ,


Leave a Reply

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

You are commenting using your 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: