TestListener

   
TestListener

Description

The class TestListener (see Figure C-25) defines an interface for observers to receive test progress notifications from a TestResult . The method TestResult::addListener( ) is used to register a TestListener .

TestListener belongs to the namespace CppUnit . It is declared in TestListener.h , which also provides its default empty implementation.

Figure C-25. The class TestListener
figs/utf_ac25.gif

Declaration

 class TestListener 

Constructors/Destructors


virtual ~TestListener( )

A destructor.

Public Methods


virtual void addFailure(const TestFailure &failure)

A method called to notify the TestListener that a failure has occurred. The argument failure is a temporary object that is deleted after the call.


virtual void endTest(Test *test)

A method called to notify the TestListener that test has ended.


virtual void startTest(Test *test)

A method called to notify the TestListener that test is about to be run.

Protected/Private Methods

None.

Attributes

None.

   
TestResult

Description

The class TestResult (see Figure C-26) receives test results from Test objects. Test results can be categorized as successes, failures, and errors. Normally, when multiple Test classes are run, a single TestResult is passed to the run( ) method of each Test .

A TestResult informs its observers of test progress and results using the TestListener interface. The TestListener subclass TestResultCollector normally is used to store the results. TestResult doesn't store the results itself.

A TestResult is a SynchronizedObject . Its operations are mutex-protected and thread-safe, allowing Test and TestListener objects to run in separate threads.

TestResult belongs to the namespace CppUnit . It is declared in TestResult.h and implemented in TestResult.cpp .

Figure C-26. The class TestResult
figs/utf_ac26.gif

Declaration

 class TestResult : protected SynchronizedObject 

Constructors/Destructors


TestResult(SynchronizationObject *syncObject = 0)

A constructor. If a SynchronizationObject is not provided, a new one is created.


virtual ~TestResult( )

A destructor.


Public Methods

virtual void addError(Test *test, Exception *e)

A method that informs TestResult of a test error (an Exception not caused by a test assertion).


virtual void addFailure(Test *test, Exception *e)

A method that informs TestResult of a test failure (an Exception caused by a test assertion).


virtual void addListener(TestListener *listener)

Adds a TestListener to this TestResult .


virtual void endTest(Test *test)

A method that informs TestResult that test has ended.


virtual void removeListener(TestListener *listener)

Removes a TestListener from this TestResult .


virtual void reset( )

Sets m_stop to FALSE to prepare for a new test run.


virtual bool shouldStop( ) const

Returns the value of m_stop .


virtual void startTest(Test *test)

A method that informs TestResult that test is about to be run.


virtual void stop( )

Sets m_stop to TRUE to stop the test run.

Protected/Private Methods


void addFailure(const TestFailure &failure)

A protected method that adds a TestFailure and inform listeners.


TestResult(const TestResult &other)

A copy constructor declared private to prevent its use.


TestResult& operator=(const TestResult &other)

A copy operator declared private to prevent its use.

Attributes


typedef deque<TestListener *> TestListeners

Defines the type TestListeners as a deque of TestListener objects ( protected ).


TestListeners m_listeners

The test listeners ( protected ).


bool m_stop

If TRUE , the test run should stop ( protected ).