CppUnit project page | FAQ | CppUnit home page |
#include <cppunit/Portability.h>
#include <cppunit/extensions/AutoRegisterSuite.h>
#include <cppunit/extensions/TestSuiteBuilder.h>
#include <string>
Include dependency graph for HelperMacros.h:
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... |
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()
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> );
|
Value: builder.addTestCaller( #testMethod, \ &__ThisTestCaseType::testMethod , \ (__ThisTestCaseType*)factory->makeTest() )
|
|
Value: private: \ typedef ASuperClass __ThisSuperClassType; \ CPPUNIT_TEST_SUITE( ATestCaseType ); \ __ThisSuperClassType::registerTests( suite, factory ) 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(); };
|
|
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 ); 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.
|
|
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 After this macro, member access is set to "private".
|
|
Value: static CppUnit::AutoRegisterSuite< ATestCaseType > \ __CPPUNIT_MAKE_UNIQUE_NAME(__autoRegisterSuite ) 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().
|
|
|
|
|
|
Decorates the specified string with the line number to obtain a unique name;.
|
|
|
|
hosts this site. |
Send comments to: CppUnit Developers |