Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼


Unit Testing: Is There Really Any Debate Any Longer?

On a panel several years ago, I was asked what was the greatest benefit that Agile had delivered to me personally. It took me no time to respond "unit testing." (While this answer is not historically accurate — unit testing precedes the Agile movement — it's clear that the Agile exponents made it a widespread practice. In large part, because of Kent Beck's lapidary JUnit implementation, which has been widely copied to most major languages.)

The specific benefit I — and many other developers — have enjoyed is quite simply less time spent in the debugger. Today (and for years now), I write code and then I write unit tests that exercise the edge cases and one or two main cases. Right away, I can tell if I missed something obvious or if my implementation has a slight burble that mishandles cases I expected to flow through easily.

This doesn't mean I can assert that I now feel comfortable my code is right. But what I do know is that it's better because it's been partially tested. The result of this is that the long sessions in the debugger trying to find why, for example, some data packets have an unexpected zero in the last byte, are pretty much done with. An error like that — invalid values or an improper format — will be caught right away by my tests. Previously, I'd have to wait for the bug to surface, then trap it, back up through code, examine and test it, and keep following the data up the call chain only to discover that at the very start when I created a substring, I miscounted by one the number of bytes to extract. Those days of 3 AM debugging are gone. Most of my debugger sessions today are about logic errors that transcend the unit level. I am happier and more productive.

I also enjoy another considerable advantage that I did not have prior to unit tests. As the collection of tests grows, the tests themselves become monitors of sorts that watch over my code to make sure it has not shifted unexpectedly beneath me. Because my code coverage is in the 75%-80% range, I have a lot of these monitors watching out for me. I run them all as a regression sequence any time I make a change that might have more than local impact. When I make a big change, I can tell right away if I have unhooked anything. For example, the other day, I changed the name of a major package. There were references to it throughout my code base. I had the IDE do the refactoring. When the compilation worked the first time, I felt a first measure of comfort. When all the unit tests passed, I had a high level of confidence that the change had been implemented non-disruptively. Without unit tests, I would never have made the change, because of the risk that it would cause problems for me far down the chain.

These benefits are not unique to me. Many Agilists view tests as enabling flexible development. In fact, the edgier subset among the Agilists won't code at all unless they have tests written beforehand. This approach is called Test-Driven Development (TDD), which they swear by, but which does not appeal to me. Regardless, the fundamental recognition that by writing unit tests, you assure yourself of the ability to make major changes easily is indeed a universal recognition.

However, the recognition is not universally embraced. A reader recently wrote to me, grousing about the current vogue that so values unit tests. His view was that the test are a waste of time. I've heard this view before. Unit testing has not been a waste of time for me, because the time it costs to write the tests is recovered from the shortened debugging cycle. And I gain the ability to make changes to the code with confidence. But the classic complaints often point to exactly such large changes. Namely, that if — in the Agile spirit — you throw away a bunch of code because of a shifting requirement, you also have to throw out a bunch of unit tests. Because unit tests typically represent 50%-75% of the size of the code base, you're chucking 1.6x code, say, for every 1x that needs to be changed. This is true, of course, but there's no way out of that bind. I could write untested code that I dreaded to debug and then I could feel better about having to dump it. But that's not exactly a happy place either.

One place where the complaint rings true is psychologically. The more code you've written, the more attached you become to it and the less you're willing to throw it away. And tests only add to this resistance. But the reality is that code is rarely stripped off and dumped en masse. Rather, some pieces are replaced selectively and others are modified. In this process, tests once again help assure that the code works as expected.

Most organizations today use unit tests in varying degrees. In a recent talk, Jeff Atwood of Coding Horror fame said he uses tests selectively — mostly when he's testing package interfaces. Others, as I pointed out, are more committed to their use throughout a project. If you're among the hold-outs who don't use unit tests at all, you're missing an excellent opportunity to make your code better and to improve your coding experience. As I said earlier, of all the practices the Agile revolution has brought to the mainstream, this is probably the one that will save you the most time and aggravation.

— Andrew Binstock
Editor in Chief
[email protected]
Twitter: platypusguy

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.