Software testing is just like tasting a cake. Sort of. You know if you’ve made a good cake when it tastes and looks good, but what if something went wrong and it doesn’t look or taste good? How would you know where the problem was? Was the recipe just wrong? Did you not use enough raising agent? Was the oven temperature correct? Working out where the problem was can be very difficult.
These same two problems exist in software development. Firstly – Does your code ultimately do what you wanted? This is something that can be tested with ‘functional testing’, which tests the overall ‘function’ of your code. Then there is still the problem of knowing whether each of the individual components worked correctly. Testing of individual components is called ‘unit testing’.
With baking a cake as well as writing code, you may find both functional and unit testing to be helpful.
Functional Testing – Tasting the Bake
In the Great British Bake Off, the contestants find out how good their bakes are when the judges taste them. At the end of each bake the cakes, pies or pastries they have made are ‘tested’ by Paul and Mary: do they taste good? do they have a soggy bottom? do they look appealing? And so on. In the software world, this testing of the final output is called ‘functional testing’ – you test to see whether the program or application functions correctly, or does what it’s meant to. In baking this means having a good bake.
Functional testing of software, then, is has the end user/consumer/judge in mind – will they be satisfied? It considers the process as a black box, and asks “Does the software do what was required of it?”. This is certainly an important thing to test, but what if something goes wrong: Are there genuine bugs, or does it just not doing what you thought it would? If there are problems, how can you be sure of their origin?
Unit Testing – Checking the Stages of the Recipe
Sometimes in baking, things don’t go according to plan. The cake doesn’t rise. The pie has a soggy bottom. There isn’t quite enough mixture in the tin. Sometimes experienced bakers are able to work out what probably when wrong when they do their ‘functional test’ at the end of the bake. But often it’s not clear. But what if there were a way to test each element of the bake as you went along? If you tested each stage you would know where the problem was (and possibly be able to fix it before giving it to the judges). For software, this testing of the individual stages or components is called ‘unit testing’.
A Simple Recipe
Unit testing is about testing the individual steps in making the cake. Let’s say our recipe was something like this:
- Pre-heat oven
- Measure out ingredients
- Mix ingredients
- Add mixture to baking tin
- Bake cake in oven
When you’re making your cake, any one of these steps could go slightly wrong – you might not use enough raising agent, the oven might not be the right temperature. If you’re cake comes out with a problem – how can you tell which stage caused the problem.
Unit testing has the baker (or programmer) in mind – the person tasting or eating the cake doesn’t really care about each of the individual steps that went to making it – as long as the final cake is good!
Unit Tests For Cakes
A unit test for stage two of measuring out the ingredients could be test that the total weight of ingredients is equal to the expected value. A test for the adding the mixture to the tin could be that you have marked a level that you expect the mixture to reach – if it goes above or below that level, you would know something is wrong. If you wanted to be a bit complicated, you could come up with a test for the baking stage by monitoring or recording the temperature of the oven or cake to make sure it bakes appropriately.
I’m stretching this baking metaphor quite far now, but hopefully two things are clear:
- Doing these tests while baking probably would help you pin down problems if they came up. If, after doing these tests, there was a problem with the final bake, you could look back at the results of your tests and get a sense of what went wrong.
- Creating and running these tests could be very time consuming, and you probably wouldn’t bother.
Both of these points are true of software too. Unit tests are widely recognised as a powerful way of reducing and diagnosing errors in code, but they can also come across as a bit of a drag – “why am I writing tests when I could be out their coding/baking?”
Thankfully – for software at least – there are frameworks out there which help in writing unit tests, and also make it easy to run a suite of tests for the whole project. I’d recommend taking a look to see what frameworks are used in your favourite projects, and for your favourite languages.