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

Writing test fixture


Files

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


Compounds

class  TestCaller
 Generate a test case from a fixture method. More...

class  TestFixture
 Wraps a test case with setUp and tearDown methods. More...

class  TestSuiteBuilder
 Helper to add tests to a TestSuite. More...


Defines

#define CPPUNIT_TEST_SUITE(ATestFixtureType)
 Begin test suite.

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

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

#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::Exception )
 Adds a test case which is excepted to fail.

#define CPPUNIT_TEST_SUITE_END()
 End declaration of the test suite.


Define Documentation

#define CPPUNIT_TEST testMethod   ) 
 

Value:

builder.addTestCaller( #testMethod,                                    \
                             &__ThisTestFixtureType::testMethod ,            \
                             (__ThisTestFixtureType*)factory->makeFixture() )
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:

builder.addTestCallerForException( #testMethod,                         \
                             &__ThisTestFixtureType::testMethod ,             \
                             (__ThisTestFixtureType*)factory->makeFixture(),  \
                             (ExceptionType *)NULL );
Add a test which fail if the specified exception is not caught.

Example:

 #include <cppunit/extensions/HelperMacros.h>
 #include <vector>
 class MyTest : public CppUnit::TestFixture {
   CPPUNIT_TEST_SUITE( MyTest );
   CPPUNIT_TEST_EXCEPTION( testVectorAtThrow, std::invalid_argument );
   CPPUNIT_TEST_SUITE_END();
 public:
   void testVectorAtThrow()
   {
     std::vector<int> v;
     v.at( 1 );     // must throw exception std::invalid_argument
   }
 };

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.

#define CPPUNIT_TEST_FAIL testMethod   )     CPPUNIT_TEST_EXCEPTION( testMethod, CppUnit::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_TEST_FAIL( testAssertFalseFail );
 
 void testAssertFalseFail()
 {
   CPPUNIT_ASSERT( false );
 }
See also:
CreatingNewAssertions.

#define CPPUNIT_TEST_SUB_SUITE ATestFixtureType,
ASuperClass   ) 
 

Value:

private:                                                       \
    typedef ASuperClass __ThisSuperClassType;                    \
    CPPUNIT_TEST_SUITE( ATestFixtureType );                      \
      __ThisSuperClassType::registerTests( suite, factory )
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:

 #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();
 };

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:

private:                                                                \
    typedef ATestFixtureType __ThisTestFixtureType;                       \
    class ThisTestFixtureFactory : public CppUnit::TestFixtureFactory     \
    {                                                                     \
      virtual CppUnit::TestFixture *makeFixture()                         \
      {                                                                   \
        return new ATestFixtureType();                                    \
      }                                                                   \
    };                                                                    \
  public:                                                                 \
    static void                                                           \
    registerTests( CppUnit::TestSuite *suite,                             \
                   CppUnit::TestFixtureFactory *factory )                 \
    {                                                                     \
      CppUnit::TestSuiteBuilder<__ThisTestFixtureType> builder( suite );
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_END  ) 
 

Value:

builder.takeSuite();                                                \
    }                                                                     \
    static CppUnit::TestSuite *suite()                                    \
    {                                                                     \
      CppUnit::TestSuiteBuilder<__ThisTestFixtureType>                    \
          builder __CPPUNIT_SUITE_CTOR_ARGS( ATestFixtureType );          \
      ThisTestFixtureFactory factory;                                     \
      __ThisTestFixtureType::registerTests( builder.suite(), &factory );  \
      return builder.takeSuite();                                         \
    }                                                                     \
  private:      \
    typedef ThisTestFixtureFactory __ThisTestFixtureFactory
End declaration of the test suite.

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

See also:
CPPUNIT_TEST_SUITE.

CPPUNIT_TEST_SUITE_REGISTRATION.


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