I think the name unit testing is confusing. What is a unit, and how do I test it? I was so confused, in fact, that I decided not to write unit tests for my code for a long time. I’ve learned a lot more about unit testing by now, though, and I think the name is accurate—but it’s still confusing. For our purposes, we will simplify matters by saying that a unit is a function. So to test a function, we need to run that function with some input and check that the function does what it is expected to do.
Suppose that we have a function called
sum, and we want to write unit tests for it. Before we start writing unit tests, we need to make sure that we know very well what
sum is supposed to do (perhaps a specification would help). For this ...