CPPUnit for Java developers – Part 1

Me being a hard core java guy found it pretty tough to understand the nuisances of cppUnit from the start. I really needed to figure out how things are done and what are these dlls getting generated from. I really didnt find much help on the web but somewhat vague entries on really explaining how everything works.

I wont explain how to setup cppUnit its in here: http://cppunit.sourceforge.net/doc/lastest/money_example.html.

Note: You just need to compile cppUnit, if you get some errors while compiling the cppunit package. This package is all you need for running my tutorial.

Create a empty VC++ Win32 console application.
Uncheck solution. check create empty project.

First of all cppUnit is similar to junit. you will need a main file as a point of entry. In my test below is the main file:

#include cppunit/BriefTestProgressListener.h
#include cppunit/CompilerOutputter.h
#include cppunit/extensions/TestFactoryRegistry.h
#include cppunit/TestResult.h
#include cppunit/TestResultCollector.h
#include cppunit/TestRunner.h

int
main( int argc, char* argv[] )
{
// Create the event manager and test controller
CPPUNIT_NS::TestResult controller;

// Add a listener that colllects test result
CPPUNIT_NS::TestResultCollector result;
controller.addListener( &result );

// Add a listener that print dots as test run.
CPPUNIT_NS::BriefTestProgressListener progress;
controller.addListener( &progress );

// Add the top suite to the test runner
CPPUNIT_NS::TestRunner runner;
runner.addTest( CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest() );
runner.run( controller );

// Print test in a compiler compatible format.
CPPUNIT_NS::CompilerOutputter outputter( &result, CPPUNIT_NS::stdCOut() );
outputter.write();

return result.wasSuccessful() ? 0 : 1;
}

The above file is almost same for all tests. Next write a class which needs to be tested. Lets take
a simple helloWorld.cpp

#include helloWorld.h

int helloWorld::add(int a,int b)
{
int sum = a+b;
return sum;
}

helloWorld::helloWorld()
{

}

—————————————–

Its header file helloWorld.h is as follows:

#ifndef HELLOWORLD_H
#define HELLOWORLD_H

class helloWorld
{
private: int a,b;
public :
helloWorld();
int add(int,int);
};

#endif

In

Leave a Reply

Your email address will not be published. Required fields are marked *