Jer on Rails

Jeremy Weiskotten on web development, startups, beer, and other stuff.


The Forgotten Benefit of Unit Testing

Why Write Test Code is a good overview of some of the benefits of writing automated tests, including unit tests. There’s another benefit that’s frequently forgotten when people discuss this topic.

High cohesion and low coupling. Better known as good encapsulation.

Unit tested code is often designed (in a pure test-first TDD process) or refactored into well-defined classes that take responsibility for their own state and behavior. This is necessary in order to isolate the class (or unit) under test, giving components better-defined interfaces and minimizing dependencies between collaborators. Otherwise your tests tend to be complicated to set up, slow, and brittle — harder to create and harder to maintain.

So unit testing isn’t just about testing, it’s about code design, and at some level it’s about architecture. Testing considerations drive small (micro) design decisions which inevitably influence larger (macro) design decisions. I think this is a key difference between the “Agile” and “waterfall” design processes. Agile projects are designed from within based on evolving requirements, from the micro to the macro, where waterfall projects are designed more from the “big picture” by Man Behind the Curtain, cascading down into the minutia.

Tags: , , , ,

One Comment

  1. On September 12th, 2007 at 6:21 am Mihai Campean said:

    Indeed, this aspect is often forgotten when discussing the benefits of unit testing. Thanks for the reminder.