Categories: CategoryCpp

CppUnit is a unit test framework for c/c++.

see: * * CunnWiki-CppUnit * CunnWiki-VisualCeePlusPlus * *

Compile it with:

$ g++ main.cpp Test.cpp Something.cpp -lcppunit

Alternatives to CppUnit: *

How to use cppunit on windows from newsgroup:

To use the Windows GUI version of in Visual Studio
I think the following steps should work.
(I hope the following does not confuse anybody it is meant as
"a get you started" set of steps only.
Once, you get going re-read the documentation with CppUnit.)
1 Unzip
You should have a folder called CppUnit with several sub-folders.

2 Build the test framework library by opening the
CppUnit\ms\culib.dsw   workspace and then building culib.lib.

3 Build the GUI library by opening
CppUnit\ms\culib\culib.dsw  workspace,
this should produce a TestRunner.dll

4 Build the sample app

You should now be able to run the sample app and see the GUI version
in action. (If the following doesn't work - try comparing settings with this

One fairly simple approach to using CppUnit in your own project is

1 Open the menu item "Project|Settings" settings for "Debug Only"
and on the C/C++ tab add a Preprocesser definition say
"CPPUNIT_TESTRUNNER" we can use this to #ifdef out any
code from the release versions.

2 On the same dialog add some "Additional include directories:"
c:CppUnit\ms\TestRunner, c:CppUnit\test\framework,
Assuming the files where expanded onto your c drive.

3 Same settings dialog, Category C++ language, enable RTTI.

4 Switch to the Link tab, and in Object/Library modules: add

Save these setting. Now we can start adding the test code.

5 It seems reasonable to launch CppUnit every time you run your app
in debug mode. So if say a dialog app you could create the test runner
in the applications dialog constructor. To your Dlg.cpp file add:

#include "TestRunnerDlg.h"
#include "T_CTestHub.h"
#endif //_TESTRUNNER

and inside the dialog constructor add:

CAnyNameDlg::CAnyNameDlg( ... )
    TestRunner  runner;
    runner.addTest (CT_CTestHub::suite ()); ();                   //   <--   will start the tests here

// the rest of the constructor code ..

6 We need to create the CT_CTestHub class that inherits from TestCase
and a CT_CFred class that also inherits from TestCase.
The CT_CtestHub is just for convenience, you can keep adding more
class tests to this. The CT_CFred class will test the functions you

//in T_CTestHub.h
#include "TestCase.h"
class CT_CTestHub : public TestCase
    CT_CTestHub(std::string name): TestCase (name){};
    virtual ~CT_CTestHub();
    static Test *suite ();
//in T_CTestHub.cpp
Test* CT_CTestHub::suite()
   TestSuite* pTSuite = new TestSuite ("Full Tests");
   pTSuite->addTest (CT_CFred::suite());    // <-- tests your CFred class
   pTSuite->addTest (CT_CAnotherClass::suite());  // <-- test some others
   pTSuite->addTest (CT_CYetAnotherClass::suite()); // <-- test some others
   return pTSuite;
class CT_CFred : public TestCase
   CT_CFred(std::string name): TestCase (name){};
   virtual ~CT_CFred();
   void setUp ();
   void tearDown ();
   static Test* suite ();
   void TestDoThis();
   void TestDoThat();

Test *CT_CFred::suite ()
   TestSuite * pTSuite = new TestSuite ("CT_CFred");
   pTSuite ->addTest (new TestCaller 
   pTSuite ->addTest (new TestCaller 
   return pTSuite;

void CT_CFred::TestDoThis()
   CFred  Freddy;
   // Get Freddy into the state where you expect an 'x'
   assert('x' == FredObj.DoThis());

void CT_CFred::TestDoThat()
   CFred  Freddy;
   // more test code ...
   // CT_CFred::setUp is called once before each test and
   // unsurprisingly you can use this to set up any
   // objects needed for the test.
   assert(42 == FredObj.DoThat());

Sandwich all the test stuff between #ifdef CPPUNIT_TESTRUNNER
and #endifs to keep it out of the release builds.

Also, the "TestCase.dll" needs to be placed where the application can
find it when it runs, if you are just trying CppUnit out just copy it into
the same directory as the debug application .exe, if you start using
it a lot place it in the dll path.

I hope the above steps work well enough to get you going. Once you
have started you can set up some Vis Studio defaults and maybe create a
macro or two to save on some donkey work.

"Argos"  wrote in message
> Hi there,
> sorry for my vast ineptitude but while I would just love to give Cpp
> Unit a go, I just can't work out how to set it up :-(
> Could anyone please give me a simple step by step, assume I'm a total
> muppet, guide on how to do this (and please don't say read the
> documentation - I have and just plain don't get it)
> The project I want to try it on is a rather larg MFC based app I am
> currently maintianing at work.
> From unzipping Cpp Unit, how would I get to testing 2 functions that
> look like
> int CFred::DoThis()  and   char CFred::DoThat()
> ?
> Cheers in advance,
> Argos