1 // //////////////////////////////////////////////////////////////////////////
2 // Header file HelperMacros.h
3 // (c)Copyright 2000, Baptiste Lepilleur.
5 // //////////////////////////////////////////////////////////////////////////
6 #ifndef CPPUNIT_EXTENSIONS_HELPERMACROS_H
7 #define CPPUNIT_EXTENSIONS_HELPERMACROS_H
9 #include <cppunit/TestCaller.h>
10 #include <cppunit/TestSuite.h>
11 #include <cppunit/extensions/AutoRegisterSuite.h>
12 #include <cppunit/extensions/ExceptionTestCaseDecorator.h>
13 #include <cppunit/extensions/TestFixtureFactory.h>
14 #include <cppunit/extensions/TestNamer.h>
15 #include <cppunit/extensions/TestSuiteBuilderContext.h>
19 /*! \addtogroup WritingTestFixture Writing test fixture
26 * Macros intended to ease the definition of test suites.
29 * CPPUNIT_TEST_SUITE(), CPPUNIT_TEST(), and CPPUNIT_TEST_SUITE_END()
30 * are designed to facilitate easy creation of a test suite.
34 * #include <cppunit/extensions/HelperMacros.h>
35 * class MyTest : public CppUnit::TestFixture {
36 * CPPUNIT_TEST_SUITE( MyTest );
37 * CPPUNIT_TEST( testEquality );
38 * CPPUNIT_TEST( testSetName );
39 * CPPUNIT_TEST_SUITE_END();
41 * void testEquality();
46 * The effect of these macros is to define two methods in the
47 * class MyTest. The first method is an auxiliary function
48 * named registerTests that you will not need to call directly.
50 * \code static CppUnit::TestSuite *suite()\endcode
51 * returns a pointer to the suite of tests defined by the CPPUNIT_TEST()
54 * Rather than invoking suite() directly,
55 * the macro CPPUNIT_TEST_SUITE_REGISTRATION() is
56 * used to create a static variable that automatically
57 * registers its test suite in a global registry.
58 * The registry yields a Test instance containing all the
61 * CPPUNIT_TEST_SUITE_REGISTRATION( MyTest );
63 * CppUnit::TestFactoryRegistry::getRegistry().makeTest();
66 * The test suite macros can even be used with templated test classes.
70 * template<typename CharType>
71 * class StringTest : public CppUnit::TestFixture {
72 * CPPUNIT_TEST_SUITE( StringTest );
73 * CPPUNIT_TEST( testAppend );
74 * CPPUNIT_TEST_SUITE_END();
80 * You need to add in an implementation file:
83 * CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<char> );
84 * CPPUNIT_TEST_SUITE_REGISTRATION( StringTest<wchar_t> );
89 /*! \brief Begin test suite
91 * This macro starts the declaration of a new test suite.
92 * Use CPPUNIT_TEST_SUB_SUITE() instead, if you wish to include the
93 * test suite of the parent class.
95 * \param ATestFixtureType Type of the test case class. This type \b MUST
96 * be derived from TestFixture.
97 * \see CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END,
98 * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL.
100 #define CPPUNIT_TEST_SUITE( ATestFixtureType ) \
102 typedef ATestFixtureType TestFixtureType; \
105 static const CPPUNIT_NS::TestNamer &getTestNamer__() \
107 static CPPUNIT_TESTNAMER_DECL( testNamer, ATestFixtureType ); \
112 typedef CPPUNIT_NS::TestSuiteBuilderContext<TestFixtureType> \
113 TestSuiteBuilderContextType; \
116 addTestsToSuite( CPPUNIT_NS::TestSuiteBuilderContextBase &baseContext ) \
118 TestSuiteBuilderContextType context( baseContext )
121 /*! \brief Begin test suite (includes parent suite)
123 * This macro may only be used in a class whose parent class
124 * defines a test suite using CPPUNIT_TEST_SUITE() or CPPUNIT_TEST_SUB_SUITE().
126 * This macro begins the declaration of a test suite, in the same
127 * manner as CPPUNIT_TEST_SUITE(). In addition, the test suite of the
128 * parent is automatically inserted in the test suite being
131 * Here is an example:
134 * #include <cppunit/extensions/HelperMacros.h>
135 * class MySubTest : public MyTest {
136 * CPPUNIT_TEST_SUB_SUITE( MySubTest, MyTest );
137 * CPPUNIT_TEST( testAdd );
138 * CPPUNIT_TEST( testSub );
139 * CPPUNIT_TEST_SUITE_END();
146 * \param ATestFixtureType Type of the test case class. This type \b MUST
147 * be derived from TestFixture.
148 * \param ASuperClass Type of the parent class.
149 * \see CPPUNIT_TEST_SUITE.
151 #define CPPUNIT_TEST_SUB_SUITE( ATestFixtureType, ASuperClass ) \
153 typedef ASuperClass ParentTestFixtureType; \
155 CPPUNIT_TEST_SUITE( ATestFixtureType ); \
156 ParentTestFixtureType::addTestsToSuite( baseContext )
159 /*! \brief End declaration of the test suite.
161 * After this macro, member access is set to "private".
163 * \see CPPUNIT_TEST_SUITE.
164 * \see CPPUNIT_TEST_SUITE_REGISTRATION.
166 #define CPPUNIT_TEST_SUITE_END() \
169 static CPPUNIT_NS::TestSuite *suite() \
171 const CPPUNIT_NS::TestNamer &namer = getTestNamer__(); \
172 std::auto_ptr<CPPUNIT_NS::TestSuite> suite( \
173 new CPPUNIT_NS::TestSuite( namer.getFixtureName() )); \
174 CPPUNIT_NS::ConcretTestFixtureFactory<TestFixtureType> factory; \
175 CPPUNIT_NS::TestSuiteBuilderContextBase context( *suite.get(), \
178 TestFixtureType::addTestsToSuite( context ); \
179 return suite.release(); \
181 private: /* dummy typedef so that the macro can still end with ';'*/ \
182 typedef int CppUnitDummyTypedefForSemiColonEnding__
184 /*! \brief End declaration of an abstract test suite.
186 * Use this macro to indicate that the %TestFixture is abstract. No
187 * static suite() method will be declared.
189 * After this macro, member access is set to "private".
191 * Here is an example of usage:
193 * The abstract test fixture:
195 * #include <cppunit/extensions/HelperMacros.h>
196 * class AbstractDocument;
197 * class AbstractDocumentTest : public CppUnit::TestFixture {
198 * CPPUNIT_TEST_SUITE( AbstractDocumentTest );
199 * CPPUNIT_TEST( testInsertText );
200 * CPPUNIT_TEST_SUITE_END_ABSTRACT();
202 * void testInsertText();
206 * m_document = makeDocument();
214 * virtual AbstractDocument *makeDocument() =0;
216 * AbstractDocument *m_document;
219 * The concret test fixture:
221 * class RichTextDocumentTest : public AbstractDocumentTest {
222 * CPPUNIT_TEST_SUB_SUITE( RichTextDocumentTest, AbstractDocumentTest );
223 * CPPUNIT_TEST( testInsertFormatedText );
224 * CPPUNIT_TEST_SUITE_END();
226 * void testInsertFormatedText();
228 * AbstractDocument *makeDocument()
230 * return new RichTextDocument();
234 * \see CPPUNIT_TEST_SUB_SUITE.
235 * \see CPPUNIT_TEST_SUITE_REGISTRATION.
237 #define CPPUNIT_TEST_SUITE_END_ABSTRACT() \
239 private: /* dummy typedef so that the macro can still end with ';'*/ \
240 typedef int CppUnitDummyTypedefForSemiColonEnding__
243 /*! \brief Add a test to the suite (for custom test macro).
245 * The specified test will be added to the test suite being declared. This macro
246 * is intended for \e advanced usage, to extend %CppUnit by creating new macro such
247 * as CPPUNIT_TEST_EXCEPTION()...
249 * Between macro CPPUNIT_TEST_SUITE() and CPPUNIT_TEST_SUITE_END(), you can assume
250 * that the following variables can be used:
252 * typedef TestSuiteBuilder<TestFixtureType> TestSuiteBuilderType;
253 * TestSuiteBuilderType &context;
256 * \c context can be used to name test case, create new test fixture instance,
257 * or add test case to the test fixture suite.
259 * Below is an example that show how to use this macro to create new macro to add
260 * test to the fixture suite. The macro below show how you would add a new type
261 * of test case which fails if the execution last more than a given time limit.
262 * It relies on an imaginary TimeOutTestCaller class which has an interface similar
266 * #define CPPUNITEX_TEST_TIMELIMIT( testMethod, timeLimit ) \
267 * CPPUNIT_TEST_SUITE_ADD_TEST( (new TimeOutTestCaller<TestFixtureType>( \
268 * namer.getTestNameFor( #testMethod ), \
269 * &TestFixtureType::testMethod, \
270 * factory.makeFixture(), \
273 * class PerformanceTest : CppUnit::TestFixture
276 * CPPUNIT_TEST_SUITE( PerformanceTest );
277 * CPPUNITEX_TEST_TIMELIMIT( testSortReverseOrder, 5.0 );
278 * CPPUNIT_TEST_SUITE_END();
280 * void testSortReverseOrder();
284 * \param test Test to add to the suite. Must be a subclass of Test. The test name
285 * should have been obtained using TestNamer::getTestNameFor().
287 #define CPPUNIT_TEST_SUITE_ADD_TEST( test ) \
288 context.addTest( test )
290 /*! \brief Add a method to the suite.
291 * \param testMethod Name of the method of the test case to add to the
292 * suite. The signature of the method must be of
293 * type: void testMethod();
294 * \see CPPUNIT_TEST_SUITE.
296 #define CPPUNIT_TEST( testMethod ) \
297 CPPUNIT_TEST_SUITE_ADD_TEST( \
298 ( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
299 context.getTestNameFor( #testMethod), \
300 &TestFixtureType::testMethod, \
301 context.makeFixture() ) ) )
303 /*! \brief Add a test which fail if the specified exception is not caught.
307 * #include <cppunit/extensions/HelperMacros.h>
309 * class MyTest : public CppUnit::TestFixture {
310 * CPPUNIT_TEST_SUITE( MyTest );
311 * CPPUNIT_TEST_EXCEPTION( testVectorAtThrow, std::invalid_argument );
312 * CPPUNIT_TEST_SUITE_END();
314 * void testVectorAtThrow()
316 * std::vector<int> v;
317 * v.at( 1 ); // must throw exception std::invalid_argument
322 * \param testMethod Name of the method of the test case to add to the suite.
323 * \param ExceptionType Type of the exception that must be thrown by the test
325 * \deprecated Use the assertion macro CPPUNIT_ASSERT_THROW instead.
327 #define CPPUNIT_TEST_EXCEPTION( testMethod, ExceptionType ) \
328 CPPUNIT_TEST_SUITE_ADD_TEST( \
329 (new CPPUNIT_NS::ExceptionTestCaseDecorator< ExceptionType >( \
330 new CPPUNIT_NS::TestCaller< TestFixtureType >( \
331 context.getTestNameFor( #testMethod ), \
332 &TestFixtureType::testMethod, \
333 context.makeFixture() ) ) ) )
335 /*! \brief Adds a test case which is excepted to fail.
337 * The added test case expect an assertion to fail. You usually used that type
338 * of test case when testing custom assertion macros.
341 * CPPUNIT_TEST_FAIL( testAssertFalseFail );
343 * void testAssertFalseFail()
345 * CPPUNIT_ASSERT( false );
348 * \see CreatingNewAssertions.
349 * \deprecated Use the assertion macro CPPUNIT_ASSERT_ASSERTION_FAIL instead.
351 #define CPPUNIT_TEST_FAIL( testMethod ) \
352 CPPUNIT_TEST_EXCEPTION( testMethod, CPPUNIT_NS::Exception )
354 /*! \brief Adds some custom test cases.
356 * Use this to add one or more test cases to the fixture suite. The specified
357 * method is called with a context parameter that can be used to name,
358 * instantiate fixture, and add instantiated test case to the fixture suite.
359 * The specified method must have the following signature:
361 * static void aMethodName( TestSuiteBuilderContextType &context );
364 * \c TestSuiteBuilderContextType is typedef to
365 * TestSuiteBuilderContext<TestFixtureType> declared by CPPUNIT_TEST_SUITE().
367 * Here is an example that add two custom tests:
370 * #include <cppunit/extensions/HelperMacros.h>
372 * class MyTest : public CppUnit::TestFixture {
373 * CPPUNIT_TEST_SUITE( MyTest );
374 * CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS( addTimeOutTests );
375 * CPPUNIT_TEST_SUITE_END();
377 * static void addTimeOutTests( TestSuiteBuilderContextType &context )
379 * context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test1" ) ),
381 * context.makeFixture(),
383 * context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test2" ) ),
385 * context.makeFixture(),
391 * // Do some test that may never end...
396 * // Do some test that may never end...
400 * @param testAdderMethod Name of the method called to add the test cases.
402 #define CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS( testAdderMethod ) \
403 testAdderMethod( context )
405 /*! \brief Adds a property to the test suite builder context.
406 * \param APropertyKey Key of the property to add.
407 * \param APropertyValue Value for the added property.
410 * CPPUNIT_TEST_SUITE_PROPERTY("XmlFileName", "paraTest.xml"); \endcode
412 #define CPPUNIT_TEST_SUITE_PROPERTY( APropertyKey, APropertyValue ) \
413 context.addProperty( std::string(APropertyKey), \
414 std::string(APropertyValue) )
420 /*! Adds the specified fixture suite to the unnamed registry.
421 * \ingroup CreatingTestSuite
423 * This macro declares a static variable whose construction
424 * causes a test suite factory to be inserted in a global registry
425 * of such factories. The registry is available by calling
426 * the static function CppUnit::TestFactoryRegistry::getRegistry().
428 * \param ATestFixtureType Type of the test case class.
429 * \warning This macro should be used only once per line of code (the line
430 * number is used to name a hidden static variable).
431 * \see CPPUNIT_TEST_SUITE_NAMED_REGISTRATION
432 * \see CPPUNIT_REGISTRY_ADD_TO_DEFAULT
433 * \see CPPUNIT_REGISTRY_ADD
434 * \see CPPUNIT_TEST_SUITE, CppUnit::AutoRegisterSuite,
435 * CppUnit::TestFactoryRegistry.
437 #define CPPUNIT_TEST_SUITE_REGISTRATION( ATestFixtureType ) \
438 static CPPUNIT_NS::AutoRegisterSuite< ATestFixtureType > \
439 CPPUNIT_MAKE_UNIQUE_NAME(autoRegisterRegistry__ )
442 /** Adds the specified fixture suite to the specified registry suite.
443 * \ingroup CreatingTestSuite
445 * This macro declares a static variable whose construction
446 * causes a test suite factory to be inserted in the global registry
447 * suite of the specified name. The registry is available by calling
448 * the static function CppUnit::TestFactoryRegistry::getRegistry().
450 * For the suite name, use a string returned by a static function rather
451 * than a hardcoded string. That way, you can know what are the name of
452 * named registry and you don't risk mistyping the registry name.
456 * namespace MySuites {
457 * std::string math() {
462 * // ComplexNumberTest.cpp
463 * #include "MySuites.h"
465 * CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ComplexNumberTest, MySuites::math() );
468 * \param ATestFixtureType Type of the test case class.
469 * \param suiteName Name of the global registry suite the test suite is
471 * \warning This macro should be used only once per line of code (the line
472 * number is used to name a hidden static variable).
473 * \see CPPUNIT_TEST_SUITE_REGISTRATION
474 * \see CPPUNIT_REGISTRY_ADD_TO_DEFAULT
475 * \see CPPUNIT_REGISTRY_ADD
476 * \see CPPUNIT_TEST_SUITE, CppUnit::AutoRegisterSuite,
477 * CppUnit::TestFactoryRegistry..
479 #define CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( ATestFixtureType, suiteName ) \
480 static CPPUNIT_NS::AutoRegisterSuite< ATestFixtureType > \
481 CPPUNIT_MAKE_UNIQUE_NAME(autoRegisterRegistry__ )(suiteName)
483 /*! Adds that the specified registry suite to another registry suite.
484 * \ingroup CreatingTestSuite
486 * Use this macros to automatically create test registry suite hierarchy. For example,
487 * if you want to create the following hierarchy:
494 * You can do this automatically with:
496 * CPPUNIT_REGISTRY_ADD( "FastFloat", "FloatMath" );
497 * CPPUNIT_REGISTRY_ADD( "IntegerMath", "Math" );
498 * CPPUNIT_REGISTRY_ADD( "FloatMath", "Math" );
499 * CPPUNIT_REGISTRY_ADD( "StandardFloat", "FloatMath" );
502 * There is no specific order of declaration. Think of it as declaring links.
504 * You register the test in each suite using CPPUNIT_TEST_SUITE_NAMED_REGISTRATION.
506 * \param which Name of the registry suite to add to the registry suite named \a to.
507 * \param to Name of the registry suite \a which is added to.
508 * \see CPPUNIT_REGISTRY_ADD_TO_DEFAULT, CPPUNIT_TEST_SUITE_NAMED_REGISTRATION.
510 #define CPPUNIT_REGISTRY_ADD( which, to ) \
511 static CPPUNIT_NS::AutoRegisterRegistry \
512 CPPUNIT_MAKE_UNIQUE_NAME( autoRegisterRegistry__ )( which, to )
514 /*! Adds that the specified registry suite to the default registry suite.
515 * \ingroup CreatingTestSuite
517 * This macro is just like CPPUNIT_REGISTRY_ADD except the specified registry
518 * suite is added to the default suite (root suite).
520 * \param which Name of the registry suite to add to the default registry suite.
521 * \see CPPUNIT_REGISTRY_ADD.
523 #define CPPUNIT_REGISTRY_ADD_TO_DEFAULT( which ) \
524 static CPPUNIT_NS::AutoRegisterRegistry \
525 CPPUNIT_MAKE_UNIQUE_NAME( autoRegisterRegistry__ )( which )
527 // Backwards compatibility
530 #if CPPUNIT_ENABLE_CU_TEST_MACROS
532 #define CU_TEST_SUITE(tc) CPPUNIT_TEST_SUITE(tc)
533 #define CU_TEST_SUB_SUITE(tc,sc) CPPUNIT_TEST_SUB_SUITE(tc,sc)
534 #define CU_TEST(tm) CPPUNIT_TEST(tm)
535 #define CU_TEST_SUITE_END() CPPUNIT_TEST_SUITE_END()
536 #define CU_TEST_SUITE_REGISTRATION(tc) CPPUNIT_TEST_SUITE_REGISTRATION(tc)
541 #endif // CPPUNIT_EXTENSIONS_HELPERMACROS_H