Tuesday, July 22, 2008

Pickles and Milk

I've recently read an interesting entry on Roy Osherove's blog, where he makes a simple analogy to explain the differences between unit testing and integration testing. His basic premise is that unit testing is akin to a group of people paying with separate checks, while integration testing is more like the entire group paying with a single check. You can read about it here.

While I love the simplicity of this analogy, I find multiple flaws with it. I agree with one of the commentors that it isn't a great analogy because the process of getting money from everyone doesn't relate much to testing. However, the larger issue that I have with it is that it paints an either/or scenario. The group is never going to pay with separate checks and with a single check.

So, I have thought up a similar analogy that I believe is a little more appropriate.



The Pickle

Lets say we have a pickle. How do we determine if it is a good pickle? We can probably come up with a set of checks, such as..
  • Is the the juice tangy or acidic enough?
  • Is the pickle stock crisp?
  • Others?
These tests ensure that the pickle is fresh. They are how we assure the quality of this pickle unit.
The Milk
Now imagine that we have a glass of milk. We can define an independent set of quality checks for this unit, too.
  • Is the milk sour to the taste?
  • Does the milk smell funny?
  • Other?
These unit tests are used to validate the "goodness" of our individual units, regardless of how they may be used.



Pickles & Milk

Now, imagine we have an application (recipe) in which we wish to use these units (ingredients). The application calls for hearty doses of both pickles and milk. Similar to our unit tests, we should identify some quality attributes.
  • Does the milk curdle?
  • Is it just plain yucky?
Of course, the quality attributes that I would define for the recipe may differ than yours.

The point is, while the individual units might pass all their checks, there still may be some integration issues. Unit testing and integration testing serve different purposes. If you have unit tests, but no integrations tests, you may find yourself in a pickle.

Monday, July 21, 2008