CppUnit project page FAQ CppUnit home page

Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

HelperMacros.h File Reference

Macros intended to ease the definition of test suites. More...

#include <cppunit/Portability.h>
#include <cppunit/extensions/AutoRegisterSuite.h>
#include <cppunit/extensions/TestSuiteBuilder.h>
#include <string>

Include dependency graph for HelperMacros.h:

Include dependency graph

Go to the source code of this file.

Defines

#define __CPPUNIT_SUITE_CTOR_ARGS(ATestCaseType)   (std::string(#ATestCaseType))
#define CPPUNIT_TEST_SUITE(ATestCaseType)
 Begin test suite. More...

#define CPPUNIT_TEST_SUB_SUITE(ATestCaseType, ASuperClass)
 Begin test suite (includes parent suite). More...

#define CPPUNIT_TEST(testMethod)
 Add a method to the suite. More...

#define CPPUNIT_TEST_SUITE_END()
 End declaration of the test suite. More...

#define __CPPUNIT_CONCATENATE_DIRECT(s1, s2)   s1##s2
#define __CPPUNIT_CONCATENATE(s1, s2)   __CPPUNIT_CONCATENATE_DIRECT( s1, s2 )
#define __CPPUNIT_MAKE_UNIQUE_NAME(str)   __CPPUNIT_CONCATENATE( str, __LINE__ )
 Decorates the specified string with the line number to obtain a unique name;. More...

#define CPPUNIT_TEST_SUITE_REGISTRATION(ATestCaseType)
 Register test suite into global registry. More...


Detailed Description

Macros intended to ease the definition of test suites.

The macros CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(), and CPPUNIT_TEST_SUITE_END() are designed to facilitate easy creation of a test suite. For example,

 #include <cppunit/extensions/HelperMacros.h>
 class MyTest : public CppUnit::TestCase {
   CPPUNIT_TEST_SUITE( MyTest );
   CPPUNIT_TEST( testEquality );
   CPPUNIT_TEST( testSetName );
   CPPUNIT_TEST_SUITE_END();
 public:
   void testEquality();
   void testSetName();
 };

The effect of these macros is to define two methods in the class MyTest. The first method is an auxiliary function named registerTests that you will not need to call directly. The second function

static CppUnit::TestSuite *suite()
returns a pointer to the suite of tests defined by the CPPUNIT_TEST() macros.

Rather than invoking suite() directly, the macro CPPUNIT_TEST_SUITE_REGISTRATION() is used to create a static variable that automatically registers its test suite in a global registry. The registry yields a Test instance containing all the registered suites.

 CPPUNIT_TEST_SUITE_REGISTRATION( MyTest );
 CppUnit::Test* tp =
   CppUnit::TestFactoryRegistry::getRegistry().makeTest();

The test suite macros can even be used with templated test classes. For example:

 template<typename CharType>
 class StringTest : public CppUnit::Testcase {
   CPPUNIT_TEST_SUITE( StringTest );
   CPPUNIT_TEST( testAppend );
   CPPUNIT_TEST_SUITE_END();
 public:  
   ...
 };

You need to add in an implementation file:

 CPPUNIT_TEST_SUITE_REGISTRATION( String<char> );
 CPPUNIT_TEST_SUITE_REGISTRATION( String<wchar_t> );


Define Documentation

#define CPPUNIT_TEST testMethod  
 

Value:

builder.addTestCaller( #testMethod,                               \
                             &__ThisTestCaseType::testMethod ,          \
                             (__ThisTestCaseType*)factory->makeTest() )
Add a method to the suite.

Parameters:
testMethod   Name of the method of the test case to add to the suite. The signature of the method must be of type: void testMethod();
See also:
CPPUNIT_TEST_SUITE.

#define CPPUNIT_TEST_SUB_SUITE ATestCaseType,
ASuperClass  
 

Value:

private:                                                    \
    typedef ASuperClass __ThisSuperClassType;                 \
    CPPUNIT_TEST_SUITE( ATestCaseType );                      \
      __ThisSuperClassType::registerTests( suite, factory )
Begin test suite (includes parent suite).

This macro may only be used in a class whose parent class defines a test suite using CPPUNIT_TEST_SUITE() or CPPUNIT_TEST_SUB_SUITE().

This macro begins the declaration of a test suite, in the same manner as CPPUNIT_TEST_SUITE(). In addition, the test suite of the parent is automatically inserted in the test suite being defined.

Here is an example:

 #include <cppunit/extensions/HelperMacros.h>
 class MySubTest : public MyTest {
   CPPUNIT_TEST_SUB_SUITE( MySubTest, MyTest );
   CPPUNIT_TEST( testAdd );
   CPPUNIT_TEST( testSub );
   CPPUNIT_TEST_SUITE_END();
 public:
   void testAdd();
   void testSub();
 };
Parameters:
ATestCaseType   Type of the test case class.
ASuperClass   Type of the parent class.
See also:
CPPUNIT_TEST_SUITE.

#define CPPUNIT_TEST_SUITE ATestCaseType  
 

Value:

private:                                                                \
    typedef ATestCaseType __ThisTestCaseType;                             \
    class ThisTestCaseFactory : public CppUnit::TestFactory               \
    {                                                                     \
      virtual CppUnit::Test *makeTest()                                   \
      {                                                                   \
        return new ATestCaseType();                                       \
      }                                                                   \
    };                                                                    \
  public:                                                                 \
    static void                                                           \
    registerTests( CppUnit::TestSuite *suite,                             \
                   CppUnit::TestFactory *factory )                        \
    {                                                                     \
      CppUnit::TestSuiteBuilder<__ThisTestCaseType> builder( suite );
Begin test suite.

This macro starts the declaration of a new test suite. Use CPPUNIT_TEST_SUB_SUITE() instead, if you wish to include the test suite of the parent class.

Parameters:
ATestCaseType   Type of the test case class.
See also:
CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END, CPPUNIT_TEST_SUITE_REGISTRATION.

#define CPPUNIT_TEST_SUITE_END  
 

Value:

builder.takeSuite();                                              \
    }                                                                   \
    static CppUnit::TestSuite *suite()                                  \
    {                                                                   \
      CppUnit::TestSuiteBuilder<__ThisTestCaseType>                     \
          builder __CPPUNIT_SUITE_CTOR_ARGS( ATestCaseType );           \
      ThisTestCaseFactory factory;                                      \
      __ThisTestCaseType::registerTests( builder.suite(), &factory );   \
      return builder.takeSuite();                                       \
    }                                                                   \
  private:    \
    typedef ThisTestCaseFactory __ThisTestCaseFactory
End declaration of the test suite.

After this macro, member access is set to "private".

See also:
CPPUNIT_TEST_SUITE. , CPPUNIT_TEST_SUITE_REGISTRATION.

#define CPPUNIT_TEST_SUITE_REGISTRATION ATestCaseType  
 

Value:

Register test suite into global registry.

This macro declares a static variable whose construction causes a test suite factory to be inserted in a global registry of such factories. The registry is available by calling the static function CppUnit::TestFactoryRegistry::getRegistry().

Parameters:
ATestCaseType   Type of the test case class.
Warning:
This macro should be used only once per line of code (the line number is used to name a hidden static variable).
See also:
CPPUNIT_TEST_SUITE, CppUnit::AutoRegisterSuite.

#define __CPPUNIT_CONCATENATE s1,
s2      __CPPUNIT_CONCATENATE_DIRECT( s1, s2 )
 

#define __CPPUNIT_CONCATENATE_DIRECT s1,
s2      s1##s2
 

#define __CPPUNIT_MAKE_UNIQUE_NAME str      __CPPUNIT_CONCATENATE( str, __LINE__ )
 

Decorates the specified string with the line number to obtain a unique name;.

Parameters:
str   String to decorate.

#define __CPPUNIT_SUITE_CTOR_ARGS ATestCaseType      (std::string(#ATestCaseType))
 


SourceForge Logo hosts this site. Send comments to:
CppUnit Developers