mirror of
https://github.com/catchorg/Catch2.git
synced 2025-10-19 00:25:39 +02:00
add user hooks to notify start and end of test cases
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
@@ -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;
|
||||
};
|
||||
}
|
||||
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user