You are on page 1of 11

Unit Testing C++ Code CppUnit by Example

Sreejith Madamana 16.Jan.2011



This document is a beginners guide to CppUnit. It takes you step-by-step through the process of
creating a simple CPP-Unit test project along with giving you a few code snippets to copy-paste
and modify.
CppUnit is the C / C++ port of the xUnit family of unit-testing frameworks. It is an open source
project available for download at http://sourceforge.net/projects/cppunit/.
Getting Started

Setting up CPPUnit
Download cppunit-1.12.0.tar.gz and un-compress it on your system. Open
CppUnitLibraries.dsw which is located in cppunit-1.12.0\src directory and compile it. After
successful build, you should see cppunit-1.12.0\lib directory.
Setting up Your Test Project
Create MFC DLL project

Project Settings:
Project->Properties -> Configuration Properties ->General Tab
Set the Out Put Directory as per your project requirement.
Configuration Type: Application (.exe)
Byte Character Set: Use Multi-Byte Character Set




Project->Properties -> Configuration Properties -> C/C++ -> General
Additional Include Directories: Provide the path to the CppUnit




Project->Properties -> Configuration Properties -> C/C++ -> Optimization: Disabled (/Od)




Precompiled Headers: Not Using Precompiled Headers

Project->Properties -> Configuration Properties -> Linker -> General -> Additional Library Directories
Add the CPP -Unit library file path




Project->Properties -> Configuration Properties -> Linker -> Input -> Additional Dependencies
Add Cppunit and Test runner libs






At this point the test project will build successfully.
Getting the Runner up
To get the runner UI up, you will have to create an instance of the runner class. Go to in the
<WhateverYourProjectName>.cpp. If you want to use the mfc UI that most are accustomed to seeing
include <cppunit/ui/mfc/TestRunner.h>
Add the following two lines in the InitInstance () function.

CPPUNIT_NS::MfcUi::TestRunner runner;
runner.run();










Adding Test fixtures and tests
Each of your test fixtures will go in as a new class [A test fixture is a group of related tests]. Create a
new header file. Include <cppunit\extensions\HelperMacros.h>. Write down the
definition for a new class. Have it derive from CPPUNIT_NS::TestFixture.
Each of your individual tests will go in as new public functions. Write the function declarati ons
down.
Keep the function names long and descriptive enough so that every time the test fails the coder
breaking it knows exactly what broke.
Within the braces of class declaration for your test fixture class add the following macro calls
CPPUNIT_TEST_SUITE(<Whatever Your Test Fixture Class Name Is>) and
CPPUNIT_TEST_SUITE_END(). Between these macro calls insert a statement of the following
format CPPUNIT_TEST(<Whatever Your Test Function Name Is>)for each of your tests. This is the way
in which your function is declared as a test.
Example:
Writing Unit tests:
Consider the class: Calculator
class Calculator
{
public:
Calculator();
~Calculator();
int add(int op1, int op2);




};
Calculator::Calculator()
{
}
Calculator::~Calculator()
{
}
int Calculator::add(int op1, int op2)
{
return 0;
}

Write Unit test for the method add()
Create Test class CalculatorTest
class CalculatorTest: public CPPUNIT_NS::TestFixture
{
public:
CalculatorTest();
~CalculatorTest();

void addTest();

private:

CPPUNIT_TEST_SUITE(CalculatorTest);


CPPUNIT_TEST(addTest);


CPPUNIT_TEST_SUITE_END();

};
CPPUNIT_TEST_SUITE_REGISTRATION(CalculatorTest);
CalculatorTest::CalculatorTest()
{
}
CalculatorTest::~CalculatorTest()
{
}
void CalculatorTest::addTest()
{
Calculator objCalculator;
int _InputParameter1 = 2;
int _InputParameter2 = 3;
int ExpectedValue = 5;
int _return = objCalculator.add(_InputParameter1,_InputParameter2);
CPPUNIT_ASSERT_EQUAL(ExpectedValue , _return);




}
Lets execute the test and see what the output is:


Equality assertion failed
- Expected: 5
- Actual: 0
The output shows us that my test failed (obviously as I am returning a 0 in the add() method).
Lets fix the code now to return op1 + op2. The output after the fix is:

Change the method implementation

int Calculator::add(int op1, int op2)
{
return op1+ op2;
}


















Conclusion:
If you are a C++ developer, you can take advantage of unit testing using CppUnit. Since C++ does not
provide some features of Java and .NET languages, you have to work around those limitations.
However, CppUnit addresses these by providing helper macros.
Wish you a happy test-first development experience with CPPUnit!

You might also like