Putting in the time to setup and use unit testing can be a tiresome task, but it is well worth it in the end. I find it sometimes difficult to do something that slows down my progress in hopes that in the future it will benefit me greatly. This is my battle with unit testing, I always worry about how much time I am putting into it but in the end it always makes things run smoother. Unit testing is the practice of isolating units in a program so that they can be tested individually. These units can vary from being a single function or could be a small program. It all depends on the project. The programmer's job is to determine how to break up his project into units and test each unit sufficiently. As I said before this is time consuming and I feel many people neglect unit testing because they cannot see how something so time consuming could save them a significant amount of time in the future.
The unit testing software we used is cxxTest. CxxTest is a testing framework that we used to help streamline the way we do our unit testing. Picking the right testing framework is important. It can depend on many things like the testing needs of the project or what kind of programming environment you are working in. The thing I immediately liked about cxxTest was that was that it was simple to use. In my opinion testing frameworks have difficulty balancing simplicity and functionality. Some testing frameworks are simple to use but lack the functionality required. In an opposite case some testing frameworks have many useful capabilities but are very complex to use. CxxTest does a good job of balancing this, it is simple to use and provides enough functionality for what is needed to be done.
To use cxxTest the first step is to make the header file where you wish to make your tests. As an example I will talk about how we tested our readInput function. As said the first step was to use make the header file which we called "readInput_tests.h". Now if we plan on using the cxxTest capabilities we first need to include it. This is really simple, at the top of our header file we simply included the test suite, "#include <~path to suite~/TestSuite.h>", and the function header file, "#include "readInput.h"". Secondly you need to declare a class that inherits from it, "CxxTest::TestSuite". After this we could begin writing our tests. This was really easy to set up. Now we had access to many functions such as TS_ASSERT() and TS_ASSERT_EQUALS().
From this point cxxTest will use the information in the header file to actually write the cpp file that will implement the testing for you. So for our readInput_tests.h file cxxTest will automatically generate readInput_tests.cpp when the appropriate cxxTest commands are used in the compilation. We automated this process in our Makefile so that every time we compiled our code we would run unit tests on all of our code. The great thing about this is that once you make the unit tests you can reuse them indefinitely.
One of my favorite benefits from this is that every time a new unit is to be tested, all the other units are tested. So if for some reason the new unit causes some other unit to error we could catch this problem quickly. The code is not allowed to regress to a buggier state because of the unit testing that was made. This ensures quality throughout the entire development process, and in my opinion once cxxTest is implemented the addition of more tests is very simple. It is for this reason that I believe unit testing is a good idea for almost all programming projects. Unit testing has a steep upfront cost but you can view it like an investment. After you pay the upfront cost it will continue to benefit you during the entire development process.

Recent Comments