add user hooks to notify start and end of test cases

This commit is contained in:
François Moisan
2014-03-25 14:51:08 -04:00
parent af8cd98f20
commit 36bed7af00
4 changed files with 97 additions and 4 deletions

View File

@@ -153,6 +153,15 @@ namespace Catch {
return m_filterSets;
}
std::vector<ITestCaseHook*> const& userTestCaseHooks() const {
return m_userHooks;
}
void addUserHook(ITestCaseHook* hook)
{
m_userHooks.push_back(hook);
}
bool showHelp() const { return m_data.showHelp; }
// IConfig interface
@@ -163,13 +172,13 @@ namespace Catch {
virtual bool warnAboutMissingAssertions() const { return m_data.warnings & WarnAbout::NoAssertions; }
virtual ShowDurations::OrNot showDurations() const { return m_data.showDurations; }
private:
ConfigData m_data;
Stream m_stream;
mutable std::ostream m_os;
std::vector<TestCaseFilters> m_filterSets;
std::vector<ITestCaseHook*> m_userHooks;
};

View File

@@ -10,11 +10,14 @@
#include <iostream>
#include <string>
#include <vector>
#include "catch_ptr.hpp"
namespace Catch {
struct TestCaseInfo;
struct Verbosity { enum Level {
NoOutput = 0,
Quiet,
@@ -32,6 +35,14 @@ namespace Catch {
Never
}; };
struct ITestCaseHook
{
virtual ~ITestCaseHook() {}
virtual void sectionStarting(TestCaseInfo const & testCaseInfo) = 0;
virtual void sectionEnding(TestCaseInfo const & testCaseInfo) = 0;
};
struct IConfig : IShared {
virtual ~IConfig();
@@ -44,6 +55,8 @@ namespace Catch {
virtual bool warnAboutMissingAssertions() const = 0;
virtual int abortAfter() const = 0;
virtual ShowDurations::OrNot showDurations() const = 0;
virtual std::vector< ITestCaseHook* > const & userTestCaseHooks() const = 0;
};
}

View File

@@ -256,6 +256,26 @@ namespace Catch {
return action;
}
void runTestCaseStartingHook(TestCaseInfo const& testCaseInfo)
{
std::vector<ITestCaseHook*> const & validations = m_config->userTestCaseHooks();
for(std::vector<ITestCaseHook*>::const_iterator it = validations.begin(), end = validations.end(); it != end; ++it)
{
(*it)->sectionStarting(testCaseInfo);
}
}
void runTestCaseEndingHook(TestCaseInfo const& testCaseInfo)
{
std::vector<ITestCaseHook*> const & validations = m_config->userTestCaseHooks();
for(std::vector<ITestCaseHook*>::const_reverse_iterator it = validations.rbegin(), end = validations.rend(); it != end; ++it)
{
(*it)->sectionEnding(testCaseInfo);
}
}
void runCurrentTest( std::string& redirectedCout, std::string& redirectedCerr ) {
TestCaseInfo const& testCaseInfo = m_activeTestCase->getTestCaseInfo();
SectionInfo testCaseSection( testCaseInfo.name, testCaseInfo.description, testCaseInfo.lineInfo );
@@ -266,6 +286,8 @@ namespace Catch {
m_lastAssertionInfo = AssertionInfo( "TEST_CASE", testCaseInfo.lineInfo, "", ResultDisposition::Normal );
TestCaseTracker::Guard guard( *m_testCaseTracker );
runTestCaseStartingHook(testCaseInfo);
Timer timer;
timer.start();
if( m_reporter->getPreferences().shouldRedirectStdOut ) {
@@ -277,6 +299,8 @@ namespace Catch {
m_activeTestCase->invoke();
}
duration = timer.getElapsedSeconds();
runTestCaseEndingHook(testCaseInfo);
}
catch( TestFailureException& ) {
// This just means the test was aborted due to failure