CppUnit - The Unit Testing Library

CppUnit project page | FAQ

Public Member Functions | List of all members
TestListener Class Reference
Tracking test execution

Listener for test progress and result. More...

`#include <TestListener.h>`

Inheritance diagram for TestListener:
[legend]

## Public Member Functions
virtual | ~TestListener ()
virtual void | startTest (Test *)
| Called when just before a TestCase is run. More...
virtual void | addFailure (const TestFailure &)
| Called when a failure occurs while running a test. More...
virtual void | endTest (Test *)
| Called just after a TestCase was run (even if a failure occured). More...
virtual void | startSuite (Test *)
| Called by a TestComposite just before running its child tests. More...
virtual void | endSuite (Test *)
| Called by a TestComposite after running its child tests. More...
virtual void | startTestRun (Test *, TestResult *)
| Called by a TestRunner before running the test. More...
virtual void | endTestRun (Test *, TestResult *)
| Called by a TestRunner after running the test. More...

## Detailed Description

Listener for test progress and result.

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:

#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;
};

And another example that track failure/success at test suite level and captures the TestPath of each suite:

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;
};

See also
  TestResult

## Constructor & Destructor Documentation

## ◆ ~TestListener()

virtual TestListener::~TestListener | ( |  | ) |
| inlinevirtual

## Member Function Documentation

## ◆ addFailure()

virtual void TestListener::addFailure | ( | const TestFailure & |  | ) |
| inlinevirtual

Called when a failure occurs while running a test.

See also
  TestFailure.

Warning
  failure is a temporary object that is destroyed after the method call. Use TestFailure::clone() to create a duplicate.

Reimplemented in TestResultCollector, TestSuccessListener, TextTestProgressListener, TextTestResult, and BriefTestProgressListener.

## ◆ endSuite()

virtual void TestListener::endSuite | ( | Test * |  | ) |
| inlinevirtual

Called by a TestComposite after running its child tests.

## ◆ endTest()

virtual void TestListener::endTest | ( | Test * |  | ) |
| inlinevirtual

Called just after a TestCase was run (even if a failure occured).

Reimplemented in BriefTestProgressListener.

## ◆ endTestRun()

virtual void TestListener::endTestRun | ( | Test * | ,
|  | TestResult * |
| ) |  |
| inlinevirtual

Called by a TestRunner after running the test.

TextTestProgressListener use this to emit a line break. You can also use this to do some global uninitialisation.

Parameters

  test | Test that was run.
  eventManager | Event manager used for the test run.

Reimplemented in TextTestProgressListener.

## ◆ startSuite()

virtual void TestListener::startSuite | ( | Test * |  | ) |
| inlinevirtual

Called by a TestComposite just before running its child tests.

## ◆ startTest()

virtual void TestListener::startTest | ( | Test * |  | ) |
| inlinevirtual

Called when just before a TestCase is run.

Reimplemented in TestResultCollector, TextTestResult, TextTestProgressListener, and BriefTestProgressListener.

## ◆ startTestRun()

virtual void TestListener::startTestRun | ( | Test * | ,
|  | TestResult * |
| ) |  |
| inlinevirtual

Called by a TestRunner before running the test.

You can use this to do some global initialisation. A listener could also use to output a 'prolog' to the test run.

Parameters

  test | Test that is going to be run.
  eventManager | Event manager used for the test run.

The documentation for this class was generated from the following file:

- TestListener.h

TestFailure
Record of a failed Test execution.
Definition: TestFailure.h:25
TestListener::addFailure
virtual void addFailure(const TestFailure &)
Called when a failure occurs while running a test.
Definition: TestListener.h:107
TestListener::endSuite
virtual void endSuite(Test *)
Called by a TestComposite after running its child tests.
Definition: TestListener.h:118
TestListener.h
TestListener::endTest
virtual void endTest(Test *)
Called just after a TestCase was run (even if a failure occured).
Definition: TestListener.h:110
Test.h
TestListener::startSuite
virtual void startSuite(Test *)
Called by a TestComposite just before running its child tests.
Definition: TestListener.h:114
TestListener::startTest
virtual void startTest(Test *)
Called when just before a TestCase is run.
Definition: TestListener.h:100

| Send comments to: CppUnit Developers