November 2011 Archives

Unit testing and CxxTest


There are many different kinds of testing while developing software. Unit testing, perhaps is one of the essential and basic form of testing which is usually performed by individual developers. Unit testing as you can guess from its name, is a test for a certain functionality, a unit. What we want to test is whether code(isolated piece of functionality) is doing what we intended to do. That's all. Testing for integrated parts or testing for its performance or any other tests beyond that functionality is not included in unit testing. For example, If we wrote a class that's supposed to read inputs from files, unit testing for that class will test whether that reading functionality works for all kinds of inputs we intended to use.

You can certainly write unit testing codes after you write your code for that functionality. However, a much better and effective way of using unit testing is to write the test first. Yes it does sound strange. Why would you like to write a test if there's nothing to test on. Think about it this way. Wouldn't it be much easier for you to study for finals if you somehow had the final tests with you before you take the tests? You will exactly know what you will need to study. It's not the same but similar for software development. If you know what to test before even you write it you get a much better understanding of the code you are going to write. This is what happened to me for the labs. When I didn't understand the specification for the lab fully I read the tests that was already written and I could immediately get better understanding of what I should write for the lab. Besides, you will have to write the test anyways, why don't you write it first?

There's this great framework that will help you write unit testing codes easily. It's called CxxTest framework. It uses perl or python to automatically create test classes for your software. All you need to provide is a header file for your test that declares test class which inherits CxxTest::TestSuite super clas in the CxxTest framework. So for instance if you would like to write a unit test for readInput.h class all you need to create is a new file something like readInputTests.h. That has a readInputTests class which inherits CxxTest::TestSuite. Next you write methods starting with lowercase 'test' followed by any name you want for that certain tests. In that method you write test cases you want to test. Final step is to use to automatically generate .cpp test file. will take header file and output .cpp file that can run the test. Usually you can automate this generation by putting it in the make file.

Unit testing is essential but it can be tedious to write all the test frameworks. With help with great framework such as CxxTest we mentioned, you can always easily change and add new unit test cases and automatically generate runnable tests on the fly.

About this Archive

This page is an archive of entries from November 2011 listed from newest to oldest.

October 2011 is the previous archive.

Find recent content on the main index or look in the archives to find all content.