Compile CppUnit
- Extract the CppUnit archive to the desired folder, hereafter refered to as %SRCROOT%. For example: C:\src
- Open %SRCROOT%\cppunit-1.12.1\src\CppUnitLibraries.dsw in Visual Studio.
- When prompted, convert each project to the ‘current Visual C++ project format’.
- 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.
- Select Build -> Batch Build… from the menu.
- In the Batch Build dialog click the Select All button.
- De-select (uncheck) all of the DSPlugIn projects in the list.
- Click Build.
- In the Solution Explorer right click on your existing solution and select Add -> New Project…
- In the Add New Project dialog Select Visual C++ -> Win32 from the Project types tree.
- Select Win32 Console Application from the Templates.
- 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”.
- Click OK.
- In the Win32 Application Wizard click Next >.
- Select Console application under Application type.
- Select Empty project under Additional options.
- Click Finish.
- In the Solution Explorer right click the Source Files folder under your new unit test project and select Add -> New Item...
- In the Add New Item dialog select Visual C++ -> Code in the Categories tree and C++ File (.cpp) under Templates.
- Give the new file a Name (e.g. testrunner.cpp) and click OK.
- Copy the test runner source code (see below) into your new source file and save it.
- In the Solution Explorer right click on the new unit test project (e.g. “MyProjectTest”) and select Properties.
- 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
- Select Configuration Properties -> C/C++ -> Linker and add the follow path to the Additional Library Directories: %SRCROOT%\cppunit-1.12.1\lib
- Select Configuration Properties -> C/C++ -> Linker -> Input and add cppunitd.lib to the Additional Dependencies.
- Select Configuration Properties -> Build Events -> Post-Build Event and enter $(TargetPath) in the Command Line field and Unit testing… in the Description field.
- Click OK.
- In the Solution Explorer right click on the top-level solution and select Properties.
- In the Solution Property Pages dialog select Common Properties -> Project Dependencies.
- Select the unit test project from the Project list and select (check) the main project in the Depends on list.
- Click OK.
Test Runner Source Code
Adapted from: http://cppunit.sourceforge.net/doc/lastest/money_example.html
#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 = runner.run();
std::cout << "--------------- END CPPUNIT TEST ---------------" << << std::endl
// Return error code 1 if the one of test failed.
return wasSucessful ? 0 : 1;
}