Application Class Reference

#include <Pt/Unit/Application.h>

Run registered tests. More...

Inherits Test.

Public Member Functions

 Application ()
 Default Constructor.
 
virtual ~Application ()
 Destructor.
 
TestfindTest (const std::string &testname)
 Find a test by name. More...
 
void attachReporter (Reporter &r)
 Add reporter for test events. More...
 
void attachReporter (Reporter &r, const std::string &testname)
 Add reporter for test events. More...
 
void run (const std::string &testName)
 Run test by name. More...
 
virtual void run ()
 Run all tests. More...
 
unsigned errors () const
 Returns the number of errors which occured during a run.
 
void registerTest (Test &test)
 Register a test. More...
 

Static Public Member Functions

static Applicationinstance ()
 Returns the instance.
 
static std::list< Test * > & tests ()
 Returns a list of all registered test TODO: find another way to query available tests. More...
 

Detailed Description

The Application class serves as the environment for running unit tests. Tests are registered at program start using Pt::Unit::RegisterTest. A Reporter can be attached to process test events such as printing results to the console or writing log files.

The simplest way to provide a main() function is to include TestMain.h in exactly one source file of the test executable. It creates an Application, attaches a BriefReporter for console output and supports the following command line arguments:

  • -h — prints a list of all registered tests.
  • -t <name> — runs only the test with the given name.
  • -f <file> — additionally writes test output to a log file.

When executed without arguments, all registered tests are run. The exit code equals the number of errors, so a successful run returns 0.

For more control over reporter setup or test execution, a custom main() function can be written instead of including TestMain.h.

#include <Pt/Unit/Application.h>
#include <Pt/Unit/Reporter.h>
int main()
{
Pt::Unit::BriefReporter reporter;
app.attachReporter(reporter);
app.run();
return app.errors() > 0 ? 1 : 0;
}

Member Function Documentation

◆ findTest()

Test* findTest ( const std::string &  testname)

Returns a pointer to the found test or 0 if not found.

◆ attachReporter() [1/2]

void attachReporter ( Reporter r)

Adds the reporter r to report test events.

◆ attachReporter() [2/2]

void attachReporter ( Reporter r,
const std::string &  testname 
)

Adds the reporter r to report test events of the test name testname.

◆ run() [1/2]

void run ( const std::string &  testName)

This method will run a previously registered test. Use the RegisterTest<T> template to register a test to the application.

Parameters
testNamename of the test to be run

◆ run() [2/2]

virtual void run ( )
virtual

This method will run all tests that have been registered previously. Use the RegisterTest<T> template to register a test to the application.

Implements Test.

◆ registerTest()

void registerTest ( Test test)

Registers the test test to the application. The application will not own the test and the caller has to make sure it exists as long as the application object. Tests can be deregistered by calling deregisterTest.

◆ tests()

static std::list<Test*>& tests ( )
static
Returns
Reference to the registered tests.
unsigned errors() const
Returns the number of errors which occured during a run.
Definition: Application.h:132
void run(const std::string &testName)
Run test by name.
Run registered tests.
Definition: Application.h:81
void attachReporter(Reporter &r)
Add reporter for test events.