You can perform test case setup and cleanup actions using setUp and tearDown functions. setUp and tearDown are called before and after each test case, respectively.

To perform setup or cleanup actions for the whole test suite (called before and after each test suite), define custom setup/cleanup functions and register them with the following macros:

 CPPTEST_TEST_SUITE_SETUP(setup_function_name)  
 CPPTEST_TEST_SUITE_TEARDOWN(cleanup_function_name)

The registration should be added within the test suite declaration section (before the first test case registration). For example:

class MyTestSuite : public CppTest_TestSuite 
{
  public:
	CPPTEST_TEST_SUITE(MyTestSuite);
	CPPTEST_TEST_SUITE_SETUP(myTestSuiteSetUp);
	CPPTEST_TEST_SUITE_TEARDOWN(myTestSuiteTearDown); 
	CPPTEST_TEST(test1);
	CPPTEST_TEST(test2);
	CPPTEST_TEST_SUITE_END();
 
	void setUp(); 
	void tearDown();
 
	static void myTestSuiteSetUp(); 
	static void myTestSuiteTearDown();
 
	void test1(); 
	void test2();
};
 
void MyTestSuite::myTestSuiteSetUp()
{
  // Test suite setup
}
 
void MyTestSuite::myTestSuiteTearDown()
{
  // Test suite cleanup
}

Notes

  • Test suite setup/cleanup functions take no parameters.
  • We recommend using the static storage class specifier for C++ test suite setup/cleanup functions.
  • We do not recommend adding constructors/destructors to the test suite and attaching setup/teardown code to them because the test suite could be instantiated multiple times to run a subset of test cases each time—or even one per instance of the test suite. It is not correct to assume that the constructor is called only once before any test suite tests are executed, or that  the destructor is correspondingly called only after all tests have completed.
  • No labels