Getting Started with UnitTest++ in Visual Studio

For some time I wanted to incorporate unit testing into our development process. Though many think unit testing is largely a waste of time with not all code bases lending themselves easily to testing, if done in the right spirit I think it (like version control and bug tracking) makes life easier. In a nutshell, it enables me to make changes to code I know works and confirm whether or not I have broken something in the process of refactoring or re-writing code. This can save hours of work.

1. Download the latest release from the sourceforge website. I unzipped and installed it to my own Visual Studio Projects folder.

2. Inside the project folder you will find Microsoft Visual Studio Solution Objects for both the 2003 and 2005 versions: open the solution appropriate for you and build it.  Notice that both projects (UnitTest++ and TestUnitTest++) are opened and that after building, the unit tests are automatically run for you, giving a simple and readable summary of the test results:

Generating Code…
Linking…
Performing Post-Build Event…
Success: 175 tests passed.
Test time: 0.15 seconds.

The output generated from this build includes the UnitTest++.vsnet2005.lib or UnitTest++.vsnet2003.lib libraries which will be needed in any subsequent unit test applications that you create.

3. Create a new Win32 console application as your main test application:

4. Go into your project properties and add the UnitTest++ header files to your includes:

5. You will also need to add the UnitTest++ library location to your project. This can be found in your orginal UnitTest++\Debug folder after your initial build (see section 2):

I copied the UnitTest++.vsnet2003.lib file itself into my project folder.

6. One more thing you can do in your project properties is to enable the automatic running of the unit tests during a build. In the post-build event property page add “$(TargetPath)” to the command line, along with a brief description:

7. Now you are ready to do some tests, starting with an extremely simple example:

// Test.cpp : Defines entry point for console application.
//
#include "stdafx.h"
#include "UnitTest++.h"

TEST( HelloUnitTestPP )
{
   CHECK( false );
}

int _tmain(int argc, _TCHAR* argv[])
{
      return UnitTest::RunAllTests();
}
`