CategoryTesting: TestingFramework for RegressionTesting of C++ programs.
See DirtSimpleCppUnitExample, WhoIsUsingCppUnit.
See CppUnit on Sourceforge
For the latest version of the software and for recent discussion about problems and usage, please go to
There is now a CppUnit wiki on Soureforge, at http://cppunit.sourceforge.net/cgi-bin/moin.cgi/FrontPage.
As of 11/18/04, it is easy to spam, difficult to remove the spams, and there isn't much discussion. However, that may change in the near future, so the below conversation is even more out of date then it already is.
As of 1/19/04, the wiki version was updated. Now there isn't any spam, but it is quite slow. Still, there are easy links to the very good documentation.
Philosophy of Test-Driven Development (TDD)
Assumption is the mother of all screw-ups; use TDD CASE software such as CppUnit. Don't assume that any part of your system works the way you intend it to -- be sure of it!
Proper and diligent use of systems like CppUnit gives your code the kind of credibility that makes it truly useful. It can be counted on by yourself and others. It can be extended, modified, and distributed with confidence.
Linking problem when using Helper macros ?
When you create a project and write its unit test suites, the work is made easier through the use of the so-called helper macros : CPPUNIT_TEST_SUITE_NAMED_REGISTRATION, CPPUNIT_REGISTRY_ADD and CPPUNIT_REGISTRY_ADD_TO_DEFAULT. The problem is that if you use those macros in the source code file of a TestFixture class (say MyTest as an example), and if you use a line like this one
runner.addTest( CppUnit::TestFactoryRegistry?::getRegistry().makeTest() );in your main() function in file main.cpp, there will have no test run at all !
The reason is simply that the link stage, one of the step of the build process, do not insert the object files (.obj or .o files) in the final executable if there is no undefined symbol in your main.cpp.
That way, the object code which contains the AutoRegister? static variables instantiation is not part of the final executable and is not able to insert oneself into the runner in the main() function.
You have to create an undefined symbol in main.cpp so that the mytest.o file is integrated with main.o into the final executable.
Trick committed by Michel Nolard
Could you explain more about that? I think I got the error you describe, but I don't know how to create undefine symbol.
Recent CppUnit Discussion >October 11, 03: Installation with Visual Studio .NET 2003 howto ?
>I want to use CppUnit with Visual Studio .NET 2003 Professional, Student Edition. Install.txt: "Quick Steps to compile & run a sample using the GUI TestRunner: - Open examples/examples.dsw in VC++ (contains all the samples) - Make HostApp? the Active project - Compile" This works !
"- in VC++, Tools/Customize.../Add-ins and macro files/Browse...". Well and with the (German version of) VisualStudio .NET 2003 Professional, I don´t know what to do. There is the German main menutray item "Extras" (English: "tools"), and there are the menu items "Add-In Manager" und "Macros". But this does not help me much, as I can´t load or register a DLL there. There is the rumour that I need a .REG file to register such a DLL first, and then it will appear in the "Add-In Manager". As I am a newbie to MSVS, I don´t know how to proceed
- With Visual Studio 6, you were able to add the DSPlugin.dll through the Add Macros menu option. In VS2003, it seem that you now do this through Tools->Add In Manager. There is information in the help regarding how to register the DLL so that will actually see the plugin. You'll find this under "Add in Registration" in the Visual Studio MSDN help. - JSprenger -
>Thanks in advance.
>Rolf Hemmerling (email@example.com)
>September 9, 03: Porting over CppUnit from VC 6.0 to VC 7.0
>I want to use CppUnit with VC 7.0 but I am not able to do that because of ObjModel?\addauto.h etc. files which Microsoft >has removed from .Net. I could neither find the header file nor the classes by grepping in the VC 7.0 files.
>It will be great you could help and give me some advise about how to solve this problem.
Use latest CppUnit development snapshot. It quite stable and compiles under VC 7.0.
- I find that the 'DSPlugin' project will not build in Visual Studio .NET 2003 (found in src\msvc6\DSPlugIn). It didn't work with 1.9.14 snapshot and it doesn't work with release 1.10.2 (the latest) The same problem reported by Abinav still exists. Stdafx.h wants to include '<ObjModel?\addauto.h>' along with other .h files that can't be found. Has anyone successfully rebuilt the 'DSPlugin' project? If so, you help here would be greatly appreciated. -- JSprenger -
For years I have wanted to use CppUnit, mainly because of the GUI features that I have never bothered with in my own UNIX text based test package. But, CppUnit repelled me in ways that I had trouble enunciating. Circa Nov 6, 2002, I studied CppUnit and friends (ConsiderationsForAndComparisonOfCeePlusPlusTestFrameworks) and concluded CppUnitCodeSmell. Resulting AndyGlewMinimalCeePlusPlusTestFramework.
Maybe I'm just a crank, but maybe my concerns are real.
I am new to CppUnit, and coming from PyUnit? and JavaUnit, I was a bit dismayed at having to create/change test names in 3 places... thus the birth of my CppUnitOnceAndOnlyOnceHack, a simple Python script that autogenerates the yourTests.h header file based on yourTests.cpp. So you write and change test names in one place only. -- HarlanWood
Old CppUnit Discussion Needs Cleaning Up
As of October 9, 01, all the stuff below this is pretty old. - Ted Graham
As of October 19, 01, MichaelFinney wonders if all the stuff below could be refactored somehow. What do you think?
November 2002, AndyGlew factored out the discussion TestingPrivateMethodsInCeePlusPlus a subtopic of ConsiderationsForAndComparisonOfCeePlusPlusTestFrameworks.
Similarly, created CppUnitExamples.
If nobody objects, as time permits, I will proceed with this cleanup, keeping useful stuff and eliminating stuff now discussed elsewhere
Old CppUnit Discussion
See EnhancingCppUnit for discussion.
(Objective C fans, see OcUnit.)
See also: CeeUnitTesting for testing discussion (currently focused more on C than C++ or C++Unit).
See also: RwCppUnit -- for C++ with RogueWave and no StandardTemplateLibrary
Great: I can be extreme and obsolete at the same time
I have been playing with CppUnit on SGI/IRIX systems. My company likes it. More importantly, I have played with Michael Feathers' enhancement where you can walk the inheritance tree and run tests as you go. I have run into a couple of problems with this. First, I would like to use friend declarations. That is, if I am performing unit tests of class Triangle, I would like to put a friend declaration in it for the test fixture that is exercising it. When I do that and also bolt in the tree-walk capability (following the BoardGame/Chess example), the compiler (SGI 7.3 compiler) complains about duplicate declarations of my test fixture. Why doesn't it like this?
Also, in TestSuite::deleteContents(), it loops over a vector and deletes each pointer plus contents as it goes. I would get a coredump in here after adding the Treewalk logic. I was able to get rid of it by doing m_tests.clear() instead of the loop. Then, the coredump goes away. I inquired about our compiler or SGI implementation of the Standard Template Library on their machines to see if that was a problem. Have you guys experimented with this stuff without running into these problems?
First there was KentBeck's Smalltalk unit TestingFramework. Then, there was KentBeck and ErichGamma's port to Java, dubbed JUnit (see JavaUnit). Now, we have MichaelFeathers' port of JUnit to C++, CppUnit.
Where will this fearless dive to the innards of the machine stop? C-unit? ASM-unit? Microcode-unit? (See ExtremeProgrammingBareMachine for a sketch of C51-Unit.) [See "A Note from a HW Guy" below"]
CppUnit is just like JUnit except different. Across platforms, Java is Java. Across platforms, C++ is GUI-less and unthreaded. Thread support for CppUnit would be easy to add. For GUI support, I'm adding a little annex. A dialog based TestRunner in a DLL under Microsoft Visual C++ 5.0. It can be run from any MFC app.
You can download it from http://www.xprogramming.com/ftp/TestingFramework/CppUnit/CppUnit15.zip
It's probably better, when you can, to connect through the links on the XProgramming software page. YMMV.
A Note from a HW Guy:
"Where will this fearless dive to the innards of the machine stop? C-unit? ASM-unit? Microcode-unit?"Well, I can certainly vouch that CPU microcode and hardware (RTL - register transfer language) uses a large set of unit tests in very similar frameworks. Although there are never enough...
While searching freshmeat, I noticed that someone has created a unix-based port of CppUnit with debugging output for use under Emacs.
The URL is: http://yaktest.sourceforge.net
CppUnit for testing persistent objects
How does one set up a test fixture to apply all tests to the same instance of the fixture object?
Problem: I was unsure how to use the STL map functionality (w.r.t. memory management) so I wanted to see how the map behaved as I added objects in different scopes.
Constraint: For each test in a test fixture CppUnit creates a new test fixture object. Thus for fixture <code>fixture1</code> containing tests <code>test1(), test2(), test3()</code>, CppUnit will create 3 instances of <code>fixture1</code> and call <code>test1()</code> on the first instance, <code>test2()</code> on the second instance etc...
Solution: Add a constructor to the <code>fixture1</code> class called <code>fixture1()</code>, insert all tests into the default constructor and add create an empty test <code>emptyTest()</code>. The <code>emptyTest</code> should be the only test added to the fixture. Thus when the <code>fixture1</code> object for the empty test is created the constructor runs all the necessary tests.
Has anyone tried to connect CppUnit to a Java GUI (perhaps JavaUnit's) using the JNI? Is there anything I should know before I attempt it myself? (I guess I'd probably want to build my cpp code as a shared object (or DLL on PC) to avoid restarting the gui). -- DaveWhipp
Dave, take a look at CppBridge by PhilGoodwin. It's been a while but I remember it being pretty neat. -- rad
I've got a port of the MS C++ GUI interface to WTL, if anyone is interested. Email me at mailto:firstname.lastname@example.org. -- Matthew Adams
There is now a version of CppUnit on Sourceforge:
I'd like to have a well-documented version with a focus on ease of use, and I'd like to relieve some of the maintainers of the various ports/versions of their workload. So why shouldn't we combine efforts?
I am considering using CppUnit in a VC++ environment and I had a few comments and questions:
Do you have any recommendations on how to organize the unit tests and library? Specifically:
Interestingly, when you build Release mode, the sample test "testDivideByZero" does not show an error, but it works in Debug mode. I tracked the reason down to the optimization options. It turns out the entire test case is optimized away. You would have to make changes to the fixture to make this work right in release mode, for instance storing the result in m_value1, or assert(result == 1). In this case you get a compiler warning about a "potential divide by zero", but the program works as expected.
My guess for how you would use CppUnit.
Keep your TestCase subclasses in separate files in their own subdirectory. Have a separate project for the test code. Build and run your test project after each application code or test code change. At regular intervals (maybe every morning) build your application project and run the application. This way you can do debug or release builds of your application which contain no unit test code.
I've been trying to get the example in the cookbook supplied with cppunit to work. Does anyone have a complete working source file (& makefile) that they can share? (I am fairly new to cppunit and c++ but I have been using junit and java for a while without too much difficulty.)
CppUnit on SourceForge falls under the GNU Lesser General Public License http://www.gnu.org/copyleft/lesser.html
1/15/02 Does anyone know if cppUnit will run under QNX Neutrino OS?
I couldn't find any better place to ask.
I have problems Compiling V1.6.2 on a Sparc/Solaris 2.5.1 machine. The linker is complaining at nearly every function in the StdC++ Lib. Does someone know a solution? And why is the linking not standard but divided (looks quite impressive ;) )?
My Sysad says our system is quite normal, although it's mixture between gnu compiler / libs and Sun linker (a hint?). Other Projects compile without complaining.
I am also having problems compiling v1.6.2 on a Sparc Solaris 8 box with gcc 2.95.3 and the Solaris 8 ld.
ld complains that "relocations remain against allocatable but non-writable sections". This appears to be a direct effect of the ld being given the '-z text' option in the libtool provided with the distribution. This flag forces a fatal error in ld under the conditions complained about. This appears to be a related to references to external data (in libstdc++) from position dependent code (i.e. a dynamic object).
According to the Solaris 8 Linkers and Libraries Guide on sun.docs.com this can be eliminated by building position independent code (which I presume means building it as a static library) or by directing the linker to produce copy relocations when referencing external data (which, as of yet, I have been unable to get it to do successfully).
p.s. I was able to successfully build cppunit 1.6.2 using the ld from the GNU binutils distribution.
Suggestion: use some filenames in the cookbook, and at the end of the cookbook, show the complete contents of what your files should look like. Otherwise I have no idea what goes where, whether I'm missing some #includes somewhere ... Yes, your tests are supposed to fail first, but it shouldn't be because it fails to compile for want cppunit working in the first place...
7-15-2002 Does anyone know of a port to C# of either CppUnit or JUnit? I don't like going back to "old school" techniques with a new environment and would like to have a CsharpUnit? if it's out there.
You might want to try NUnit - http://nunit.org/ or http://nunit.sourceforge.net - I found this in a Google search. There's also a page in this wiki too - NunitFramework -- DuaneMilne
Does anybody have used CppUnit on HP-UX with HP C++ compiler? I am trying to compile but the compiler is complaining about all stdC++ functions as well as it is not in position to define CppUnit NameSpace.
did Anybody use TestSetUp? class of CppUnit that provides decorator functionality i.e., to set up initial data using setUp and tearDown but I don't understand, how is this different from setUp and tearDown of TestCase class?
CppUnit contains both a TestCase and TestFixture class. When should one be used instead of the other. It appears that the only functional difference is that TestFixture contains Setup() and Teardown() to allow for the creation of objects for use during the tests. However, looking at the samples for CppUnit on sourceforge, there are numerous cases where TestFixture was used, but Setup() and Teardown() were empty.
Any hints on how to test template classes? Say I want to run a set of tests for a number of instantiations of that template class - can/should the whole TestCase / TestFixture class be templatized or should the test methods delegate to a number of instantiations of template functions doing the actual (generic) test?
Bit of a newb question, perhaps, but how does one manage stubs/mock objects in a real application? My problem is that I need to stub out class A to test class B in one test, and then can't test class A because the stub definition stops me from linking in the real version. The solutions I've come up with are:
2 is usually the way to go; besides making testing easier, it's often a good idea to SeparateInterfacesFromImplementation. Using an AbstractBaseClass is the most common way to do it in languages like C++ that don't support DuckTyping. You also might want to have a look at FearOfAddingClasses when you have time.
Are there any examples of how to use Properties?
See also CppUnitLite, NanoCppUnit