2010-12-10 03:01:42 -05:00
|
|
|
/*
|
|
|
|
* Created by Phil on 26/11/2010.
|
|
|
|
* Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
|
|
|
|
*
|
|
|
|
* Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
* file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
*/
|
|
|
|
#ifndef TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
|
|
|
|
|
2011-06-02 03:49:47 -04:00
|
|
|
#include "../internal/catch_capture.hpp"
|
|
|
|
#include "../internal/catch_interfaces_reporter.h"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_xmlwriter.hpp"
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class JunitReporter : public SharedImpl<IReporter> {
|
|
|
|
|
|
|
|
struct TestStats {
|
2011-01-31 05:10:20 -05:00
|
|
|
std::string m_element;
|
|
|
|
std::string m_resultType;
|
|
|
|
std::string m_message;
|
|
|
|
std::string m_content;
|
2010-12-10 03:01:42 -05:00
|
|
|
};
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
struct TestCaseStats {
|
|
|
|
|
2012-11-04 16:11:59 -05:00
|
|
|
TestCaseStats( const std::string& className, const std::string& name )
|
|
|
|
: m_className( className ),
|
|
|
|
m_name( name )
|
|
|
|
{}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2011-01-31 05:10:20 -05:00
|
|
|
double m_timeInSeconds;
|
|
|
|
std::string m_status;
|
|
|
|
std::string m_className;
|
|
|
|
std::string m_name;
|
2012-11-21 13:04:10 -05:00
|
|
|
std::string m_stdOut;
|
|
|
|
std::string m_stdErr;
|
2011-01-31 05:10:20 -05:00
|
|
|
std::vector<TestStats> m_testStats;
|
2012-11-22 14:17:20 -05:00
|
|
|
std::vector<TestCaseStats> m_sections;
|
2010-12-10 03:01:42 -05:00
|
|
|
};
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
struct Stats {
|
|
|
|
|
2010-12-10 03:01:42 -05:00
|
|
|
Stats( const std::string& name = std::string() )
|
2011-01-31 05:10:20 -05:00
|
|
|
: m_testsCount( 0 ),
|
|
|
|
m_failuresCount( 0 ),
|
|
|
|
m_disabledCount( 0 ),
|
|
|
|
m_errorsCount( 0 ),
|
|
|
|
m_timeInSeconds( 0 ),
|
|
|
|
m_name( name )
|
2012-05-16 09:53:59 -04:00
|
|
|
{}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2011-01-31 05:10:20 -05:00
|
|
|
std::size_t m_testsCount;
|
|
|
|
std::size_t m_failuresCount;
|
|
|
|
std::size_t m_disabledCount;
|
|
|
|
std::size_t m_errorsCount;
|
|
|
|
double m_timeInSeconds;
|
|
|
|
std::string m_name;
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2011-01-31 05:10:20 -05:00
|
|
|
std::vector<TestCaseStats> m_testCaseStats;
|
2010-12-10 03:01:42 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2012-07-20 14:07:42 -04:00
|
|
|
JunitReporter( const ReporterConfig& config )
|
2010-12-10 03:01:42 -05:00
|
|
|
: m_config( config ),
|
|
|
|
m_testSuiteStats( "AllTests" ),
|
|
|
|
m_currentStats( &m_testSuiteStats )
|
2012-08-13 02:46:10 -04:00
|
|
|
{}
|
|
|
|
virtual ~JunitReporter();
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
static std::string getDescription() {
|
2010-12-10 03:01:42 -05:00
|
|
|
return "Reports test results in an XML format that looks like Ant's junitreport target";
|
|
|
|
}
|
|
|
|
|
2010-12-31 17:07:47 -05:00
|
|
|
private: // IReporter
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual bool shouldRedirectStdout() const {
|
2012-02-17 04:28:21 -05:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void StartTesting(){}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-11-16 03:47:03 -05:00
|
|
|
virtual void StartGroup( const std::string& groupName ) {
|
|
|
|
if( groupName.empty() )
|
2012-11-19 14:59:10 -05:00
|
|
|
m_statsForSuites.push_back( Stats( m_config.name ) );
|
2012-11-16 03:47:03 -05:00
|
|
|
else
|
|
|
|
m_statsForSuites.push_back( Stats( groupName ) );
|
2010-12-10 03:18:06 -05:00
|
|
|
m_currentStats = &m_statsForSuites.back();
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void EndGroup( const std::string&, const Totals& totals ) {
|
2012-02-24 03:59:35 -05:00
|
|
|
m_currentStats->m_testsCount = totals.assertions.total();
|
2010-12-10 03:01:42 -05:00
|
|
|
m_currentStats = &m_testSuiteStats;
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void StartSection( const std::string&, const std::string& ){}
|
2010-12-28 09:42:46 -05:00
|
|
|
|
2012-08-31 03:10:36 -04:00
|
|
|
virtual void NoAssertionsInSection( const std::string& ) {}
|
|
|
|
virtual void NoAssertionsInTestCase( const std::string& ) {}
|
|
|
|
|
|
|
|
virtual void EndSection( const std::string&, const Counts& ) {}
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2012-11-22 14:17:20 -05:00
|
|
|
virtual void StartTestCase( const Catch::TestCase& testInfo ) {
|
2012-11-04 16:11:59 -05:00
|
|
|
m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getClassName(), testInfo.getName() ) );
|
2012-11-22 14:17:20 -05:00
|
|
|
m_currentTestCaseStats.push_back( &m_currentStats->m_testCaseStats.back() );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
|
2012-10-16 03:31:05 -04:00
|
|
|
virtual void Result( const Catch::AssertionResult& assertionResult ) {
|
|
|
|
if( assertionResult.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) {
|
2011-01-31 05:10:20 -05:00
|
|
|
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
|
2010-12-10 03:01:42 -05:00
|
|
|
TestStats stats;
|
|
|
|
std::ostringstream oss;
|
2012-10-16 03:31:05 -04:00
|
|
|
if( !assertionResult.getMessage().empty() )
|
|
|
|
oss << assertionResult.getMessage() << " at ";
|
2012-10-24 16:59:47 -04:00
|
|
|
oss << assertionResult.getSourceInfo();
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_content = oss.str();
|
2012-10-16 03:31:05 -04:00
|
|
|
stats.m_message = assertionResult.getExpandedExpression();
|
|
|
|
stats.m_resultType = assertionResult.getTestMacroName();
|
2012-05-16 09:53:59 -04:00
|
|
|
|
2012-10-16 03:31:05 -04:00
|
|
|
switch( assertionResult.getResultType() ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
case ResultWas::ThrewException:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "error";
|
|
|
|
m_currentStats->m_errorsCount++;
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "info"; // !TBD ?
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "warning"; // !TBD ?
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "failure";
|
|
|
|
m_currentStats->m_failuresCount++;
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::ExpressionFailed:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "failure";
|
|
|
|
m_currentStats->m_failuresCount++;
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
case ResultWas::Ok:
|
2011-01-31 05:10:20 -05:00
|
|
|
stats.m_element = "success";
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
2011-09-29 03:58:40 -04:00
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
case ResultWas::DidntThrowException:
|
2010-12-10 03:01:42 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-05-16 09:53:59 -04:00
|
|
|
testCaseStats.m_testStats.push_back( stats );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-22 14:17:20 -05:00
|
|
|
virtual void EndTestCase( const Catch::TestCase&, const Totals&, const std::string& stdOut, const std::string& stdErr ) {
|
|
|
|
m_currentTestCaseStats.pop_back();
|
|
|
|
assert( m_currentTestCaseStats.empty() );
|
2012-11-21 13:04:10 -05:00
|
|
|
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
|
|
|
|
testCaseStats.m_stdOut = stdOut;
|
|
|
|
testCaseStats.m_stdErr = stdErr;
|
2010-12-10 03:01:42 -05:00
|
|
|
if( !stdOut.empty() )
|
|
|
|
m_stdOut << stdOut << "\n";
|
|
|
|
if( !stdErr.empty() )
|
|
|
|
m_stdErr << stdErr << "\n";
|
|
|
|
}
|
|
|
|
|
2012-06-01 14:40:27 -04:00
|
|
|
virtual void Aborted() {
|
|
|
|
// !TBD
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
virtual void EndTesting( const Totals& ) {
|
2012-07-20 14:07:42 -04:00
|
|
|
std::ostream& str = m_config.stream;
|
2010-12-10 03:01:42 -05:00
|
|
|
{
|
|
|
|
XmlWriter xml( str );
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
for(; it != itEnd; ++it ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
|
2011-01-31 05:10:20 -05:00
|
|
|
xml.writeAttribute( "name", it->m_name );
|
|
|
|
xml.writeAttribute( "errors", it->m_errorsCount );
|
|
|
|
xml.writeAttribute( "failures", it->m_failuresCount );
|
|
|
|
xml.writeAttribute( "tests", it->m_testsCount );
|
2010-12-10 03:18:06 -05:00
|
|
|
xml.writeAttribute( "hostname", "tbd" );
|
|
|
|
xml.writeAttribute( "time", "tbd" );
|
|
|
|
xml.writeAttribute( "timestamp", "tbd" );
|
2010-12-10 03:01:42 -05:00
|
|
|
|
|
|
|
OutputTestCases( xml, *it );
|
|
|
|
}
|
|
|
|
|
2012-11-22 04:02:24 -05:00
|
|
|
xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ), false );
|
|
|
|
xml.scopedElement( "system-err" ).writeText( trim( m_stdErr.str() ), false );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
void OutputTestCases( XmlWriter& xml, const Stats& stats ) {
|
2011-01-31 05:10:20 -05:00
|
|
|
std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin();
|
|
|
|
std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end();
|
2012-05-16 09:53:59 -04:00
|
|
|
for(; it != itEnd; ++it ) {
|
2010-12-10 03:01:42 -05:00
|
|
|
|
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
|
2011-01-31 05:10:20 -05:00
|
|
|
xml.writeAttribute( "classname", it->m_className );
|
|
|
|
xml.writeAttribute( "name", it->m_name );
|
2010-12-10 03:01:42 -05:00
|
|
|
xml.writeAttribute( "time", "tbd" );
|
|
|
|
|
|
|
|
OutputTestResult( xml, *it );
|
2012-11-21 13:04:10 -05:00
|
|
|
|
|
|
|
std::string stdOut = trim( it->m_stdOut );
|
|
|
|
if( !stdOut.empty() )
|
2012-11-22 04:02:24 -05:00
|
|
|
xml.scopedElement( "system-out" ).writeText( stdOut, false );
|
2012-11-21 13:04:10 -05:00
|
|
|
std::string stdErr = trim( it->m_stdErr );
|
|
|
|
if( !stdErr.empty() )
|
2012-11-22 04:02:24 -05:00
|
|
|
xml.scopedElement( "system-err" ).writeText( stdErr, false );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-16 09:53:59 -04:00
|
|
|
void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats ) {
|
2011-01-31 05:10:20 -05:00
|
|
|
std::vector<TestStats>::const_iterator it = stats.m_testStats.begin();
|
|
|
|
std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end();
|
2012-05-16 09:53:59 -04:00
|
|
|
for(; it != itEnd; ++it ) {
|
|
|
|
if( it->m_element != "success" ) {
|
2011-01-31 05:10:20 -05:00
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( it->m_element );
|
2010-12-10 03:01:42 -05:00
|
|
|
|
2011-01-31 05:10:20 -05:00
|
|
|
xml.writeAttribute( "message", it->m_message );
|
|
|
|
xml.writeAttribute( "type", it->m_resultType );
|
|
|
|
if( !it->m_content.empty() )
|
|
|
|
xml.writeText( it->m_content );
|
2010-12-10 03:01:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-07-20 14:07:42 -04:00
|
|
|
ReporterConfig m_config;
|
2010-12-10 03:01:42 -05:00
|
|
|
bool m_currentTestSuccess;
|
|
|
|
|
|
|
|
Stats m_testSuiteStats;
|
|
|
|
Stats* m_currentStats;
|
|
|
|
std::vector<Stats> m_statsForSuites;
|
2012-11-22 14:17:20 -05:00
|
|
|
std::vector<const TestCaseStats*> m_currentTestCaseStats;
|
2010-12-10 03:01:42 -05:00
|
|
|
std::ostringstream m_stdOut;
|
|
|
|
std::ostringstream m_stdErr;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 09:42:46 -05:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
|