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

Fixture definition (C++ only)

Fixture definition (C++ only)

Test suites written in C++ can use the API and fixture definition common to both C and C++.

As of cfix 1.2, however, C++ test suites can also be written in an object-oriented fashion, using a different style of fixture definition covered in this section.

Rather than providing a set of global functions, a C++ fixture is written as a class. This class has to derive from TestFixture and may contain any number of test methods. If required, before and after methods can be provided by overriding the resprctive methods inherited from the base class. Setup and teardown routines can be implemented using static methods.

The following code listing shows an example of a test fixture written using C++:

#include <cfixcc.h>

class SimpleFixture : public cfixcc::TestFixture
{
public:
  virtual void Before()
  {
    ...
  }

  virtual void After()
  {
    ...
  }
  
  void Method01() 
  {
    ...
  }

  void Method02() 
  {
    ...
  }
};

CFIXCC_BEGIN_CLASS( SimpleFixture )
  CFIXCC_METHOD( Method01 )
  CFIXCC_METHOD( Method02 )
CFIXCC_END_CLASS()
		

Note that rather than CFIX_BEGIN_FIXTURE and CFIX_END_FIXTURE, CFIXCC_BEGIN_CLASS (using the name of the class as argument) and CFIXCC_END_CLASS have been used.

Using CFIXCC_METHOD, the two test methods have been added to the fixture. Note that the before and after methods did not need to be specified.

[Note]Note
Between CFIXCC_BEGIN_CLASS and CFIXCC_END_CLASS, only use the CFIXCC_xxx macros used for C++ fixtures to add methods. Never use CFIX_FIXTURE_SETUP, CFIX_FIXTURE_TEARDOWN, CFIX_FIXTURE_BEFORE, or CFIX_FIXTURE_AFTER within a C++ fixture definition.