The website of Lei Chen

Sunshine and rain of a developer

Intergrade CPPUNIT in Visual Studio 2008

Posted by hide1713 on June 15, 2009

This tutorial helps you configure the CPPUNIT  unit test environment in Visual Studio 2008. If you have any questions, please look at the trouble shooting section at the end of tutorial.

Firstly, There’s a bug in CPPUNIT. If you compile cppunit-1.12.1 under VS2008.You will either fail to compile the code or generate cppunit library without correct debug information.

Let’s fix the bug first. According to this post

I just tried to compile CPP Unit Under Visual C++ 2008. Compiler. There was one problem When I tried to compile MFC Test Runner.

MsDevCallerListCtrl.cpp

.\MsDevCallerListCtrl.cpp(67) : error C3505: cannot load type library ‘{80CC9F66-E7D8-4DDD-85B6-D9E6CD0E93E2}’

.\MsDevCallerListCtrl.cpp(67) : fatal error C1104: fatal error importing libid: ‘80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2′

CPP Unit using IDE Automation component to track the source lines in the MFCTest runner. Version cppunit-1.12.0 currently support till version 7 of IDE Automation. I’ve only version 2008 of Visual Studio installed in my machine. So just correct the version number in MsDevCallerListCtrl.cpp file

#pragma warning( disable : 4278 )

#pragma warning( disable : 4146 )

#import “libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2″ version(”9.0″) lcid(”0″) raw_interfaces_only named_guids

#pragma warning( default : 4146 )

#pragma warning( default : 4278 )

When you build the source code. You should see some assertion errors. That’s fine. They are not real errors, just some unit test code. Don’t worry about it. You should look into ./lib directory. There should be many files including cppunitd.dll, cppunit.dll,cppunitd_dll.pdb.

Now, you have code compiled. It won’t be difficult to setup right environemnt. Follow the money tutorial

In the following document, $CPPUNIT is the directory where you unpacked CppUnit: $CPPUNIT/: include/ lib/ src/ cppunit/First, you need to compile CppUnit libraries:

  • Open the $CPPUNIT/src/CppUnitLibraries.dsw workspace in VC++.
  • In the ‘Build’ menu, select ‘Batch Build…’
  • In the batch build dialog, select all projects and press the build button.
  • The resulting libraries can be found in the $CPPUNIT/lib/ directory.

Once it is done, you need to tell VC++ where are the includes and librairies to use them in other projects. Open the ‘Tools/Options…’ dialog, and in the ‘Directories’ tab, select ‘include files’ in the combo. Add a new entry that points to $CPPUNIT/include/. Change to ‘libraries files’ in the combo and add a new entry for $CPPUNIT/lib/. Repeat the process with ‘source files’ and add $CPPUNIT/src/cppunit/.

Getting started

Creates a new console application (‘a simple application’ template will do). Let’s link CppUnit library to our project. In the project settings:

  • In tab ‘C++’, combo ‘Code generation’, set the combo to ‘Multithreaded DLL’ for the release configuration, and ‘Debug Multithreaded DLL’ for the debug configure,
  • In tab ‘C++’, combo ‘C++ langage’, for All Configurations, check ‘enable Run-Time Type Information (RTTI)’,
  • In tab ‘Link’, in the ‘Object/library modules’ field, add cppunitd.lib for the debug configuration, and cppunit.lib for the release configuration.

We’re done !

We are not done yet. There are three more things you should know.

1. By default, the cppunit is liked to Dll with /MDd option. Make sure other projects are using the same option. You will get many weird errors if you link other parts with static library /MTd

If you compile your code with MTd, you should link your code with cppunitd_dll.lib. otherwise, you should link with cppunitd.dll.

2. There are many ways to manage your unit test. You can setup  a subproject in a solution which generates unit test binary.  I spent few hours to figure out that I need to add cpp files to the unit test project so that  linker could find those functions.  If you see “Symbol unsolved” errors,  this is the right direction to look into.

3. Look at your project path. The default project path contain space. So Visual Studio postcompile command can not correctly execute if you follow the “Money” tutorial.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.