added timing support to jUnit XML reporter

Implemented support for report test timings in the jUnit XML reporter.

The implementation uses the new C++11 chrono functionality, but all
this has been encapsulated in only 3 lines at the top of the
JunitReporter class, which should allow for an easy adjustment for
non-C++11-compilers...
This commit is contained in:
Kosta 2013-07-22 20:49:54 +02:00
parent 1d3dd71774
commit b80c3b043a

View File

@ -19,6 +19,12 @@ namespace Catch {
class JunitReporter : public SharedImpl<IReporter> { 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 { struct TestStats {
std::string m_element; std::string m_element;
std::string m_resultType; std::string m_resultType;
@ -30,15 +36,16 @@ namespace Catch {
TestCaseStats( const std::string& className, const std::string& name ) TestCaseStats( const std::string& className, const std::string& name )
: m_className( className ), : m_className( className ),
m_name( name ) m_name( name ),
m_startTime( time_now() ), m_endTime( m_startTime )
{} {}
double m_timeInSeconds;
std::string m_status; std::string m_status;
std::string m_className; std::string m_className;
std::string m_name; std::string m_name;
std::string m_stdOut; std::string m_stdOut;
std::string m_stdErr; std::string m_stdErr;
time_point m_startTime, m_endTime;
std::vector<TestStats> m_testStats; std::vector<TestStats> m_testStats;
std::vector<TestCaseStats> m_sections; std::vector<TestCaseStats> m_sections;
}; };
@ -50,17 +57,16 @@ namespace Catch {
m_failuresCount( 0 ), m_failuresCount( 0 ),
m_disabledCount( 0 ), m_disabledCount( 0 ),
m_errorsCount( 0 ), m_errorsCount( 0 ),
m_timeInSeconds( 0 ), m_name( name ),
m_name( name ) m_startTime( time_now() ), m_endTime( m_startTime )
{} {}
std::size_t m_testsCount; std::size_t m_testsCount;
std::size_t m_failuresCount; std::size_t m_failuresCount;
std::size_t m_disabledCount; std::size_t m_disabledCount;
std::size_t m_errorsCount; std::size_t m_errorsCount;
double m_timeInSeconds;
std::string m_name; std::string m_name;
time_point m_startTime, m_endTime;
std::vector<TestCaseStats> m_testCaseStats; std::vector<TestCaseStats> m_testCaseStats;
}; };
@ -94,6 +100,7 @@ namespace Catch {
virtual void EndGroup( const std::string&, const Totals& totals ) { virtual void EndGroup( const std::string&, const Totals& totals ) {
m_currentStats->m_testsCount = totals.assertions.total(); m_currentStats->m_testsCount = totals.assertions.total();
m_currentStats->m_endTime = time_now();
m_currentStats = &m_testSuiteStats; m_currentStats = &m_testSuiteStats;
} }
@ -163,6 +170,7 @@ namespace Catch {
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back(); TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
testCaseStats.m_stdOut = stdOut; testCaseStats.m_stdOut = stdOut;
testCaseStats.m_stdErr = stdErr; testCaseStats.m_stdErr = stdErr;
testCaseStats.m_endTime = time_now();
if( !stdOut.empty() ) if( !stdOut.empty() )
m_stdOut << stdOut << "\n"; m_stdOut << stdOut << "\n";
if( !stdErr.empty() ) if( !stdErr.empty() )
@ -174,6 +182,8 @@ namespace Catch {
} }
virtual void EndTesting( const Totals& ) { virtual void EndTesting( const Totals& ) {
m_testSuiteStats.m_endTime = time_now();
XmlWriter xml( m_config.stream() ); XmlWriter xml( m_config.stream() );
if( m_statsForSuites.size() > 0 ) if( m_statsForSuites.size() > 0 )
@ -189,7 +199,7 @@ namespace Catch {
xml.writeAttribute( "failures", it->m_failuresCount ); xml.writeAttribute( "failures", it->m_failuresCount );
xml.writeAttribute( "tests", it->m_testsCount ); xml.writeAttribute( "tests", it->m_testsCount );
xml.writeAttribute( "hostname", "tbd" ); xml.writeAttribute( "hostname", "tbd" );
xml.writeAttribute( "time", "tbd" ); xml.writeAttribute( "time", time_diff(it->m_endTime, it->m_startTime) );
xml.writeAttribute( "timestamp", "tbd" ); xml.writeAttribute( "timestamp", "tbd" );
OutputTestCases( xml, *it ); OutputTestCases( xml, *it );
@ -203,11 +213,10 @@ namespace Catch {
std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin(); std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin();
std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end(); std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end();
for(; it != itEnd; ++it ) { for(; it != itEnd; ++it ) {
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" ); XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
xml.writeAttribute( "classname", it->m_className ); xml.writeAttribute( "classname", it->m_className );
xml.writeAttribute( "name", it->m_name ); xml.writeAttribute( "name", it->m_name );
xml.writeAttribute( "time", "tbd" ); xml.writeAttribute( "time", time_diff(it->m_endTime, it->m_startTime) );
OutputTestResult( xml, *it ); OutputTestResult( xml, *it );