Create CPPUNIT Test Steps:

 

·        Create your regular classes and methods

 

·        Create a new C++ that has the following

 

Remember that we need to use

 

/home/uni1/jeffery/371/cppunit-1.10.2/include/cppunit     As our CppUnit path.

 

v     Include the following in all your test units

 

#include <cppunit/ui/text/TestRunner.h>

#include <cppunit/extensions/HelperMacros.h>

 

 

v     Class inherit TestFixture from CppUnit

 

class FractionTest : public CppUnit::TestFixture

 

 

v     Define test classes, each test class correspond to one method in the user classes.

 

void testEquality()

{

Fraction a(2,6);

Fraction b(2,6);

 

CPPUNIT_ASSERT(a == b );

}

 

 

v     Setup a test suite, in the test suite add all the defined test units

 

CPPUNIT_TEST_SUITE(FractionTest );

//pass class name

 

CPPUNIT_TEST( testEquality );

CPPUNIT_TEST_SUITE_END();

 

 

v     In the main program

 

          Define test runner

 

  CppUnit::TextUi::TestRunner runner;

 

 

 

Add the test to the test runner

 

  runner.addTest( fractionTest::suite() );

 

Run the test

 

  runner.run();

 

 

·        How to compile

 

v     It's good to use make file

 

Compile your C++ files  using gcc

 

gcc -c filename.cpp

 

Compile the test program using

gcc –c filename –I{CppUnit Path}

 

Then link all the .o files

 

gcc -L{CppUnit Path}/lib -lstdc++ -lcppunitldl

 

 

After generating the executable file

 

setenv LD_LIBRARY_PATH {CppUnit Path}/lib

 

Now you can run your test program

 

 

 

You can download the following source code

 

    http://www.evocomp.de/tutorials/tutorium_cppunit/cppunit_tutorial_en.tgz

 

 

 

Notes:

 

·        To make a simple test Subclass you may use the TestCase class. Override the method runTest().

 

               class FractionTest : public CppUnit::TestCase {
 
 
 
·    If you'll have many little test cases that you'll want to run on the same set of objects use a fixture. 
  For TestFixture you could override setUp() and tearDown methods.
 
        setup() initialize the variables
 
        teardown() release resources allocated in setup()
 
 
 
·    If you need to set up your tests so that you can run them all at once create a suite of two or more tests.
 
 
·    Run your tests and collect their results, make your suite accessible to a TestRunner program
 
 

·        The following are examples on CppUnit macros:

 

v     CPPUNIT_ASSERT_EQUAL : hecks whether the first parameter is like the second one

 

CPPUNIT_ASSERT_EQUAL (Fraction (1, 2), Fraction (-1, 6));

 

v     CPPUNIT_ASSERT : it is checked whether the passed expression returns the value True.

 

CPPUNIT_ASSERT (Fraction (1) == Fraction (2, 2));

 

v     CPPUNIT_ASSERT_THROW : is used to check whether the passed expression throws an exception of the passed type.

 

CPPUNIT_ASSERT_THROW (Fraction (1, 0), DivisionByZeroException);