CPPUnit for Java developers – Part 2

In my previous post we had created the classes to be tested. Now we will create the actual test classes from where we call cppUnit.
helloWorldTest.h
————————
#ifndef CPP_UNIT_EXAMPLETESTCASE_H
#define CPP_UNIT_EXAMPLETESTCASE_H
#include cppunit/extensions/HelperMacros.h
class helloWorldTestCase : public CPPUNIT_NS::TestFixture
{
CPPUNIT_TEST_SUITE( helloWorldTestCase );
CPPUNIT_TEST( add );
CPPUNIT_TEST_SUITE_END();
protected:
int a;
int b;
public:
void setUp();
protected:
void add();
};
#endif
=======================================
helloWorldTest.cpp
——————-
#include cppunit/config/SourcePrefix.h
#include helloWorldTest.h
#include helloWorld.h
CPPUNIT_TEST_SUITE_REGISTRATION( helloWorldTestCase );
void helloWorldTestCase::add()
{
helloWorld* hw = new helloWorld();
int mySum = hw->add(a,b);
int sum = 10 + b;
CPPUNIT_ASSERT( mySum == sum );
//CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, 1.1, 0.05 );
//CPPUNIT_ASSERT( 1 == 0 );
//CPPUNIT_ASSERT( 1 == 1 );
}
void helloWorldTestCase::setUp()
{
a = 2;
b = 3;
}
——————————————————
Now we have all the files needed for running cppUnit. You will need to build the project once. Then goto Debug->Start without debugging(Ctrl+F5)
You will get the results.

In

Leave a Reply

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