mirror of
https://github.com/catchorg/Catch2.git
synced 2025-09-19 03:15:40 +02:00
JunitReporter reimplemented using the new IStreamingReporter interface
* created new AccumulatingReporterBase class for accumulating test results hierarchically and store them for a single processResults() call after all tests have been executed; sections are currently not handled, since their usage are optional and/or could be nested arbitrarily, which would result in overly complex code, IMHO * JunitReporter reimplemented on top of this new AccumulatingReporterBase class * added support for tracking time spend in each test case, each test group, and overall tests to the base "*Stats" classes; this enables each reporter (derived from IStreamingReporter interface) to report the timings; for now only the JunitReporter takes advantage of that.
This commit is contained in:
@@ -17,243 +17,152 @@
|
||||
|
||||
namespace Catch {
|
||||
|
||||
class JunitReporter : public SharedImpl<IReporter> {
|
||||
|
||||
// C++11 specific start => needs to be adjusted for pre-C++11-compilers
|
||||
typedef decltype(std::chrono::high_resolution_clock::now()) time_point;
|
||||
static time_point time_now() { return std::chrono::high_resolution_clock::now(); }
|
||||
static double time_diff(const time_point& end, const time_point& start) { return (1e-6 * std::chrono::duration_cast<std::chrono::microseconds>(end - start).count()); }
|
||||
// C++11 specific end
|
||||
|
||||
struct TestStats {
|
||||
std::string m_element;
|
||||
std::string m_resultType;
|
||||
std::string m_message;
|
||||
std::string m_content;
|
||||
};
|
||||
|
||||
struct TestCaseStats {
|
||||
|
||||
TestCaseStats( const std::string& className, const std::string& name )
|
||||
: m_className( className ),
|
||||
m_name( name ),
|
||||
m_startTime( time_now() ), m_endTime( m_startTime )
|
||||
{}
|
||||
|
||||
std::string m_status;
|
||||
std::string m_className;
|
||||
std::string m_name;
|
||||
std::string m_stdOut;
|
||||
std::string m_stdErr;
|
||||
time_point m_startTime, m_endTime;
|
||||
std::vector<TestStats> m_testStats;
|
||||
std::vector<TestCaseStats> m_sections;
|
||||
};
|
||||
|
||||
struct Stats {
|
||||
|
||||
Stats( const std::string& name = std::string() )
|
||||
: m_testsCount( 0 ),
|
||||
m_failuresCount( 0 ),
|
||||
m_disabledCount( 0 ),
|
||||
m_errorsCount( 0 ),
|
||||
m_name( name ),
|
||||
m_startTime( time_now() ), m_endTime( m_startTime )
|
||||
{}
|
||||
|
||||
std::size_t m_testsCount;
|
||||
std::size_t m_failuresCount;
|
||||
std::size_t m_disabledCount;
|
||||
std::size_t m_errorsCount;
|
||||
std::string m_name;
|
||||
time_point m_startTime, m_endTime;
|
||||
std::vector<TestCaseStats> m_testCaseStats;
|
||||
};
|
||||
|
||||
class JunitReporter : public SharedImpl<AccumulatingReporterBase> {
|
||||
public:
|
||||
JunitReporter( ReporterConfig const& config )
|
||||
: m_config( config ),
|
||||
m_testSuiteStats( "AllTests" ),
|
||||
m_currentStats( &m_testSuiteStats )
|
||||
{}
|
||||
JunitReporter( ReporterConfig const& config ) : m_config( config ) {}
|
||||
virtual ~JunitReporter();
|
||||
|
||||
static std::string getDescription() {
|
||||
return "Reports test results in an XML format that looks like Ant's junitreport target";
|
||||
}
|
||||
|
||||
private: // IReporter
|
||||
|
||||
virtual bool shouldRedirectStdout() const {
|
||||
return true;
|
||||
virtual ReporterPreferences getPreferences() const {
|
||||
ReporterPreferences prefs;
|
||||
prefs.shouldRedirectStdOut = true;
|
||||
return prefs;
|
||||
}
|
||||
|
||||
virtual void StartTesting(){}
|
||||
|
||||
virtual void StartGroup( const std::string& groupName ) {
|
||||
if( groupName.empty() )
|
||||
m_statsForSuites.push_back( Stats( m_config.fullConfig()->name() ) );
|
||||
else
|
||||
m_statsForSuites.push_back( Stats( groupName ) );
|
||||
m_currentStats = &m_statsForSuites.back();
|
||||
virtual void noMatchingTestCases( std::string const& spec ) {
|
||||
(void)spec;
|
||||
}
|
||||
|
||||
virtual void EndGroup( const std::string&, const Totals& totals ) {
|
||||
m_currentStats->m_testsCount = totals.assertions.total();
|
||||
m_currentStats->m_endTime = time_now();
|
||||
m_currentStats = &m_testSuiteStats;
|
||||
}
|
||||
|
||||
virtual void StartSection( const std::string&, const std::string& ){}
|
||||
|
||||
virtual void NoAssertionsInSection( const std::string& ) {}
|
||||
virtual void NoAssertionsInTestCase( const std::string& ) {}
|
||||
|
||||
virtual void EndSection( const std::string&, const Counts& ) {}
|
||||
|
||||
virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) {
|
||||
m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.className, testInfo.name ) );
|
||||
m_currentTestCaseStats.push_back( &m_currentStats->m_testCaseStats.back() );
|
||||
}
|
||||
|
||||
virtual void Result( const Catch::AssertionResult& assertionResult ) {
|
||||
if( assertionResult.getResultType() != ResultWas::Ok || m_config.fullConfig()->includeSuccessfulResults() ) {
|
||||
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
|
||||
TestStats stats;
|
||||
std::ostringstream oss;
|
||||
if( !assertionResult.getMessage().empty() )
|
||||
oss << assertionResult.getMessage() << " at ";
|
||||
oss << assertionResult.getSourceInfo();
|
||||
stats.m_content = oss.str();
|
||||
stats.m_message = assertionResult.getExpandedExpression();
|
||||
stats.m_resultType = assertionResult.getTestMacroName();
|
||||
|
||||
switch( assertionResult.getResultType() ) {
|
||||
case ResultWas::ThrewException:
|
||||
stats.m_element = "error";
|
||||
m_currentStats->m_errorsCount++;
|
||||
break;
|
||||
case ResultWas::Info:
|
||||
stats.m_element = "info"; // !TBD ?
|
||||
break;
|
||||
case ResultWas::Warning:
|
||||
stats.m_element = "warning"; // !TBD ?
|
||||
break;
|
||||
case ResultWas::ExplicitFailure:
|
||||
stats.m_element = "failure";
|
||||
m_currentStats->m_failuresCount++;
|
||||
break;
|
||||
case ResultWas::ExpressionFailed:
|
||||
stats.m_element = "failure";
|
||||
m_currentStats->m_failuresCount++;
|
||||
break;
|
||||
case ResultWas::Ok:
|
||||
stats.m_element = "success";
|
||||
break;
|
||||
case ResultWas::DidntThrowException:
|
||||
stats.m_element = "failure";
|
||||
m_currentStats->m_failuresCount++;
|
||||
break;
|
||||
case ResultWas::Unknown:
|
||||
case ResultWas::FailureBit:
|
||||
case ResultWas::Exception:
|
||||
stats.m_element = "* internal error *";
|
||||
break;
|
||||
}
|
||||
testCaseStats.m_testStats.push_back( stats );
|
||||
}
|
||||
}
|
||||
|
||||
virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string& stdOut, const std::string& stdErr ) {
|
||||
m_currentTestCaseStats.pop_back();
|
||||
assert( m_currentTestCaseStats.empty() );
|
||||
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
|
||||
testCaseStats.m_stdOut = stdOut;
|
||||
testCaseStats.m_stdErr = stdErr;
|
||||
testCaseStats.m_endTime = time_now();
|
||||
if( !stdOut.empty() )
|
||||
m_stdOut << stdOut << "\n";
|
||||
if( !stdErr.empty() )
|
||||
m_stdErr << stdErr << "\n";
|
||||
}
|
||||
|
||||
virtual void Aborted() {
|
||||
// !TBD
|
||||
}
|
||||
|
||||
virtual void EndTesting( const Totals& ) {
|
||||
m_testSuiteStats.m_endTime = time_now();
|
||||
|
||||
virtual void processResults( AccumTestRunStats const& stats ) {
|
||||
XmlWriter xml( m_config.stream() );
|
||||
|
||||
if( m_statsForSuites.size() > 0 )
|
||||
xml.startElement( "testsuites" );
|
||||
|
||||
std::vector<Stats>::const_iterator it = m_statsForSuites.begin();
|
||||
std::vector<Stats>::const_iterator itEnd = m_statsForSuites.end();
|
||||
|
||||
for(; it != itEnd; ++it ) {
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
|
||||
xml.writeAttribute( "name", it->m_name );
|
||||
xml.writeAttribute( "errors", it->m_errorsCount );
|
||||
xml.writeAttribute( "failures", it->m_failuresCount );
|
||||
xml.writeAttribute( "tests", it->m_testsCount );
|
||||
xml.writeAttribute( "hostname", "tbd" );
|
||||
xml.writeAttribute( "time", time_diff(it->m_endTime, it->m_startTime) );
|
||||
xml.writeAttribute( "timestamp", "tbd" );
|
||||
|
||||
OutputTestCases( xml, *it );
|
||||
}
|
||||
|
||||
xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ), false );
|
||||
xml.scopedElement( "system-err" ).writeText( trim( m_stdErr.str() ), false );
|
||||
OutputTestSuites( xml, stats );
|
||||
}
|
||||
|
||||
void OutputTestCases( XmlWriter& xml, const Stats& stats ) {
|
||||
std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin();
|
||||
std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end();
|
||||
for(; it != itEnd; ++it ) {
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
|
||||
xml.writeAttribute( "classname", it->m_className );
|
||||
xml.writeAttribute( "name", it->m_name );
|
||||
xml.writeAttribute( "time", time_diff(it->m_endTime, it->m_startTime) );
|
||||
private:
|
||||
static void OutputTestSuites( XmlWriter& xml, AccumTestRunStats const& stats ) {
|
||||
xml.startElement( "testsuites" );
|
||||
|
||||
xml.writeAttribute( "time", stats.testRun.timeSecs );
|
||||
|
||||
std::vector<AccumTestGroupStats>::const_iterator it = stats.testGroups.begin();
|
||||
std::vector<AccumTestGroupStats>::const_iterator itEnd = stats.testGroups.end();
|
||||
|
||||
std::ostringstream stdErr, stdOut;
|
||||
for( ; it != itEnd; ++it ) {
|
||||
OutputTestSuite( xml, *it);
|
||||
CollectErrAndOutMessages( *it, stdErr, stdOut );
|
||||
}
|
||||
|
||||
OutputTextIfNotEmpty( xml, "system-out", stdOut.str() );
|
||||
OutputTextIfNotEmpty( xml, "system-err", stdErr.str() );
|
||||
}
|
||||
|
||||
static void OutputTestSuite( XmlWriter& xml, AccumTestGroupStats const& stats ) {
|
||||
size_t errors = 0, failures = 0;
|
||||
CountErrorAndFailures(stats, errors, failures);
|
||||
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
|
||||
xml.writeAttribute( "name", stats.testGroup.groupInfo.name );
|
||||
xml.writeAttribute( "errors", errors );
|
||||
xml.writeAttribute( "failures", failures );
|
||||
xml.writeAttribute( "tests", stats.testCases.size() );
|
||||
xml.writeAttribute( "hostname", "tbd" );
|
||||
xml.writeAttribute( "time", stats.testGroup.timeSecs );
|
||||
xml.writeAttribute( "timestamp", "tbd" );
|
||||
|
||||
std::vector<AccumTestCaseStats>::const_iterator it2 = stats.testCases.begin();
|
||||
std::vector<AccumTestCaseStats>::const_iterator it2End = stats.testCases.end();
|
||||
for(; it2 != it2End; ++it2 ) {
|
||||
OutputTestCase( xml, *it2 );
|
||||
}
|
||||
}
|
||||
|
||||
static void OutputTestCase( XmlWriter& xml, AccumTestCaseStats const& stats ) {
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
|
||||
xml.writeAttribute( "classname", stats.testCase.testInfo.className );
|
||||
xml.writeAttribute( "name", stats.testCase.testInfo.name );
|
||||
xml.writeAttribute( "time", stats.testCase.timeSecs );
|
||||
|
||||
std::vector<AssertionStats>::const_iterator it = stats.tests.begin();
|
||||
std::vector<AssertionStats>::const_iterator itEnd = stats.tests.end();
|
||||
for( ; it != itEnd; ++it ) {
|
||||
OutputTestResult( xml, *it );
|
||||
}
|
||||
|
||||
std::string stdOut = trim( it->m_stdOut );
|
||||
if( !stdOut.empty() )
|
||||
xml.scopedElement( "system-out" ).writeText( stdOut, false );
|
||||
std::string stdErr = trim( it->m_stdErr );
|
||||
if( !stdErr.empty() )
|
||||
xml.scopedElement( "system-err" ).writeText( stdErr, false );
|
||||
OutputTextIfNotEmpty( xml, "system-out", stats.testCase.stdOut );
|
||||
OutputTextIfNotEmpty( xml, "system-err", stats.testCase.stdErr );
|
||||
}
|
||||
|
||||
static std::string GetResultTag( AssertionStats const& test ) {
|
||||
switch(test.assertionResult.getResultType()) {
|
||||
case ResultWas::Ok: return "success";
|
||||
case ResultWas::ThrewException: return "error";
|
||||
case ResultWas::Info: return "info";
|
||||
case ResultWas::Warning: return "warning";
|
||||
case ResultWas::ExplicitFailure:
|
||||
case ResultWas::ExpressionFailed:
|
||||
case ResultWas::DidntThrowException: return "failure";
|
||||
case ResultWas::Unknown:
|
||||
case ResultWas::FailureBit:
|
||||
case ResultWas::Exception:
|
||||
default: return "* internal error *";
|
||||
}
|
||||
}
|
||||
|
||||
static void OutputTestResult( XmlWriter& xml, AssertionStats const& test ) {
|
||||
std::string tag = GetResultTag(test);
|
||||
if( tag != "success" ) {
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( tag );
|
||||
|
||||
void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats ) {
|
||||
std::vector<TestStats>::const_iterator it = stats.m_testStats.begin();
|
||||
std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end();
|
||||
for(; it != itEnd; ++it ) {
|
||||
if( it->m_element != "success" ) {
|
||||
XmlWriter::ScopedElement e = xml.scopedElement( it->m_element );
|
||||
xml.writeAttribute( "message", test.assertionResult.getExpandedExpression() );
|
||||
xml.writeAttribute( "type", test.assertionResult.getTestMacroName() );
|
||||
|
||||
xml.writeAttribute( "message", it->m_message );
|
||||
xml.writeAttribute( "type", it->m_resultType );
|
||||
if( !it->m_content.empty() )
|
||||
xml.writeText( it->m_content );
|
||||
std::ostringstream oss;
|
||||
if( !test.assertionResult.getMessage().empty() ) {
|
||||
oss << test.assertionResult.getMessage() << " at ";
|
||||
}
|
||||
oss << test.assertionResult.getSourceInfo();
|
||||
xml.writeText( oss.str() );
|
||||
}
|
||||
}
|
||||
|
||||
static void OutputTextIfNotEmpty( XmlWriter& xml, std::string const& elementName, std::string const& text ) {
|
||||
std::string trimmed = trim( text );
|
||||
if( !trimmed.empty() ) {
|
||||
xml.scopedElement( elementName ).writeText( trimmed, false );
|
||||
}
|
||||
}
|
||||
|
||||
static void CountErrorAndFailures(AccumTestGroupStats const& stats, size_t& outErrors, size_t& outFailures) {
|
||||
std::vector<AccumTestCaseStats>::const_iterator it = stats.testCases.begin();
|
||||
std::vector<AccumTestCaseStats>::const_iterator itEnd = stats.testCases.end();
|
||||
for( ; it != itEnd; ++it ) {
|
||||
std::vector<AssertionStats>::const_iterator it2 = it->tests.begin();
|
||||
std::vector<AssertionStats>::const_iterator it2End = it->tests.end();
|
||||
for( ; it2 != it2End; ++it2 ) {
|
||||
std::string tag = GetResultTag(*it2);
|
||||
if( tag == "error" ) { ++outErrors; }
|
||||
if( tag == "failure" ) { ++outFailures; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void CollectErrAndOutMessages(AccumTestGroupStats const& stats, std::ostream& outErr, std::ostream& outOut) {
|
||||
std::vector<AccumTestCaseStats>::const_iterator it = stats.testCases.begin();
|
||||
std::vector<AccumTestCaseStats>::const_iterator itEnd = stats.testCases.end();
|
||||
for( ; it != itEnd; ++it ) {
|
||||
std::string err = trim( it->testCase.stdErr );
|
||||
if( !err.empty() ) { outErr << err << std::endl; }
|
||||
std::string out = trim( it->testCase.stdOut );
|
||||
if( !out.empty() ) { outOut << out << std::endl; }
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
ReporterConfig m_config;
|
||||
|
||||
Stats m_testSuiteStats;
|
||||
Stats* m_currentStats;
|
||||
std::vector<Stats> m_statsForSuites;
|
||||
std::vector<const TestCaseStats*> m_currentTestCaseStats;
|
||||
std::ostringstream m_stdOut;
|
||||
std::ostringstream m_stdErr;
|
||||
};
|
||||
|
||||
} // end namespace Catch
|
||||
|
Reference in New Issue
Block a user