TestResult.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef CPPUNIT_TESTRESULT_H
  2. #define CPPUNIT_TESTRESULT_H
  3. #include <cppunit/Portability.h>
  4. #if CPPUNIT_NEED_DLL_DECL
  5. #pragma warning( push )
  6. #pragma warning( disable: 4251 ) // X needs to have dll-interface to be used by clients of class Z
  7. #endif
  8. #include <cppunit/SynchronizedObject.h>
  9. #include <cppunit/portability/CppUnitDeque.h>
  10. #include <string>
  11. CPPUNIT_NS_BEGIN
  12. class Exception;
  13. class Functor;
  14. class Protector;
  15. class ProtectorChain;
  16. class Test;
  17. class TestFailure;
  18. class TestListener;
  19. #if CPPUNIT_NEED_DLL_DECL
  20. // template class CPPUNIT_API std::deque<TestListener *>;
  21. #endif
  22. /*! \brief Manages TestListener.
  23. * \ingroup TrackingTestExecution
  24. *
  25. * A single instance of this class is used when running the test. It is usually
  26. * created by the test runner (TestRunner).
  27. *
  28. * This class shouldn't have to be inherited from. Use a TestListener
  29. * or one of its subclasses to be informed of the ongoing tests.
  30. * Use a Outputter to receive a test summary once it has finished
  31. *
  32. * TestResult supplies a template method 'setSynchronizationObject()'
  33. * so that subclasses can provide mutual exclusion in the face of multiple
  34. * threads. This can be useful when tests execute in one thread and
  35. * they fill a subclass of TestResult which effects change in another
  36. * thread. To have mutual exclusion, override setSynchronizationObject()
  37. * and make sure that you create an instance of ExclusiveZone at the
  38. * beginning of each method.
  39. *
  40. * \see Test, TestListener, TestResultCollector, Outputter.
  41. */
  42. class CPPUNIT_API TestResult : protected SynchronizedObject
  43. {
  44. public:
  45. /// Construct a TestResult
  46. TestResult( SynchronizationObject *syncObject = 0 );
  47. /// Destroys a test result
  48. virtual ~TestResult();
  49. virtual void addListener( TestListener *listener );
  50. virtual void removeListener( TestListener *listener );
  51. /// Resets the stop flag.
  52. virtual void reset();
  53. /// Stop testing
  54. virtual void stop();
  55. /// Returns whether testing should be stopped
  56. virtual bool shouldStop() const;
  57. /// Informs TestListener that a test will be started.
  58. virtual void startTest( Test *test );
  59. /*! \brief Adds an error to the list of errors.
  60. * The passed in exception
  61. * caused the error
  62. */
  63. virtual void addError( Test *test, Exception *e );
  64. /*! \brief Adds a failure to the list of failures. The passed in exception
  65. * caused the failure.
  66. */
  67. virtual void addFailure( Test *test, Exception *e );
  68. /// Informs TestListener that a test was completed.
  69. virtual void endTest( Test *test );
  70. /// Informs TestListener that a test suite will be started.
  71. virtual void startSuite( Test *test );
  72. /// Informs TestListener that a test suite was completed.
  73. virtual void endSuite( Test *test );
  74. /*! \brief Run the specified test.
  75. *
  76. * Calls startTestRun(), test->run(this), and finally endTestRun().
  77. */
  78. virtual void runTest( Test *test );
  79. /*! \brief Protects a call to the specified functor.
  80. *
  81. * See Protector to understand how protector works. A default protector is
  82. * always present. It captures CppUnit::Exception, std::exception and
  83. * any other exceptions, retrieving as much as possible information about
  84. * the exception as possible.
  85. *
  86. * Additional Protector can be added to the chain to support other exception
  87. * types using pushProtector() and popProtector().
  88. *
  89. * \param functor Functor to call (typically a call to setUp(), runTest() or
  90. * tearDown().
  91. * \param test Test the functor is associated to (used for failure reporting).
  92. * \param shortDescription Short description override for the failure message.
  93. */
  94. virtual bool protect( const Functor &functor,
  95. Test *test,
  96. const std::string &shortDescription = std::string("") );
  97. /// Adds the specified protector to the protector chain.
  98. virtual void pushProtector( Protector *protector );
  99. /// Removes the last protector from the protector chain.
  100. virtual void popProtector();
  101. protected:
  102. /*! \brief Called to add a failure to the list of failures.
  103. */
  104. void addFailure( const TestFailure &failure );
  105. virtual void startTestRun( Test *test );
  106. virtual void endTestRun( Test *test );
  107. protected:
  108. typedef CppUnitDeque<TestListener *> TestListeners;
  109. TestListeners m_listeners;
  110. ProtectorChain *m_protectorChain;
  111. bool m_stop;
  112. private:
  113. TestResult( const TestResult &other );
  114. TestResult &operator =( const TestResult &other );
  115. };
  116. CPPUNIT_NS_END
  117. #if CPPUNIT_NEED_DLL_DECL
  118. #pragma warning( pop )
  119. #endif
  120. #endif // CPPUNIT_TESTRESULT_H