TestFixture.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #ifndef CPPUNIT_TESTFIXTURE_H // -*- C++ -*-
  2. #define CPPUNIT_TESTFIXTURE_H
  3. #include <cppunit/Portability.h>
  4. CPPUNIT_NS_BEGIN
  5. /*! \brief Wraps a test case with setUp and tearDown methods.
  6. * \ingroup WritingTestFixture
  7. *
  8. * A TestFixture is used to provide a common environment for a set
  9. * of test cases.
  10. *
  11. * To define a test fixture, do the following:
  12. * - implement a subclass of TestCase
  13. * - the fixture is defined by instance variables
  14. * - initialize the fixture state by overriding setUp
  15. * (i.e. construct the instance variables of the fixture)
  16. * - clean-up after a test by overriding tearDown.
  17. *
  18. * Each test runs in its own fixture so there
  19. * can be no side effects among test runs.
  20. * Here is an example:
  21. *
  22. * \code
  23. * class MathTest : public CppUnit::TestFixture {
  24. * protected:
  25. * int m_value1, m_value2;
  26. *
  27. * public:
  28. * MathTest() {}
  29. *
  30. * void setUp () {
  31. * m_value1 = 2;
  32. * m_value2 = 3;
  33. * }
  34. * }
  35. * \endcode
  36. *
  37. * For each test implement a method which interacts
  38. * with the fixture. Verify the expected results with assertions specified
  39. * by calling CPPUNIT_ASSERT on the expression you want to test:
  40. *
  41. * \code
  42. * public:
  43. * void testAdd () {
  44. * int result = m_value1 + m_value2;
  45. * CPPUNIT_ASSERT( result == 5 );
  46. * }
  47. * \endcode
  48. *
  49. * Once the methods are defined you can run them. To do this, use
  50. * a TestCaller.
  51. *
  52. * \code
  53. * CppUnit::Test *test = new CppUnit::TestCaller<MathTest>( "testAdd",
  54. * &MathTest::testAdd );
  55. * test->run();
  56. * \endcode
  57. *
  58. *
  59. * The tests to be run can be collected into a TestSuite.
  60. *
  61. * \code
  62. * public:
  63. * static CppUnit::TestSuite *MathTest::suite () {
  64. * CppUnit::TestSuite *suiteOfTests = new CppUnit::TestSuite;
  65. * suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
  66. * "testAdd", &MathTest::testAdd));
  67. * suiteOfTests->addTest(new CppUnit::TestCaller<MathTest>(
  68. * "testDivideByZero", &MathTest::testDivideByZero));
  69. * return suiteOfTests;
  70. * }
  71. * \endcode
  72. *
  73. * A set of macros have been created for convenience. They are located in HelperMacros.h.
  74. *
  75. * \see TestResult, TestSuite, TestCaller,
  76. * \see CPPUNIT_TEST_SUB_SUITE, CPPUNIT_TEST, CPPUNIT_TEST_SUITE_END,
  77. * \see CPPUNIT_TEST_SUITE_REGISTRATION, CPPUNIT_TEST_EXCEPTION, CPPUNIT_TEST_FAIL.
  78. */
  79. class CPPUNIT_API TestFixture
  80. {
  81. public:
  82. virtual ~TestFixture() {};
  83. //! \brief Set up context before running a test.
  84. virtual void setUp() {};
  85. //! Clean up after the test run.
  86. virtual void tearDown() {};
  87. };
  88. CPPUNIT_NS_END
  89. #endif