2010-12-10 09:01:42 +01: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 09:49:47 +02: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 09:01:42 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
|
|
|
class JunitReporter : public SharedImpl<IReporter> {
|
|
|
|
|
|
|
|
struct TestStats {
|
2011-01-31 11:10:20 +01:00
|
|
|
std::string m_element;
|
|
|
|
std::string m_resultType;
|
|
|
|
std::string m_message;
|
|
|
|
std::string m_content;
|
2010-12-10 09:01:42 +01:00
|
|
|
};
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
struct TestCaseStats {
|
|
|
|
|
|
|
|
TestCaseStats( const std::string& name = std::string() ) :m_name( name ){}
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2011-01-31 11:10:20 +01:00
|
|
|
double m_timeInSeconds;
|
|
|
|
std::string m_status;
|
|
|
|
std::string m_className;
|
|
|
|
std::string m_name;
|
|
|
|
std::vector<TestStats> m_testStats;
|
2010-12-10 09:01:42 +01:00
|
|
|
};
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
struct Stats {
|
|
|
|
|
2010-12-10 09:01:42 +01:00
|
|
|
Stats( const std::string& name = std::string() )
|
2011-01-31 11:10:20 +01:00
|
|
|
: m_testsCount( 0 ),
|
|
|
|
m_failuresCount( 0 ),
|
|
|
|
m_disabledCount( 0 ),
|
|
|
|
m_errorsCount( 0 ),
|
|
|
|
m_timeInSeconds( 0 ),
|
|
|
|
m_name( name )
|
2012-05-16 15:53:59 +02:00
|
|
|
{}
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2011-01-31 11:10:20 +01: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 09:01:42 +01:00
|
|
|
|
2011-01-31 11:10:20 +01:00
|
|
|
std::vector<TestCaseStats> m_testCaseStats;
|
2010-12-10 09:01:42 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2012-07-20 20:07:42 +02:00
|
|
|
JunitReporter( const ReporterConfig& config )
|
2010-12-10 09:01:42 +01:00
|
|
|
: m_config( config ),
|
|
|
|
m_testSuiteStats( "AllTests" ),
|
|
|
|
m_currentStats( &m_testSuiteStats )
|
2012-08-13 08:46:10 +02:00
|
|
|
{}
|
|
|
|
virtual ~JunitReporter();
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
static std::string getDescription() {
|
2010-12-10 09:01:42 +01:00
|
|
|
return "Reports test results in an XML format that looks like Ant's junitreport target";
|
|
|
|
}
|
|
|
|
|
2010-12-31 23:07:47 +01:00
|
|
|
private: // IReporter
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual bool shouldRedirectStdout() const {
|
2012-02-17 10:28:21 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void StartTesting(){}
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void StartGroup( const std::string& groupName ) {
|
2010-12-10 09:18:06 +01:00
|
|
|
m_statsForSuites.push_back( Stats( groupName ) );
|
|
|
|
m_currentStats = &m_statsForSuites.back();
|
2010-12-10 09:01:42 +01:00
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void EndGroup( const std::string&, const Totals& totals ) {
|
2012-02-24 09:59:35 +01:00
|
|
|
m_currentStats->m_testsCount = totals.assertions.total();
|
2010-12-10 09:01:42 +01:00
|
|
|
m_currentStats = &m_testSuiteStats;
|
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void StartSection( const std::string&, const std::string& ){}
|
2010-12-28 15:42:46 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void EndSection( const std::string&, const Counts& ){}
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) {
|
|
|
|
m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) );
|
2010-12-10 09:01:42 +01:00
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void Result( const Catch::ResultInfo& resultInfo ) {
|
2012-07-20 20:07:42 +02:00
|
|
|
if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults ) {
|
2011-01-31 11:10:20 +01:00
|
|
|
TestCaseStats& testCaseStats = m_currentStats->m_testCaseStats.back();
|
2010-12-10 09:01:42 +01:00
|
|
|
TestStats stats;
|
|
|
|
std::ostringstream oss;
|
|
|
|
if( !resultInfo.getMessage().empty() )
|
|
|
|
oss << resultInfo.getMessage() << " at ";
|
2012-02-15 09:20:06 +01:00
|
|
|
oss << SourceLineInfo( resultInfo.getFilename(), resultInfo.getLine() );
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_content = oss.str();
|
|
|
|
stats.m_message = resultInfo.getExpandedExpression();
|
|
|
|
stats.m_resultType = resultInfo.getTestMacroName();
|
2012-05-16 15:53:59 +02:00
|
|
|
|
|
|
|
switch( resultInfo.getResultType() ) {
|
2010-12-10 09:01:42 +01:00
|
|
|
case ResultWas::ThrewException:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "error";
|
|
|
|
m_currentStats->m_errorsCount++;
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "info"; // !TBD ?
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "warning"; // !TBD ?
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "failure";
|
|
|
|
m_currentStats->m_failuresCount++;
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExpressionFailed:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "failure";
|
|
|
|
m_currentStats->m_failuresCount++;
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Ok:
|
2011-01-31 11:10:20 +01:00
|
|
|
stats.m_element = "success";
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
2011-09-29 09:58:40 +02:00
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
case ResultWas::DidntThrowException:
|
2010-12-10 09:01:42 +01:00
|
|
|
break;
|
|
|
|
}
|
2012-05-16 15:53:59 +02:00
|
|
|
testCaseStats.m_testStats.push_back( stats );
|
2010-12-10 09:01:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string& stdOut, const std::string& stdErr ) {
|
2010-12-10 09:01:42 +01:00
|
|
|
if( !stdOut.empty() )
|
|
|
|
m_stdOut << stdOut << "\n";
|
|
|
|
if( !stdErr.empty() )
|
|
|
|
m_stdErr << stdErr << "\n";
|
|
|
|
}
|
|
|
|
|
2012-06-01 20:40:27 +02:00
|
|
|
virtual void Aborted() {
|
|
|
|
// !TBD
|
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
virtual void EndTesting( const Totals& ) {
|
2012-07-20 20:07:42 +02:00
|
|
|
std::ostream& str = m_config.stream;
|
2010-12-10 09:01:42 +01: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 15:53:59 +02:00
|
|
|
for(; it != itEnd; ++it ) {
|
2010-12-10 09:01:42 +01:00
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
|
2011-01-31 11:10:20 +01: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 09:18:06 +01:00
|
|
|
xml.writeAttribute( "hostname", "tbd" );
|
|
|
|
xml.writeAttribute( "time", "tbd" );
|
|
|
|
xml.writeAttribute( "timestamp", "tbd" );
|
2010-12-10 09:01:42 +01:00
|
|
|
|
|
|
|
OutputTestCases( xml, *it );
|
|
|
|
}
|
|
|
|
|
|
|
|
xml.scopedElement( "system-out" ).writeText( trim( m_stdOut.str() ) );
|
2012-05-24 00:59:42 +02:00
|
|
|
xml.scopedElement( "system-err" ).writeText( trim( m_stdErr.str() ) );
|
2010-12-10 09:01:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
void OutputTestCases( XmlWriter& xml, const Stats& stats ) {
|
2011-01-31 11:10:20 +01:00
|
|
|
std::vector<TestCaseStats>::const_iterator it = stats.m_testCaseStats.begin();
|
|
|
|
std::vector<TestCaseStats>::const_iterator itEnd = stats.m_testCaseStats.end();
|
2012-05-16 15:53:59 +02:00
|
|
|
for(; it != itEnd; ++it ) {
|
2010-12-10 09:01:42 +01:00
|
|
|
xml.writeBlankLine();
|
|
|
|
xml.writeComment( "Test case" );
|
|
|
|
|
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
|
2011-01-31 11:10:20 +01:00
|
|
|
xml.writeAttribute( "classname", it->m_className );
|
|
|
|
xml.writeAttribute( "name", it->m_name );
|
2010-12-10 09:01:42 +01:00
|
|
|
xml.writeAttribute( "time", "tbd" );
|
|
|
|
|
|
|
|
OutputTestResult( xml, *it );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
void OutputTestResult( XmlWriter& xml, const TestCaseStats& stats ) {
|
2011-01-31 11:10:20 +01:00
|
|
|
std::vector<TestStats>::const_iterator it = stats.m_testStats.begin();
|
|
|
|
std::vector<TestStats>::const_iterator itEnd = stats.m_testStats.end();
|
2012-05-16 15:53:59 +02:00
|
|
|
for(; it != itEnd; ++it ) {
|
|
|
|
if( it->m_element != "success" ) {
|
2011-01-31 11:10:20 +01:00
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( it->m_element );
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2011-01-31 11:10:20 +01: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 09:01:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-07-20 20:07:42 +02:00
|
|
|
ReporterConfig m_config;
|
2010-12-10 09:01:42 +01:00
|
|
|
bool m_currentTestSuccess;
|
|
|
|
|
|
|
|
Stats m_testSuiteStats;
|
|
|
|
Stats* m_currentStats;
|
|
|
|
std::vector<Stats> m_statsForSuites;
|
|
|
|
std::ostringstream m_stdOut;
|
|
|
|
std::ostringstream m_stdErr;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 15:42:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED
|