Menu

[r261]: / trunk / cppunit / include / cppunit / TestListener.h  Maximize  Restore  History

Download this file

125 lines (108 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#ifndef CPPUNIT_TESTLISTENER_H // -*- C++ -*-
#define CPPUNIT_TESTLISTENER_H
#include <cppunit/Portability.h>
namespace CppUnit {
class Exception;
class Test;
class TestFailure;
/*! \brief Listener for test progress and result.
* \ingroup TrackingTestExecution
*
* Implementing the Observer pattern a TestListener may be registered
* to a TestResult to obtain information on the testing progress. Use
* specialized sub classes of TestListener for text output
* (TextTestProgressListener). Do not use the Listener for the test
* result output, use a subclass of Outputter instead.
*
* The test framework distinguishes between failures and errors.
* A failure is anticipated and checked for with assertions. Errors are
* unanticipated problems signified by exceptions that are not generated
* by the framework.
*
* Here is an example to track test time:
*
*
* \code
* #include <cppunit/TestListener.h>
* #include <cppunit/Test.h>
* #include <time.h> // for clock()
*
* class TimingListener : public CppUnit::TestListener
* {
* public:
* void startTest( CppUnit::Test *test )
* {
* _chronometer.start();
* }
*
* void endTest( CppUnit::Test *test )
* {
* _chronometer.end();
* addTest( test, _chronometer.elapsedTime() );
* }
*
* // ... (interface to add/read test timing result)
*
* private:
* Clock _chronometer;
* };
* \endcode
*
* And another example that track failure/success at test suite level and captures
* the TestPath of each suite:
* \code
* class SuiteTracker : public CppUnit::TestListener
* {
* public:
* void startSuite( CppUnit::Test *suite )
* {
* m_currentPath.add( suite );
* }
*
* void addFailure( const TestFailure &failure )
* {
* m_suiteFailure.top() = false;
* }
*
* void endSuite( CppUnit::Test *suite )
* {
* m_suiteStatus.insert( std::make_pair( suite, m_suiteFailure.top() ) );
* m_suitePaths.insert( std::make_pair( suite, m_currentPath ) );
*
* m_currentPath.up();
* m_suiteFailure.pop();
* }
*
* private:
* std::stack<bool> m_suiteFailure;
* CppUnit::TestPath m_currentPath;
* std::map<CppUnit::Test *, bool> m_suiteStatus;
* std::map<CppUnit::Test *, CppUnit::TestPath> m_suitePaths;
* };
* \endcode
*
* \see TestResult
*/
class CPPUNIT_API TestListener
{
public:
virtual ~TestListener() {}
/// Called when just before a TestCase is run.
virtual void startTest( Test *test ) {}
/*! Called when a failure occurs while running a test.
* \see TestFailure.
* \warning \a failure is a temporary object that is destroyed after the
* method call. Use TestFailure::clone() to create a duplicate.
*/
virtual void addFailure( const TestFailure &failure ) {}
/// Called just after a TestCase was run (even if a failure occured).
virtual void endTest( Test *test ) {}
/*! Called by a TestComposite just before running its child tests.
*/
virtual void startSuite( Test *suite ) {}
/*! Called by a TestComposite after running its child tests.
*/
virtual void endSuite( Test *suite ) {}
};
} // namespace CppUnit
#endif // CPPUNIT_TESTLISTENER_H
MongoDB Logo MongoDB