CppUnit project page | FAQ

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

Files | Classes | Macros
Writing test fixture

## Files

file | HelperMacros.h
| Macros intended to ease the definition of test suites.

## Classes

class | TestCaller< Fixture >
| Generate a test case from a fixture method.A test caller provides access to a test case method on a test fixture class. Test callers are useful when you want to run an individual test or add it to a suite. Test Callers invoke only one Test (i.e. test method) on one Fixture of a TestFixture. More...
class | TestFixture
| Wraps a test case with setUp and tearDown methods.A TestFixture is used to provide a common environment for a set of test cases. More...

## Macros

#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_SUITE_END()
| End declaration of the test suite. More...
#define | CPPUNIT_TEST_SUITE_END_ABSTRACT()
| End declaration of an abstract test suite. More...
#define | CPPUNIT_TEST_SUITE_ADD_TEST(test) context.addTest( test )
| Add a test to the suite (for custom test macro). 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_NS::Exception )
| Adds a test case which is excepted to fail. More...
#define | CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS(testAdderMethod) testAdderMethod( context )
| Adds some custom test cases. More...
#define | CPPUNIT_TEST_SUITE_PROPERTY(APropertyKey, APropertyValue)
| Adds a property to the test suite builder context. More...

## Detailed Description

## Macro Definition Documentation

#define CPPUNIT_TEST | ( |  | testMethod | ) |

Value:
CPPUNIT_TEST_SUITE_ADD_TEST( \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) )
CPPUNIT_TEST_SUITE_ADD_TEST
#define CPPUNIT_TEST_SUITE_ADD_TEST(test)
Add a test to the suite (for custom test macro).
Definition: HelperMacros.h:297

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_EXCEPTION | ( |  | testMethod,
|  |  | ExceptionType
| ) |  |

Value:
CPPUNIT_TEST_SUITE_ADD_TEST( \
(new CPPUNIT_NS::ExceptionTestCaseDecorator< ExceptionType >( \
new CPPUNIT_NS::TestCaller< TestFixtureType >( \
context.getTestNameFor( #testMethod ), \
&TestFixtureType::testMethod, \
context.makeFixture() ) ) ) )
CPPUNIT_TEST_SUITE_ADD_TEST
#define CPPUNIT_TEST_SUITE_ADD_TEST(test)
Add a test to the suite (for custom test macro).
Definition: HelperMacros.h:297

Add a test which fail if the specified exception is not caught.

Example:

1 #include <cppunit/extensions/HelperMacros.h>
2 #include <vector>
3 class MyTest : public CppUnit::TestFixture {
4 CPPUNIT_TEST_SUITE( MyTest );
5 CPPUNIT_TEST_EXCEPTION( testVectorAtThrow, std::out_of_range );
6 CPPUNIT_TEST_SUITE_END();
7 public:
8 void testVectorAtThrow()
9 {
10 std::vector<int> v;
11 v.at( 1 ); // must throw exception std::out_of_range
12 }
13 };

Parameters

  testMethod | Name of the method of the test case to add to the suite.
  ExceptionType | Type of the exception that must be thrown by the test method.

Deprecated:
  Use the assertion macro CPPUNIT_ASSERT_THROW instead.

#define CPPUNIT_TEST_FAIL | ( |  | testMethod | ) | CPPUNIT_TEST_EXCEPTION( testMethod, CPPUNIT_NS::Exception )

Adds a test case which is excepted to fail.

The added test case expect an assertion to fail. You usually used that type of test case when testing custom assertion macros.

1 CPPUNIT_TEST_FAIL( testAssertFalseFail );
2
3 void testAssertFalseFail()
4 {
5 CPPUNIT_ASSERT( false );
6 }

See also
  Creating custom assertions.

Deprecated:
  Use the assertion macro CPPUNIT_ASSERT_ASSERTION_FAIL instead.

#define CPPUNIT_TEST_SUB_SUITE | ( |  | ATestFixtureType,
|  |  | ASuperClass
| ) |  |

Value:
public: \
typedef ASuperClass ParentTestFixtureType; \
private: \
CPPUNIT_TEST_SUITE( ATestFixtureType ); \
ParentTestFixtureType::addTestsToSuite( baseContext )
CPPUNIT_TEST_SUITE
#define CPPUNIT_TEST_SUITE(ATestFixtureType)
Begin test suite.
Definition: HelperMacros.h:100

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:

1 #include <cppunit/extensions/HelperMacros.h>
2 class MySubTest : public MyTest {
3 CPPUNIT_TEST_SUB_SUITE( MySubTest, MyTest );
4 CPPUNIT_TEST( testAdd );
5 CPPUNIT_TEST( testSub );
6 CPPUNIT_TEST_SUITE_END();
7 public:
8 void testAdd();
9 void testSub();
10 };

Parameters

  ATestFixtureType | Type of the test case class. This type MUST be derived from TestFixture.
  ASuperClass | Type of the parent class.

See also
  CPPUNIT_TEST_SUITE.

#define CPPUNIT_TEST_SUITE | ( |  | ATestFixtureType | ) |

Value:
public: \
typedef ATestFixtureType TestFixtureType; \
\
private: \
static const CPPUNIT_NS::TestNamer &getTestNamer__() \
{ \
static CPPUNIT_TESTNAMER_DECL( testNamer, ATestFixtureType ); \
return testNamer; \
} \
\
public: \
typedef CPPUNIT_NS::TestSuiteBuilderContext<TestFixtureType> \
TestSuiteBuilderContextType; \
\
static void \
addTestsToSuite( CPPUNIT_NS::TestSuiteBuilderContextBase &baseContext ) \
{ \
TestSuiteBuilderContextType context( baseContext )
CPPUNIT_TESTNAMER_DECL
#define CPPUNIT_TESTNAMER_DECL(variableName, FixtureType)
Declares a TestNamer.
Definition: TestNamer.h:36

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

  ATestFixtureType | Type of the test case class. This type MUST be derived from TestFixture.

See also
  CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END,
  CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL.

#define CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS | ( |  | testAdderMethod | ) | testAdderMethod( context )

Adds some custom test cases.

Use this to add one or more test cases to the fixture suite. The specified method is called with a context parameter that can be used to name, instantiate fixture, and add instantiated test case to the fixture suite. The specified method must have the following signature:

1 static void aMethodName( TestSuiteBuilderContextType &context );

`TestSuiteBuilderContextType` is typedef to TestSuiteBuilderContext<TestFixtureType> declared by CPPUNIT_TEST_SUITE().

Here is an example that add two custom tests:

1 #include <cppunit/extensions/HelperMacros.h>
2
3 class MyTest : public CppUnit::TestFixture {
4 CPPUNIT_TEST_SUITE( MyTest );
5 CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS( addTimeOutTests );
6 CPPUNIT_TEST_SUITE_END();
7 public:
8 static void addTimeOutTests( TestSuiteBuilderContextType &context )
9 {
10 context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test1" ) ),
11 &MyTest::test1,
12 context.makeFixture(),
13 5.0 );
14 context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test2" ) ),
15 &MyTest::test2,
16 context.makeFixture(),
17 5.0 );
18 }
19
20 void test1()
21 {
22 // Do some test that may never end...
23 }
24
25 void test2()
26 {
27 // Do some test that may never end...
28 }
29 };

Parameters

  testAdderMethod | Name of the method called to add the test cases.

#define CPPUNIT_TEST_SUITE_ADD_TEST | ( |  | test | ) | context.addTest( test )

Add a test to the suite (for custom test macro).

The specified test will be added to the test suite being declared. This macro is intended for advanced usage, to extend CppUnit by creating new macro such as CPPUNIT_TEST_EXCEPTION()...

Between macro CPPUNIT_TEST_SUITE() and CPPUNIT_TEST_SUITE_END(), you can assume that the following variables can be used:

1 typedef TestSuiteBuilder<TestFixtureType> TestSuiteBuilderType;
2 TestSuiteBuilderType &context;

`context` can be used to name test case, create new test fixture instance, or add test case to the test fixture suite.

Below is an example that show how to use this macro to create new macro to add test to the fixture suite. The macro below show how you would add a new type of test case which fails if the execution last more than a given time limit. It relies on an imaginary TimeOutTestCaller class which has an interface similar to TestCaller.

1 #define CPPUNITEX_TEST_TIMELIMIT( testMethod, timeLimit ) \
2 CPPUNIT_TEST_SUITE_ADD_TEST( (new TimeOutTestCaller<TestFixtureType>( \
3 namer.getTestNameFor( #testMethod ), \
4 &TestFixtureType::testMethod, \
5 factory.makeFixture(), \
6 timeLimit ) ) )
7
8 class PerformanceTest : CppUnit::TestFixture
9 {
10 public:
11 CPPUNIT_TEST_SUITE( PerformanceTest );
12 CPPUNITEX_TEST_TIMELIMIT( testSortReverseOrder, 5.0 );
13 CPPUNIT_TEST_SUITE_END();
14
15 void testSortReverseOrder();
16 };

Parameters

  test | Test to add to the suite. Must be a subclass of Test. The test name should have been obtained using TestNamer::getTestNameFor().

#define CPPUNIT_TEST_SUITE_END | ( |  | ) |

Value:
} \
\
struct CppUnitExDeleter { /* avoid deprecated auto_ptr warnings */ \
CPPUNIT_NS::TestSuite *suite; \
CppUnitExDeleter() : suite (0) {} \
~CppUnitExDeleter() { delete suite; } \
CPPUNIT_NS::TestSuite *release() { \
CPPUNIT_NS::TestSuite *tmp = suite; suite = NULL; return tmp; \
} \
}; \
\
public: \
static CPPUNIT_NS::TestSuite *suite() \
{ \
const CPPUNIT_NS::TestNamer &namer = getTestNamer__(); \
CppUnitExDeleter guard; \
guard.suite = new CPPUNIT_NS::TestSuite( namer.getFixtureName() ); \
CPPUNIT_NS::ConcretTestFixtureFactory<TestFixtureType> factory; \
CPPUNIT_NS::TestSuiteBuilderContextBase context( *guard.suite, \
namer, \
factory ); \
TestFixtureType::addTestsToSuite( context ); \
return guard.release(); \
} \
private: /* dummy typedef so that the macro can still end with ';'*/ \
typedef int CppUnitDummyTypedefForSemiColonEnding__

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_END_ABSTRACT | ( |  | ) |

Value:
} \
private: /* dummy typedef so that the macro can still end with ';'*/ \
typedef int CppUnitDummyTypedefForSemiColonEnding__

End declaration of an abstract test suite.

Use this macro to indicate that the TestFixture is abstract. No static suite() method will be declared.

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

Here is an example of usage:

The abstract test fixture:

1 #include <cppunit/extensions/HelperMacros.h>
2 class AbstractDocument;
3 class AbstractDocumentTest : public CppUnit::TestFixture {
4 CPPUNIT_TEST_SUITE( AbstractDocumentTest );
5 CPPUNIT_TEST( testInsertText );
6 CPPUNIT_TEST_SUITE_END_ABSTRACT();
7 public:
8 void testInsertText();
9
10 void setUp()
11 {
12 m_document = makeDocument();
13 }
14
15 void tearDown()
16 {
17 delete m_document;
18 }
19 protected:
20 virtual AbstractDocument *makeDocument() =0;
21
22 AbstractDocument *m_document;
23 };

The concret test fixture:

1 class RichTextDocumentTest : public AbstractDocumentTest {
2 CPPUNIT_TEST_SUB_SUITE( RichTextDocumentTest, AbstractDocumentTest );
3 CPPUNIT_TEST( testInsertFormatedText );
4 CPPUNIT_TEST_SUITE_END();
5 public:
6 void testInsertFormatedText();
7 protected:
8 AbstractDocument *makeDocument()
9 {
10 return new RichTextDocument();
11 }
12 };

See also
  CPPUNIT_TEST_SUB_SUITE.
  CPPUNIT_TEST_SUITE_REGISTRATION.

#define CPPUNIT_TEST_SUITE_PROPERTY | ( |  | APropertyKey,
|  |  | APropertyValue
| ) |  |

Value:
context.addProperty( std::string(APropertyKey), \
std::string(APropertyValue) )

Adds a property to the test suite builder context.

Parameters

  APropertyKey | Key of the property to add.
  APropertyValue | Value for the added property. Example: 1 CPPUNIT_TEST_SUITE_PROPERTY("XmlFileName", "paraTest.xml");

| Send comments to: CppUnit Developers