CppUnit project page FAQ CppUnit home page

Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

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>

Go to the source code of this file.

Namespaces

namespace  CppUnit

Defines

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

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

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

#define CPPUNIT_TEST_EXCEPTION(testMethod, ExceptionType)
 Add a test which fail if the specified exception is not caught. More...

#define CPPUNIT_TEST_FAIL(testMethod)   CPPUNIT_TEST_EXCEPTION( testMethod, CppUnit::Exception )
 Adds a test case which is excepted to fail. 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(ATestFixtureType)
 Adds the specified fixture suite to the unnamed registry. More...

#define CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(ATestFixtureType, suiteName)
 Adds the specified fixture suite to the specified registry suite. 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::TestFixture {
   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::TestFixture {
   CPPUNIT_TEST_SUITE( StringTest );
   CPPUNIT_TEST( testAppend );
   CPPUNIT_TEST_SUITE_END();
 public:  
   ...
 };

You need to add in an implementation file:

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


Define Documentation

#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 ATestFixtureType       (std::string(#ATestFixtureType))
 


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