Add new reporter event called for each test case enter/exit

This means that e.g. for `TEST_CASE` with two sibling `SECTION`s
the event will fire twice, because the `TEST_CASE` will be entered
twice.

Closes #2107 (the event mentioned there already exists, but this
is its counterpart that we also want to provide to users)
This commit is contained in:
Martin Hořeňovský
2021-09-12 00:00:47 +02:00
parent 4dcf8382c7
commit 10fb93cce8
11 changed files with 207 additions and 3 deletions

View File

@@ -177,7 +177,10 @@ namespace Catch {
virtual void testRunStarting( TestRunInfo const& testRunInfo ) = 0;
//! Called _once_ for each TEST_CASE, no matter how many times it is entered
virtual void testCaseStarting( TestCaseInfo const& testInfo ) = 0;
//! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
virtual void testCasePartialStarting( TestCaseInfo const& testInfo, uint64_t partNumber ) = 0;
virtual void sectionStarting( SectionInfo const& sectionInfo ) = 0;
virtual void benchmarkPreparing( StringRef ) {}
@@ -191,6 +194,9 @@ namespace Catch {
virtual bool assertionEnded( AssertionStats const& assertionStats ) = 0;
virtual void sectionEnded( SectionStats const& sectionStats ) = 0;
//! Called _every time_ a TEST_CASE is entered, including repeats (due to sections)
virtual void testCasePartialEnded(TestCaseStats const& testCaseStats, uint64_t partNumber ) = 0;
//! Called _once_ for each TEST_CASE, no matter how many times it is entered
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) = 0;
virtual void testRunEnded( TestRunStats const& testRunStats ) = 0;

View File

@@ -176,7 +176,7 @@ namespace Catch {
}
Totals RunContext::runTest(TestCaseHandle const& testCase) {
Totals prevTotals = m_totals;
const Totals prevTotals = m_totals;
std::string redirectedCout;
std::string redirectedCerr;
@@ -191,10 +191,25 @@ namespace Catch {
ITracker& rootTracker = m_trackerContext.startRun();
assert(rootTracker.isSectionTracker());
static_cast<SectionTracker&>(rootTracker).addInitialFilters(m_config->getSectionsToRun());
uint64_t testRuns = 0;
do {
m_trackerContext.startCycle();
m_testCaseTracker = &SectionTracker::acquire(m_trackerContext, TestCaseTracking::NameAndLocation(testInfo.name, testInfo.lineInfo));
runCurrentTest(redirectedCout, redirectedCerr);
m_reporter->testCasePartialStarting(testInfo, testRuns);
const auto beforeRunTotals = m_totals;
std::string oneRunCout, oneRunCerr;
runCurrentTest(oneRunCout, oneRunCerr);
redirectedCout += oneRunCout;
redirectedCerr += oneRunCerr;
const auto singleRunTotals = m_totals.delta(beforeRunTotals);
auto statsForOneRun = TestCaseStats(testInfo, singleRunTotals, redirectedCout, oneRunCerr, aborting());
m_reporter->testCasePartialEnded(statsForOneRun, testRuns);
++testRuns;
} while (!m_testCaseTracker->isSuccessfullyCompleted() && !aborting());
Totals deltaTotals = m_totals.delta(prevTotals);

View File

@@ -232,8 +232,10 @@ namespace Catch {
void EventListenerBase::noMatchingTestCases( std::string const& ) {}
void EventListenerBase::testRunStarting( TestRunInfo const& ) {}
void EventListenerBase::testCaseStarting( TestCaseInfo const& ) {}
void EventListenerBase::testCasePartialStarting(TestCaseInfo const&, uint64_t) {}
void EventListenerBase::sectionStarting( SectionInfo const& ) {}
void EventListenerBase::sectionEnded( SectionStats const& ) {}
void EventListenerBase::testCasePartialEnded(TestCaseStats const&, uint64_t) {}
void EventListenerBase::testCaseEnded( TestCaseStats const& ) {}
void EventListenerBase::testRunEnded( TestRunStats const& ) {}
void EventListenerBase::skipTest( TestCaseInfo const& ) {}

View File

@@ -52,13 +52,14 @@ namespace Catch {
void testRunStarting( TestRunInfo const& ) override {}
void testCaseStarting( TestCaseInfo const& ) override {}
void testCasePartialStarting( TestCaseInfo const&, uint64_t ) override {}
void sectionStarting( SectionInfo const& sectionInfo ) override;
void assertionStarting( AssertionInfo const& ) override {}
bool assertionEnded( AssertionStats const& assertionStats ) override;
void sectionEnded( SectionStats const& sectionStats ) override;
void testCasePartialEnded( TestCaseStats const&, uint64_t ) override {}
void testCaseEnded( TestCaseStats const& testCaseStats ) override;
void testRunEnded( TestRunStats const& testRunStats ) override;
//! Customization point: called after last test finishes (testRunEnded has been handled)

View File

@@ -35,11 +35,16 @@ namespace Catch {
void noMatchingTestCases( std::string const& spec ) override;
void testRunStarting( TestRunInfo const& testRunInfo ) override;
void testCaseStarting( TestCaseInfo const& testInfo ) override;
void testCasePartialStarting( TestCaseInfo const& testInfo,
uint64_t partNumber ) override;
void sectionStarting( SectionInfo const& sectionInfo ) override;
void sectionEnded( SectionStats const& sectionStats ) override;
void testCasePartialEnded( TestCaseStats const& testCaseStats,
uint64_t partNumber ) override;
void testCaseEnded( TestCaseStats const& testCaseStats ) override;
void testRunEnded( TestRunStats const& testRunStats ) override;
void skipTest( TestCaseInfo const& testInfo ) override;
};
} // end namespace Catch

View File

@@ -76,6 +76,15 @@ namespace Catch {
m_reporter->testCaseStarting( testInfo );
}
void
ListeningReporter::testCasePartialStarting( TestCaseInfo const& testInfo,
uint64_t partNumber ) {
for ( auto& listener : m_listeners ) {
listener->testCasePartialStarting( testInfo, partNumber );
}
m_reporter->testCasePartialStarting( testInfo, partNumber );
}
void ListeningReporter::sectionStarting( SectionInfo const& sectionInfo ) {
for ( auto& listener : m_listeners ) {
listener->sectionStarting( sectionInfo );
@@ -105,6 +114,14 @@ namespace Catch {
m_reporter->sectionEnded( sectionStats );
}
void ListeningReporter::testCasePartialEnded( TestCaseStats const& testInfo,
uint64_t partNumber ) {
for ( auto& listener : m_listeners ) {
listener->testCasePartialEnded( testInfo, partNumber );
}
m_reporter->testCasePartialEnded( testInfo, partNumber );
}
void ListeningReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
for ( auto& listener : m_listeners ) {
listener->testCaseEnded( testCaseStats );

View File

@@ -41,12 +41,14 @@ namespace Catch {
void testRunStarting( TestRunInfo const& testRunInfo ) override;
void testCaseStarting( TestCaseInfo const& testInfo ) override;
void testCasePartialStarting(TestCaseInfo const& testInfo, uint64_t partNumber) override;
void sectionStarting( SectionInfo const& sectionInfo ) override;
void assertionStarting( AssertionInfo const& assertionInfo ) override;
// The return value indicates if the messages buffer should be cleared:
bool assertionEnded( AssertionStats const& assertionStats ) override;
void sectionEnded( SectionStats const& sectionStats ) override;
void testCasePartialEnded(TestCaseStats const& testInfo, uint64_t partNumber) override;
void testCaseEnded( TestCaseStats const& testCaseStats ) override;
void testRunEnded( TestRunStats const& testRunStats ) override;

View File

@@ -51,6 +51,7 @@ namespace Catch {
void testCaseStarting(TestCaseInfo const& _testInfo) override {
currentTestCaseInfo = &_testInfo;
}
void testCasePartialStarting( TestCaseInfo const&, uint64_t ) override {}
void sectionStarting(SectionInfo const& _sectionInfo) override {
m_sectionStack.push_back(_sectionInfo);
}
@@ -61,6 +62,7 @@ namespace Catch {
void sectionEnded(SectionStats const& /* _sectionStats */) override {
m_sectionStack.pop_back();
}
void testCasePartialEnded( TestCaseStats const&, uint64_t ) override {}
void testCaseEnded(TestCaseStats const& /* _testCaseStats */) override {
currentTestCaseInfo = nullptr;
}