Visual Assert – The Unit Testing Add-In for Visual C++
cfix – C/C++ Unit Testing for Win32 and NT
 
 

Getting Started

Getting Started

The first step to creating cfix tests is to decide on where to put them. You can add cfix tests to any Visual C++ project, so you have the option of either creating a dedicated "test project" or adding the tests to an existing project. Which of these two options is better depends on your specific requirements.

For the sake of a cleaner project structure, creating a separate Visual Studio project for unit tests may be the preferred way to go. In this case, use the familiar Win32 Project template to add a new project to your solution.

Figure 2.1. Creating a new project (1/2)

Creating a new project (1/2)


As Application Type, choose DLL. DLL projects offer slightly more flexibility w.r.t. cfix unit testing and free you from having to maintain a main() function.

Figure 2.2. Creating a new project (2/2)

Creating a new project (2/2)


For some projects, however, maintaining unit tests in a separate Visual Studio project may not be practical: In particular when you would like to test classes and functions that are part of an EXE project and are not exported, such separation may not be feasible. In such cases, it is therefore advisable to make the unit tests part of the same Visual Studio project.

Of course, this is no strict either-or decision -- in case your Visual Studio solution spans multiple projects, any of these projects may contain unit tests.

Once you have identified the project to implement a unit test in, writing a test comprises the following steps:

  • include cfixcc.h
  • Create a fixture by writing a class that publicly derives from cfixcc::TestFixture
  • Add one or more methods to this class that implement the actual tests
  • Optionally, implement before/after or setup/teardown methods
  • Make the class known to cfix by writing a fixture definition

(The steps slightly differ in case you intend to use the C or WinUnit API but the overall process is the same.)

Rather than performing these steps manually, you can use the Add Unit Test wizard: In the Solution Explorer, right-click on a project or folder node, and choose Add | Unit Test...:

Figure 2.3. Launching the Add Unit Test Wizard

Launching the Add Unit Test Wizard


In the wizard Window, enter a fixture name and click Finish:

Figure 2.4. The Add Unit Test Wizard

The Add Unit Test Wizard


Whether you typed it by hand or used the wizard, the result will look more or less the same. The following listing shows the scaffold of an example unit test. cfix does not require the class to be declared in a separate header file first, so the entire code can be placed in a single C++ source file (*.cpp).

#include <cfixcc.h>

class ExampleTest : public cfixcc::TestFixture
{
public:
  void TestOne() 
  {}
  
  void TestTwo() 
  {}
};

CFIXCC_BEGIN_CLASS( ExampleTest )
  CFIXCC_METHOD( TestOne )
  CFIXCC_METHOD( TestTwo )
CFIXCC_END_CLASS()
			

ExampleTest derives from cfixcc::TestFixture, which makes it a fixture. TestOne and TestTwo are two arbitrarily named methods that implement tests. To tell cfix that these methhods indeed implement a test cases, we have to add the CFIXCC_BEGIN_CLASS/ CFIXCC_METHOD/CFIXCC_END_CLASS construct at the end of the file.

Note that this is in fact all there is to do to write tests -- there is no need to write any additional registration code, implement a main routine or anything else. Although the test does not do anything meaningful yet, we can already compile and run it.

Once you have built the solution, you will notice that the Explorer Window has updated its contents:

Figure 2.5. Explorer Window showing the fixtures and tests of the current project

Explorer Window showing the fixtures and tests of the current project


It shows the current solution and project, the primary output module, the fixture we have just created, and, as leaf nodes, our two test methods.

Now right-click 'ExampleTest' and select Run Test Without Debugging from the context menu. Shortly thereafter, the Run Window will pop up and show the status of the run -- which, unsuprisingly, succeeded:

Figure 2.6. Run window showing status and results of a test run

Run window showing status and results of a test run


Now that we have a basic test project in place and know how to run tests, we can do some more interesting things...