CPPUnit in VS2010–with a sample

I modified the CPPUnit 1.12.1 environment so it works in Visual Studio 2010.  I also included a small project that uses CPPUnit as part of the build of the small project.  Until I find a permanent home for the files, email me at erict [ at ] powersoft [ dot ] ca , and I will send to you.

The sample below doesn’t begin to explore the power that is available in CPPUnit.  The examples provided in CPPUnit itself need to be studied.  But this is a start…

Building CPPUnit

To recompile all the CPPUnit stuff, open examples.sln, then click on Build |  Batch Build…, do select all, then unselect all the Template configurations (I don’t know what the Template configurations do).  You will get some errors in the build, but these are example builds included which are supposed to fail.

A sample project of my own using CPPUnit

The project I needed was to test a class, which is defined in a header file.  I named the project TestSomeClassesInHeaders.

Below I will describe all the steps I had to do to create the small project.

  1. Create new project in VS2010: Visual C++ | Win32; Console application; add Project Name ‘TestSomeClassesInHeaders’; click Finish to complete the application wizard
  2. Go into project properties;
    1. C/C++; General and modify the ‘Additional Include Directories’ field by adding the path to the folder cppunit-1.12.1\include
    2. Linker; General and modify the ‘Additional Library Directories’ field by adding the path to the folder cppunit-1.12.1\lib
    3. Linker; Input and modify the ‘Additional Dependencies’ field by adding the name cppunitd.lib for your debug configuration, and cppunit.lib for the release configuration.
    4. Build Events | Post-Build Event, add the line $(TargetDir)$(TargetFileName) so that the tests execute as part of the build.
  3. Modify the file TestSomeClassesInHeaders.cpp by adding the following headers:

       1:  #include <cppunit/CompilerOutputter.h>
       2:  #include <cppunit/extensions/TestFactoryRegistry.h>
       3:  #include <cppunit/ui/text/TestRunner.h>

  4. Replace the contents of _tmain as follows:

       1:  // get the top-level suite of tests (registry is not the Win32 registry)
       2:  CppUnit::Test * suite = CppUnit::TestFactoryRegistry::getRegistry().makeTest();
       3:  // add the test to the list of tests to run
       4:  CppUnit::TextUi::TestRunner runner;
       5:  runner.addTest(suite);
       6:  // change the default outputter to a compiler error format outputter
       7:  runner.setOutputter(new CppUnit::CompilerOutputter(&runner.result(), std::cerr));
       8:  // run the tests
       9:  bool wasSuccessful = runner.run();
      10:  // return error code 1 if one or more tests failed
      11:  return wasSuccessful ? 0 : 1;

  5. If you now build, you will have a working framework, without any tests. But part of the build is the execution of the runner. 
    Next we add a dummy class which needs to be tested:
  6. Add a C++ class using the class wizard (Add Class). Call it NewClass, and make it inline.  In the generated header file add a public method, as follows:

       1:  int Feature(int x)
       2:  {
       3:      return x;
       4:  }

  7. Next we add a test object.  Add a C++ class using the class wizard (Add Class).  Call it NewClassTesting.  Don’t make it inline.
  8. Just above the constructor in the CPP File, add the following to identify it as a test suite:


  9. The header file of the testing class needs to be modified quite a bit.  Here is the complete file, with comments:

       1:  #pragma once
       2:  #include <cppUnit/TestCase.h>
       3:  #include <cppUnit/extensions/HelperMacros.h>
       5:  #include "NewClass.h"   // the class we want to test
       7:  class NewClassTesting :
       8:          public CppUnit::TestFixture
       9:  {
      10:      CPPUNIT_TEST_SUITE(NewClassTesting);
      11:      CPPUNIT_TEST(test1);
      12:      // ... name each method that should be executed
      13:      CPPUNIT_TEST_SUITE_END();
      15:  public:
      16:      NewClassTesting(void);
      17:      ~NewClassTesting(void);
      19:      void test1()
      20:      {
      21:          NewClass nc;
      22:          int x = nc.Feature(5);
      23:          CPPUNIT_ASSERT(x == 5);
      24:      }
      25:  };

  10. Again build.  You now have a framework and unit tested your NewClass!
  11. Just for the fun of it, change line 23 of the testing class to CPPUNIT_ASSERT(x != 5);  Upon building you will see build errors with a description ‘error : Assertion’.  Double clicking here will show you which assert failed.  You can single step through the project to find out details.


Here's cppunit-1.12.1-VS2010.zip

Print | posted on Tuesday, February 21, 2012 1:19 PM


# re: CPPUnit in VS2010–with a sample

left by Adam Dreaver at 3/18/2012 8:02 PM Gravatar
Nice project here. I'm having some difficulty recreating the sample. Every time I build it, the sample doesn't output anything to the output window, except the first time it's built!

If I download your example TestSomeClassesInHeaders, I can repeatedly hit the build button and keep seeing the test results.

I follow your example to a T, new project, console. Copy in the code, fix up compiler/linker to find the cppunit hearders/lib. I fix post build options to $(TargetDir)$(TargetFileName).

I have gone through your projects settings and the settings of the project I created from scratch, and I can't seem to find any differences in any of the settings, and I've gone through them one by one.

# re: CPPUnit in VS2010–with a sample

left by ME at 8/9/2013 4:24 PM Gravatar
Good tutorial

# re: CPPUnit in VS2010–with a sample

left by sangeetha at 4/14/2014 10:23 PM Gravatar
I am not able to find the "cppunit.lib" file anywhere ... please help in finding the file...

# re: CPPUnit in VS2010–with a sample

left by Lucien at 5/27/2014 1:21 AM Gravatar
Nice article, thanks for sharing, I'm searching this for a long time.

@ sangeetha - don't know if you find the answer, kindly check the INSTALL-WIN32.txt in cppunit folder for generating cppunit.lib

# re: CPPUnit in VS2010–with a sample

left by Thibaud at 6/8/2017 7:47 AM Gravatar
Hi, i'm trying to compile the CPPUnit library, but i can't find the "examples.sln" project you're talking about.. Any ideas ?

# Alldaychemist Prescription Needed KelendorO

left by KelendorO at 1/15/2019 9:36 PM Gravatar
Purchase Prednisone W Not Prescription buy generic cialis Fiabilidad Propecia Medication Cephalexin

# dxsbyo

left by gjyvtg at 3/17/2019 8:59 AM Gravatar
viagra online
canadian viagra

# mksyuq

left by drnesd at 3/29/2019 5:28 AM Gravatar
levitra orosolubile meno effetti collaterali
buy real levitra
Email (never displayed)
Please add 4 and 6 and type the answer here: