Testing can be a challenging task for a programmer. Especially when using source control systems with multiple developers working on the same project. There are several strategies to go about tackling this. In this entry and I am only going to talk about unit testing. Unit testing is done by testing small portions or segments of the code to ensure correct functionality. The idea is to test individual but complete units so that developers can test and debug as they write more code. Like white-box testing, this method is used for validating the design and the inner workings so that the functionality is clearly known to the programmer throughout the development process. Typically, we write tests for each method or function so that if in the future any problem is encountered, it can be identified and fixed quickly. Before this class, I never had the opportunity to use this style of testing and I am quite impressed by it. Not only it helps you spot bugs in the early stages of development but also helps you save a significant amount of time in debugging. It can be painful to write test cases after a project is complete. You can be lost simply answering questions like, Where do I start? Which test needs to be called when? Which cases can be integrated? Etc. In situations like these, you might end up writing a entire test plan which can be a project in itself. Therefore to avoid such circumstances, it is essential to write out tests at an earlier stage of coding so that we are able to identify and isolate small parts of our code for robust testing.
Now we know that writing tests for every function/method or class can be very tedious and time consuming, but is there a way to automate the execution of these tests? For our project in this class, we have been using cxxTest for automating unit testing. This framework allows us to define tests for functions or methods by creating a header file and a class that inherits from the test suit. Once defined, the calls for these test cases can be automated using a makefile, so that they are all called automatically in a consistent manner. You can also run tests with assertions. For example, TS_ASSERT (expr), verifies if the given expression is true or not. TS_ASSERT_EQUALS (x,y), checks to see if x is equal to y. More assertions and info on cxxTest can be found here: http://cxxtest.sourceforge.net/guide.pdf. Using cxxTest, we have been able to quickly pinpoint the changes that cause a test to fail, helping us resolve issues as soon as they arise. The framework can be a little challenging to understand at first. But once you have the individual tests created and added to your compiled script, then it only becomes a matter or calling a command to execute all the tests. As a programmer, I would strongly recommend writing small test cases during development rather than waiting till the very end. It makes testing more consistent and debugging a lot easier.

Recent Comments