Why is software unit testing important?

Lets do some cooking

First product to be tested ©flickr.com

Cooking? You serious?

Yes I am. Cooking is pretty much a good analogy to unit testing.

Lets start off by making an omelette. What ingredients do we need? Well not that much, but could be something like:

Eggs

Milk

Bacon

Cheese

Salt

and more

Next thing we (hopefully) do is to inspect, evaluate and verify our ingredients if they suit our needs.

How many eggs do we need?

What kind of milk? Spoiled? Bio?

Frying pan heated enough?

Is it really salt or is it sugar?

And so on…

As you probably noticed, we are testing our ingredients or units. As we are in the process of creating our product, we do test our work and each component throughout.

But we’re not testing the whole product.

In some cases one may simply take each component and make the product, without looking at the conditions of them. You will get the same product in the end but there are implications:

When finished you can’t change a component

Too much milk used? To bad, omelette is floating in water

Used sugar instead of salt? Yummy sweet omelette

Milk was overdue but you didn’t notice? Maybe you won’t taste it right away but it could have consequences later on

So why is unit testing important? We do it to ensure every part of our product works as defined while developing it. This way problems will be detected before we ship our product.

Unit testing is only one kind of tests we can do, there are many more. A good overview of choosing the right testing tool for your needs is given by Sebastian Bergmann and Ciaran McNulty¹

Embracing the Test Pyramid⁵

Developed by Mike Cohn, the test pyramid is a concept about the amount of tests, where you should have more low-level unit tests than high level end-to-end tests running through a GUI.

There is also a relation between speed and cost of money in the same way. Unit tests are per definition faster than end-to-end tests using a GUI. Going up in the pyramid the cost of maintenance will increase significantly.