CppUnit project page FAQ

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. More...
 
class  TestFixture
 Wraps a test case with setUp and tearDown methods. More...
 

Macros

#define CPPUNIT_TEST_SUITE(ATestFixtureType)
 Begin test suite.
 
#define CPPUNIT_TEST_SUB_SUITE(ATestFixtureType, ASuperClass)
 Begin test suite (includes parent suite)
 
#define CPPUNIT_TEST_SUITE_END()
 End declaration of the test suite.
 
#define CPPUNIT_TEST_SUITE_END_ABSTRACT()
 End declaration of an abstract test suite.
 
#define CPPUNIT_TEST_SUITE_ADD_TEST(test)    context.addTest( test )
 Add a test to the suite (for custom test macro).
 
#define CPPUNIT_TEST(testMethod)
 Add a method to the suite.
 
#define CPPUNIT_TEST_PARAMETERIZED(testMethod, ...)
 
#define CPPUNIT_TEST_EXCEPTION(testMethod, ExceptionType)
 Add a test which fail if the specified exception is not caught.
 
#define CPPUNIT_TEST_FAIL(testMethod)    CPPUNIT_TEST_EXCEPTION( testMethod, CPPUNIT_NS::Exception )
 Adds a test case which is excepted to fail.
 
#define CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS(testAdderMethod)    testAdderMethod( context )
 Adds some custom test cases.
 
#define CPPUNIT_TEST_SUITE_PROPERTY(APropertyKey, APropertyValue)
 Adds a property to the test suite builder context.
 

Detailed Description

Macro Definition Documentation

◆ CPPUNIT_TEST

#define CPPUNIT_TEST ( testMethod)
Value:
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor( #testMethod), \
context.makeFixture() ) ) )
Generate a test case from a fixture method.
Definition TestCaller.h:107
TestCaller(std::string name, TestMethod test)
Definition TestCaller.h:117
#define CPPUNIT_TEST_SUITE_ADD_TEST(test)
Add a test to the suite (for custom test macro).
Definition HelperMacros.h:288

Add a method to the suite.

Parameters
testMethodName 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.

◆ CPPUNIT_TEST_EXCEPTION

#define CPPUNIT_TEST_EXCEPTION ( testMethod,
ExceptionType )
Value:
(new CPPUNIT_NS::ExceptionTestCaseDecorator< ExceptionType >( \
new CPPUNIT_NS::TestCaller< TestFixtureType >( \
context.getTestNameFor( #testMethod ), \
context.makeFixture() ) ) ) )

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

Example:

#include <vector>
class MyTest : public CppUnit::TestFixture {
public:
{
std::vector<int> v;
v.at( 1 ); // must throw exception std::out_of_range
}
};
Macros intended to ease the definition of test suites.
#define CPPUNIT_TEST_SUITE_END()
End declaration of the test suite.
Definition HelperMacros.h:166
#define CPPUNIT_TEST_SUITE(ATestFixtureType)
Begin test suite.
Definition HelperMacros.h:100
#define CPPUNIT_TEST_EXCEPTION(testMethod, ExceptionType)
Add a test which fail if the specified exception is not caught.
Definition HelperMacros.h:339
Parameters
testMethodName of the method of the test case to add to the suite.
ExceptionTypeType of the exception that must be thrown by the test method.
Deprecated
Use the assertion macro CPPUNIT_ASSERT_THROW instead.

◆ CPPUNIT_TEST_FAIL

#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.

{
CPPUNIT_ASSERT( false );
}
#define CPPUNIT_ASSERT(condition)
Assertions that a condition is true.
Definition TestAssert.h:239
#define CPPUNIT_TEST_FAIL(testMethod)
Adds a test case which is excepted to fail.
Definition HelperMacros.h:363
See also
Creating custom assertions.
Deprecated
Use the assertion macro CPPUNIT_ASSERT_ASSERTION_FAIL instead.

◆ CPPUNIT_TEST_PARAMETERIZED

#define CPPUNIT_TEST_PARAMETERIZED ( testMethod,
... )
Value:
for (auto& i : __VA_ARGS__) \
{ \
TestFixtureType* fixture = context.makeFixture(); \
( new CPPUNIT_NS::TestCaller<TestFixtureType>( \
context.getTestNameFor(#testMethod, i), \
fixture))); \
}

◆ CPPUNIT_TEST_SUB_SUITE

#define CPPUNIT_TEST_SUB_SUITE ( ATestFixtureType,
ASuperClass )
Value:

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:

class MySubTest : public MyTest {
public:
void testAdd();
void testSub();
};
#define CPPUNIT_TEST(testMethod)
Add a method to the suite.
Definition HelperMacros.h:297
#define CPPUNIT_TEST_SUB_SUITE(ATestFixtureType, ASuperClass)
Begin test suite (includes parent suite)
Definition HelperMacros.h:151
Parameters
ATestFixtureTypeType of the test case class. This type MUST be derived from TestFixture.
ASuperClassType of the parent class.
See also
CPPUNIT_TEST_SUITE.

◆ CPPUNIT_TEST_SUITE

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

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
ATestFixtureTypeType 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.

◆ CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS

#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:

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

Here is an example that add two custom tests:

class MyTest : public CppUnit::TestFixture {
public:
{
context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test1" ) ),
context.makeFixture(),
5.0 );
context.addTest( new TimeOutTestCaller( context.getTestNameFor( "test2" ) ),
context.makeFixture(),
5.0 );
}
void test1()
{
// Do some test that may never end...
}
void test2()
{
// Do some test that may never end...
}
};
#define CPPUNIT_TEST_SUITE_ADD_CUSTOM_TESTS(testAdderMethod)
Adds some custom test cases.
Definition HelperMacros.h:414
Parameters
testAdderMethodName of the method called to add the test cases.

◆ CPPUNIT_TEST_SUITE_ADD_TEST

#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:

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.

#define CPPUNITEX_TEST_TIMELIMIT( testMethod, timeLimit ) \
CPPUNIT_TEST_SUITE_ADD_TEST( (new TimeOutTestCaller<TestFixtureType>( \
namer.getTestNameFor( #testMethod ), \
&TestFixtureType::testMethod, \
factory.makeFixture(), \
timeLimit ) ) )
class PerformanceTest : CppUnit::TestFixture
{
public:
};
Parameters
testTest to add to the suite. Must be a subclass of Test. The test name should have been obtained using TestNamer::getTestNameFor().

◆ CPPUNIT_TEST_SUITE_END

#define CPPUNIT_TEST_SUITE_END ( )
Value:
} \
\
public: \
static CPPUNIT_NS::TestSuite *suite() \
{ \
const CPPUNIT_NS::TestNamer &namer = getTestNamer__(); \
std::unique_ptr<CPPUNIT_NS::TestSuite> guard( \
new CPPUNIT_NS::TestSuite( namer.getFixtureName() )); \
CPPUNIT_NS::ConcretTestFixtureFactory<TestFixtureType> factory; \
CPPUNIT_NS::TestSuiteBuilderContextBase context( *guard.get(), \
namer, \
return guard.release(); \
} \
private: /* dummy typedef so that the macro can still end with ';'*/ \

End declaration of the test suite.

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

See also
CPPUNIT_TEST_SUITE.
CPPUNIT_TEST_SUITE_REGISTRATION.

◆ CPPUNIT_TEST_SUITE_END_ABSTRACT

#define CPPUNIT_TEST_SUITE_END_ABSTRACT ( )
Value:
} \
private: /* dummy typedef so that the macro can still end with ';'*/ \

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:

class AbstractDocumentTest : public CppUnit::TestFixture {
public:
void setUp()
{
}
void tearDown()
{
delete m_document;
}
protected:
};
void tearDown()
Clean up after the test run.
Definition TestCaller.h:183
void setUp()
Set up context before running a test.
Definition TestCaller.h:178
#define CPPUNIT_TEST_SUITE_END_ABSTRACT()
End declaration of an abstract test suite.
Definition HelperMacros.h:238

The concret test fixture:

See also
CPPUNIT_TEST_SUB_SUITE.
CPPUNIT_TEST_SUITE_REGISTRATION.

◆ CPPUNIT_TEST_SUITE_PROPERTY

#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
APropertyKeyKey of the property to add.
APropertyValueValue for the added property. Example:
CPPUNIT_TEST_SUITE_PROPERTY("XmlFileName", "paraTest.xml");
#define CPPUNIT_TEST_SUITE_PROPERTY(APropertyKey, APropertyValue)
Adds a property to the test suite builder context.
Definition HelperMacros.h:424

Send comments to:
CppUnit Developers