View Full Version : Writing unit tests for your code

07-06-09, 07:57 PM
Writing unit tests for your code:

Programmers when writing the code, perform some internal testing on their own and then pass the application to testers for testing. Which works most of the time, as long as the tester is testing everything everyday. The problem is that the code within any application evolve every single second with many programmers committing new stuff, which means more and more bugs are introduced. Finding these bug earlier pays off really well, but if the bugs are not caught on time, then it can become a huge problem. No one knows who has introduced this bug. Although the commit log shows who has committed what and when, but still it is very painful process. Even tester manage to catch the bug, the problem becomes which bit of code is causing this problem.

As a programmers, what we can do to save ourselves with this problem is to write unit tests. Unit test is a special kind of test which just test one unit of your application. The unit test are always meant to be very small and meaningful. So when you are writing a new functionality or either fixing a bug, it is very important to write a unit test that proves that you code is working in the intended way. In programming the smallest unit is a class. So whenever you write a class, you write some unit tests. The unit tests have to be run as a part of the building process. So everyday you run unit tests. If you find any unit tests failing then you go to the class where this test belong to see why it is breaking.

I am showing an simple example of unit test.

//Header file for MathClass

class MathClass

static double multiply(double x, double y);
static double add(double x, double y);
static double divide(double x, double y);
static double subtract(double x, double y);

//implementation for MathClass

#include "mathclass.h"



double MathClass::multiply(double x, double y)
return x * y;

double MathClass:: add(double x, double y)
return x + y;

double MathClass:: divide(double x, double y)
return x / y;

double MathClass:: subtract(double x, double y)
return x - y;

//driver program testing the MathClass
#include <iostream>
#include <mathclass.h"
int main()
cout<<"Testing MathClass"<<endl;

double xplusy = MathClass::add(5, 10);
assert(xplusy == 15);
cout<<"Add ok"<<endl;

double xminusy = MathClass::subtract(10, 5);
assert(xminusy == 5);
cout<<"Subtract ok"<<endl;

double xtimesy = MathClass::multiply(5, 10);
assert(xtimesy == 50);
cout<<"Multiply ok"<<endl;

double xdividey = MathClass::divide(10, 5);
assert(xdividey == 2);
cout<<"Divide ok"<<endl;

return 0;
}For people who doe snot understand what assert is. Assert assert that if the code inside the assert block evaluates to false then the program will through an error, saying assert found. asserts only works when your code in C, C++ is compiled and linked in debug mode. In release mode the asserts are disabled.

If you run this program, you should see something like this.

Testing MathClass
Add ok
Subtract ok
Multiply ok
Divide ok

The MathClass will surely break if someone uses the divide function as


The divide function is surely not handling the case of DIVIDE BY ZERO. As a programmer what you will do next is to fix the problem and also write a unit test to prove that this problem is fixed.

This is how you test a simple class. There are number of frameworks available for unit testing. On C++ the best is to use the CPPUnit framework. On Java one can use the Junit framework. Nearly all programming platforms has some kind of unit testing framework available.

From www.wikipedia.org (http://www.wikipedia.org)

In computer programming, unit testing is a software verification and validation method where the programmer gains confidence that individual units of source code are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a class, which may belong to a base/super class, abstract class or derived/child class.
Ideally, each test case is independent from the others: substitutes like method stubs, mock objects[1], fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation.

You can clearly see that the unit testing makes life of a programmer lot more easier. The unit test you write for your code should test the class fully, else non tested parts will still cause the problem.

This introduction applies to all kind of programming platforms.

To further learn about unit testing see the below mentioned links.