I am sure all of us have had to write a small program yourself as a homework assignment, maybe it was a program that sorts a list of integers. I bet it started out as you coding the small program in the best way you can. Now you want to test it to make sure you are getting the intended output, maybe you add a value to your list and make sure it gets sorted in the right place, or you delete a few values and then test your program to make sure they are gone and the remaining list is sorted correctly. If everything looks good you submit it to be graded. The tests you preformed were unit tests.
FOLDOC (the Free On-Line Dictionary Of Computing) defines unit testing as, "The type of testing where a developer (usually the one who wrote the code) proves that a code module (the "unit") meets its requirements." Meeting its requirements means that the software behaves as intended and a unit can be thought of as the smallest testable part of an application. In object-oriented programming languages, like C++, a unit is usually an interface, such as a class. The whole goal behind unit testing is to isolate each part of the program and check that each individual part is correct. The unit test itself is actually a strict statement that the unit must satisfy. Unit tests are used to find problems early in the development cycle, since that is much more convenient than having to go back and find the bug after the application was finished.
The actual testing can be done manually like my first example but is best done with a testing framework such as CxxTest for C++. Once a framework is in place and you know how to use it, adding unit tests becomes relatively easy. Running unit tests was indispensable for our last project, Iteration 2. My partner and I had unknowingly written the string regex incorrectly. We forgot to put a "." in the string regex and therefore our testing statement,
TS_ASSERT ( p.parse( readFile("../samples/forest_loss_v2.dsl")).ok ) ;
always failed when it tried to read "myData.dat" in the .dsl file. Without unit testing we never would have known the bug, all other tests we have ever run always worked fine. Sometimes you just don't know what input to test and this is where unit testing really pays off since it can be used to test all kinds of input, even whole files. The biggest challenge I have encountered with unit testing is just getting used to the framework and knowing what tests to run. My advice to someone about to get into unit testing with CxxTest or any other testing framework would be to not give up when you get frustrated when your tests fail. It is going to happen, just stick with it and use simple tests first, then work your way up to whole files. Believe me, you and your code will be thankful when finally, all your tests pass.