Take the 2-minute tour ×

I'm having trouble figuring out how to get the testing framework set up and usable in Visual Studio 2008 for C++ presumably with the built-in unit testing suite.

Any links or tutorials would be appreciated.

share|improve this question
Google releases C++ Test Framework which is very similar with xUnit frameworks. http://code.google.com/p/googletest/ –  popopome Aug 20 '08 at 18:51
What framework did you end up using? –  Joakim Karlsson Nov 4 '09 at 8:30
actually I still haven't used any of them. I decided for what I was working on unit tests weren't the most effective use of time. –  DShook Nov 5 '09 at 19:01

12 Answers 12

up vote 45 down vote accepted

This page may help, it reviews quite a few C++ unit test frameworks:

  • CppUnit
  • Boost.Test
  • CppUnitLite
  • NanoCppUnit
  • Unit++
  • CxxTest

Check out CPPUnitLite or CPPUnitLite2.

CPPUnitLite was created by Michael Feathers, who originally ported Java's JUnit to C++ as CPPUnit (CPPUnit tries mimic the development model of JUnit - but C++ lacks Java's features [e.g. reflection] to make it easy to use).

CPPUnitLite attempts to make a true C++-style testing framework, not a Java one ported to C++. (I'm paraphrasing from Feather's Working Effectively with Legacy Code book). CPPUnitLite2 seems to be another rewrite, with more features and bug fixes.

I also just stumbled across UnitTest++ which includes stuff from CPPUnitLite2 and some other framework.

Microsoft has released WinUnit (via MSDN magazine). The download link for the code seems broken, but here is a link found in the comments here.

* Update: WinUnit Homepage *

share|improve this answer
winunit.codeplex.com –  inazaruk Oct 2 '09 at 6:53
Googletest is very similar to boost::test but a little easier to integrate into VS –  Martin Beckett Oct 6 '10 at 21:29

There is a way to test unmanaged C++ using the built in testing framework within Visual Studio 2008. If you create a C++ Test Project, using C++/CLI, you can then make calls to an unmanaged DLL. You will have to switch the Common Language Runtime support to /clr from /clr:safe if you want to test code that was written in unmanaged C++.

I have step by step details on my blog here: http://msujaws.wordpress.com/2009/05/06/unit-testing-mfc-with-mstest/

share|improve this answer
Great, I am glad this is possible, thanks :) –  DanDan Dec 2 '09 at 22:40
This does place certain restrictions on your C++ code, I've got at least one code base where trying to include its headers into C++/CLI code was more trouble than it was worth. The big benefit for me of using C++/CLI is you can use the funky parameterised testing with attributes like RowTest in mbUnit/NUnit/xUnit.Net –  Andy Dent Aug 24 '10 at 2:17

Here is the approach I use to test the IIS URL Rewrite module at Microsoft (it is command-line based, but should work for VS too):

  1. Make sure your header files are consumable by moving source code to cpp files and using forward declaration if needed.
  2. Compile your code to test as library (.lib)
  3. Create your UnitTest project as C++ with CLR support.
  4. Include your header files.
  5. Include your .lib files.
  6. Add a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll
  7. Use a really small class for declaring your unit test and jump from managed to C++/Native code like this (may have typos):

Here is an example:

// Example
#include "stdafx.h"
#include "mstest.h"

// Following code is native code.
#pragma unmanaged
void AddTwoNumbersTest() {
  // Arrange
  Adder yourNativeObject;
  int expected = 3;
  int actual;
  // Act
  actual = yourNativeObject.Add(1, 2);
  // Assert
  Assert::AreEqual(expected, actual, L"1 + 2 != 3");

// Following code is C++/CLI (Managed)
#pragma managed
using namespace Microsoft::VisualStudio::TestTools::UnitTesting;
public ref class TestShim {
  void AddTwoNumbersTest() {
     // Just jump to C++ native code (above)

With this approach, people don't have to learn too much C++/CLI stuff, all the real test will be done in C++ native and the TestShim class will be used to 'publish' the test to MSTest.exe (or make it visible).

For adding new tests you just declare a new [TestMethod] void NewTest(){::NewTest();} method and a new void NewTest() native function. No macros, no tricks, straighforward.

Now, the heade file is optionally, but it can be used to expose the Assert class' methods with C++ native signatures (e.g. wchar_t* instead of Stirng^), so it can you can keep it close to C++ and far from C++/CLI:

Here is an example:

// Example
#pragma once
#pragma managed(push, on)
using namespace System;
class Assert {
    static void AreEqual(int expected, int actual) {
        Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual);

    static void AreEqual(int expected, int actual, PCWSTR pszMessage) {
        Microsoft::VisualStudio::TestTools::UnitTesting::Assert::AreEqual(expected, actual, gcnew String(pszMe

    template<typename T>
    static void AreEqual(T expected, T actual) {

    // Etcetera, other overloads...
#pragma managed(pop)


share|improve this answer

Personally, I prefer WinUnit since it doesn't require me to write anything except for my tests (I build a .dll as the test, not an exe). I just build a project, and point WinUnit.exe to my test output directory and it runs everything it finds. You can download the WinUnit project here. (MSDN now requires you to download the entire issue, not the article. WinUnit is included within.)

share|improve this answer
Note that WinUnit has since moved to CodePlex winunit.codeplex.com –  Andy Dent May 28 '10 at 2:21
Awesome. Thanks for the info. –  moswald Sep 14 '10 at 13:48

The framework included with VS9 is .NET, but you can write tests in C++/CLI, so as long as you're comfortable learning some .NET isms, you should be able to test most any C++ code.

boost.test and googletest look to be fairly similar, but adapted for slightly different uses. Both of these have a binary component, so you'll need an extra project in your solution to compile and run the tests.

The framework we use is CxxTest, which is much lighter; it's headers only, and uses a Perl (!) script to scrape test suite information from your headers (suites inherit from CxxTest::Base, all your test methods' names start with "test"). Obviously, this requires that you get Perl from one source or another, which adds overhead to your build environment setup.

share|improve this answer
CxxTest can use python instead of perl –  Martin Beckett Jun 8 '09 at 13:38
You also need to be careful about any DLLs required by your code - I've had problems with NUnit and mbUnit testing C++/CLI code, trying to get DLLs loaded. –  Andy Dent Jul 26 '10 at 4:48

The tools that have been mentioned here are all command line tools. If you look for a more integrated solution, have a look at cfix studio, which is a Visual Studio AddIn for C/C++ unit testing . It is quite similar to TestDriven.Net, but for (unmanaged) C/C++ rather than .Net.

share|improve this answer
Now called VisualAssert, but the link still works. –  newenglander May 30 '12 at 10:54

I use UnitTest++.

While the example tutorial is for Visual Studio 2005, the concepts are similar (try setting one up for VC6...).

Update: The VC6 hacks are now included in the source!

share|improve this answer

I've used CppUnit with VS2005 and Eclipse. The wiki is very thorough (especially if you are familiar with JUnit).

share|improve this answer

I'm not 100% sure about VS2008, but I know that the Unit Testing framework that microsoft shipped in VS2005 as part of their Team Suite was only for .NET, not C++

I've used CppUnit also and it was alright. Much the same as NUnit/JUnit/so on.

If you've used boost, they also have a unit testing library

The guys behind boost have some serious coding chops, so I'd say their framework should be pretty good, but it might not be the most user friendly :-)

share|improve this answer

The unit tester for VS2008 is only for .NET code as far as I know.

I used CppUnit on Vs2005 and found it to be pretty good.

As far as I remember, the setup was relatively painless, just make sure that in your testing projects the linker (Linker->Input->Additional Dependencies) includes cppunitd.lib.

Then, #include <cppunit/extensions/HelperMacros.h> in your header

You can then follow the steps in http://cppunit.sourceforge.net/doc/1.11.6/cppunit_cookbook.html to get your test class working.

share|improve this answer

I like the CxxTest as well for the same reasons. It's a header file only so no linking required. You aren't stuck with Perl as there is a Python runner as well. I will be reviewing the google library soon. The Boost stuff pulls in too much other baggage.

share|improve this answer

I was suffering to implement unit test for unmanaged C++ application in windows environment with Visual Studio. So I manage to overcome and wrote a post as a step by step guidance to unmanaged C++ Application Unit Test. Hope It may help you.


share|improve this answer

Your Answer


