You can listen to events during a test run to perform additional work such as logging, statistics, or additional setup and teardown. You do this by deriving from TestListener:
class TestListener
{
public:
virtual void TestRunStarting() = 0;
virtual void TestRunEnded(const TestResults& results) = 0;
virtual void ContextRunStarting(const ContextBase& context) = 0;
virtual void ContextRunEnded(const ContextBase& context) = 0;
virtual void SpecRunStarting(const ContextBase& context, const std::string& specName) = 0;
virtual void SpecSucceeded(const ContextBase& context, const std::string& specName) = 0;
virtual void SpecFailed(const ContextBase& context, const std::string& specName) = 0;
};
TestRunStarting()
Called when a test run is about to begin.
TestRunEnded()
Called after a test run has been completed. The TestResults parameter contains information about the test run, and stores collections of all succeeded and failed specs.
ContextRunStarting()
Called before the specs in a context is about to be called. If you’ve added ContextAttributes to your contexts you can read these here.
Context(MyContext)
{
ContextAttribute("category", "my category")
// ...
};
void ContextRunStarting(const ContextBase& context)
{
const std::string& category = context.GetAttribute("context");
// ...
}
ContextRunEnded()
Called after all specs have been called for a context.
SpecRunStarting()
Called before a spec is about to be executed.
SpecSucceeded()
Called after a spec has been successfully executed.
SpecFailed()
Called after a spec has failed.
Registering a TestListener
The following version of main creates and registers a test listener with Igloo:
int main()
{
DefaultTestResultsOutput output;
TestRunner runner(output);
MyTestListener listener;
runner.AddListener(&listener);
runner.Run();
}
No comments