Unit Testing is to code coverage as Exercise is to weight loss

This is a quick post, following up on a tweet from a little while back:

"Unit testing is for code coverage like exercise is for weight loss. You're actually missing the point."

A great number of people in the software field appear to think the primary benefit of unit tests is test coverage. Some even talk about them as if they were one and the same. "No need for more unit tests, we have plenty of coverage already."

But code coverage is not the point of unit tests. Code coverage is a side affect. Some would say that the point of unit tests is to ensure a discrete piece of code does what you intend. This is a benefit. And a significant one at that. Others would say the point of unit tests is to influence design at a low-level. And here I think we're getting closer to the crux of it. Code that is easy to test is discrete, it has few dependencies (if any), and it serves one and only one purpose. When you have an entire system that is comprised of these small, easily testable pieces of code, you have a significantly higher probability that the system itself is malleable, flexible, extendable ... healthy.

The point of unit tests is to help ensure you are building and maintaining a healthy system.


3 comments:

  1. likewise the primary benefit of exercise is health, not weight loss. you don't actually lose that much weight through exercise alone, for many complex reasons, but you will get fitter and healthier.

    ReplyDelete
  2. I would say the same is true for static type systems in programming languages. The point of type systems isn't to prevent errors or make code run faster. It's to enable explicit human expression of intended design.

    ReplyDelete
  3. Always dubious to say what is "significant" and what is the "side-effect"...
    Different people have different viewpoints or needs... :)

    To me (having assumed maintainence of an old, quite complex application), by far the most value of adding automated tests is REGRESSION testing. Whenever we need to CHANGE something, to minimize the risk of accidently breaking something else...

    ReplyDelete