1275072Semaste#ifndef CPPUNIT_TESTFIXTURE_H // -*- C++ -*- 2275072Semaste#define CPPUNIT_TESTFIXTURE_H 3275072Semaste 4275072Semaste#include <cppunit/Portability.h> 5275072Semaste 6275072Semastenamespace CppUnit { 7275072Semaste 8275072Semaste 9275072Semaste/*! \brief Wraps a test case with setUp and tearDown methods. 10275072Semaste * \ingroup WritingTestFixture 11275072Semaste * 12275072Semaste * A TestFixture is used to provide a common environment for a set 13275072Semaste * of test cases. 14275072Semaste * 15275072Semaste * To define a test fixture, do the following: 16275072Semaste * - implement a subclass of TestCase 17275072Semaste * - the fixture is defined by instance variables 18275072Semaste * - initialize the fixture state by overriding setUp 19275072Semaste * (i.e. construct the instance variables of the fixture) 20280031Sdim * - clean-up after a test by overriding tearDown. 21280031Sdim * 22280031Sdim * Each test runs in its own fixture so there 23280031Sdim * can be no side effects among test runs. 24280031Sdim * Here is an example: 25280031Sdim * 26280031Sdim * \code 27280031Sdim * class MathTest : public CppUnit::TestFixture { 28280031Sdim * protected: 29275072Semaste * int m_value1, m_value2; 30275072Semaste * 31275072Semaste * public: 32280031Sdim * MathTest() {} 33280031Sdim * 34296417Sdim * void setUp () { 35280031Sdim * m_value1 = 2; 36280031Sdim * m_value2 = 3; 37280031Sdim * } 38280031Sdim * } 39280031Sdim * \endcode 40275072Semaste * 41280031Sdim * For each test implement a method which interacts 42280031Sdim * with the fixture. Verify the expected results with assertions specified 43280031Sdim * by calling CPPUNIT_ASSERT on the expression you want to test: 44296417Sdim * 45280031Sdim * \code 46280031Sdim * public: 47280031Sdim * void testAdd () { 48280031Sdim * int result = m_value1 + m_value2; 49288943Sdim * CPPUNIT_ASSERT( result == 5 ); 50288943Sdim * } 51275072Semaste * \endcode 52 * 53 * Once the methods are defined you can run them. To do this, use 54 * a TestCaller. 55 * 56 * \code 57 * CppUnit::Test *test = new CppUnit::TestCaller<MathTest>( "testAdd", 58 * &MathTest::testAdd ); 59 * test->run(); 60 * \endcode 61 * 62 * 63 * The tests to be run can be collected into a TestSuite. 64 * 65 * \code 66 * public: 67 * static CppUnit::TestSuite *MathTest::suite () { 68 * CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite; 69 * suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>( 70 * "testAdd", &MathTest::testAdd)); 71 * suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>( 72 * "testDivideByZero", &MathTest::testDivideByZero)); 73 * return suiteOfTests; 74 * } 75 * \endcode 76 * 77 * A set of macros have been created for convenience. They are located in HelperMacros.h. 78 * 79 * \see TestResult, TestSuite, TestCaller, 80 * \see CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END, 81 * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL. 82 */ 83class CPPUNIT_API TestFixture 84{ 85public: 86 virtual ~TestFixture() {}; 87 88 //! \brief Set up context before running a test. 89 virtual void setUp() {}; 90 91 //! Clean up after the test run. 92 virtual void tearDown() {}; 93}; 94 95 96} 97 98#endif 99