Wednesday, December 30, 2009

CppUnit 1.12.1 Installation Guide for Visual Studio 2008

I am sure- Scratch that. I know for a fact that this topic has been covered before, but extensive research (15 minutes with teh Googler) reveals that previous coverage has SUCKED. People just don't know how to write good instructions. Sure, all of the info is out there, but it is scattered all over the web in bits and pieces. Most posts start in noble fashion and then end mid way with cryptic notes about hurdles that require more than a cryptic note to overcome. I suspect the authors either just get tired of typing or they can't remember how they got around certain problems so they skim over them in their posts. Anyway, here goes...

Compile CppUnit
  1. Extract the CppUnit archive to the desired folder, hereafter refered to as %SRCROOT%. For example: C:\src
  2. Open %SRCROOT%\cppunit-1.12.1\src\CppUnitLibraries.dsw in Visual Studio.
  3. When prompted, convert each project to the ‘current Visual C++ project format’.
  4. In the Solution Explorer open TestRunner -> UserInterface -> MsDevCallerListCtrl.cpp and change the version on line 67 from “7.0” to “9.0”. Save the file.
  5. Select Build -> Batch Build… from the menu.
  6. In the Batch Build dialog click the Select All button.
  7. De-select (uncheck) all of the DSPlugIn projects in the list.
  8. Click Build.
Add CppUnit to a Project
  1. In the Solution Explorer right click on your existing solution and select Add -> New Project…
  2. In the Add New Project dialog Select Visual C++ -> Win32 from the Project types tree.
  3. Select Win32 Console Application from the Templates.
  4. Enter a Name for the unit test project. For example, if your main project is called “MyProject” a suitable name for this project would be “MyProjectTest”.
  5. Click OK.
  6. In the Win32 Application Wizard click Next >.
  7. Select Console application under Application type.
  8. Select Empty project under Additional options.
  9. Click Finish.
  10. In the Solution Explorer right click the Source Files folder under your new unit test project and select Add -> New Item...
  11. In the Add New Item dialog select Visual C++ -> Code in the Categories tree and C++ File (.cpp) under Templates.
  12. Give the new file a Name (e.g. testrunner.cpp) and click OK.
  13. Copy the test runner source code (see below) into your new source file and save it.
  14. In the Solution Explorer right click on the new unit test project (e.g. “MyProjectTest”) and select Properties.
  15. In the Project Property Pages dialog select Configuration Properties -> C/C++ -> General and add the follow path to the Additional Include Directories: %SRCROOT%\cppunit-1.12.1\include\cppunit
  16. Select Configuration Properties -> C/C++ -> Linker and add the follow path to the Additional Library Directories: %SRCROOT%\cppunit-1.12.1\lib
  17. Select Configuration Properties -> C/C++ -> Linker -> Input and add cppunitd.lib to the Additional Dependencies.
  18. Select Configuration Properties -> Build Events -> Post-Build Event and enter $(TargetPath) in the Command Line field and Unit testing… in the Description field.
  19. Click OK.
  20. In the Solution Explorer right click on the top-level solution and select Properties.
  21. In the Solution Property Pages dialog select Common Properties -> Project Dependencies.
  22. Select the unit test project from the Project list and select (check) the main project in the Depends on list.
  23. Click OK.
You are now ready to add CppUnit test classes to your unit test project! When you build the solution the test runner will output its test results in Visual Studio’s Output window.

Test Runner Source Code

Adapted from:

#include <iostream>
#include "cppunit/CompilerOutputter.h"
#include "cppunit/extensions/TestFactoryRegistry.h"
#include "cppunit/ui/text/TestRunner.h"

int main(int argc, char* argv[])

std::cout << "--------------- BEGIN CPPUNIT TEST ---------------" << std::endl;

// Get the top level suite from the registry
CppUnit::Test *suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();

// Adds the test to the list of test to run
CppUnit::TextUi::TestRunner runner;
runner.addTest( suite );

// Change the default outputter to a compiler error format outputter
runner.setOutputter( new CppUnit::CompilerOutputter( &runner.result(),
std::cerr ) );
// Run the tests.
bool wasSucessful =;

std::cout << "--------------- END CPPUNIT TEST ---------------" << << std::endl

// Return error code 1 if the one of test failed.
return wasSucessful ? 0 : 1;

No comments: