2010-11-10 00:24:00 +01:00
|
|
|
/*
|
|
|
|
* Created by Phil on 28/10/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_XML_HPP_INCLUDED
|
|
|
|
#define TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "catch_reporter_bases.hpp"
|
|
|
|
|
2011-06-02 09:49:47 +02:00
|
|
|
#include "../internal/catch_capture.hpp"
|
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
|
|
|
#include "../internal/catch_xmlwriter.hpp"
|
2014-10-12 01:57:45 +02:00
|
|
|
#include "../internal/catch_timer.h"
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
namespace Catch {
|
2014-10-11 23:20:55 +02:00
|
|
|
class XmlReporter : public StreamingReporterBase {
|
2010-11-10 00:24:00 +01:00
|
|
|
public:
|
2014-10-11 23:20:55 +02:00
|
|
|
XmlReporter( ReporterConfig const& _config )
|
|
|
|
: StreamingReporterBase( _config ),
|
2016-11-26 12:11:23 +01:00
|
|
|
m_xml(_config.stream()),
|
2014-10-11 23:20:55 +02:00
|
|
|
m_sectionDepth( 0 )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual ~XmlReporter() CATCH_OVERRIDE;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
static std::string getDescription() {
|
2010-11-10 00:24:00 +01:00
|
|
|
return "Reports test results as an XML document";
|
|
|
|
}
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
public: // StreamingReporterBase
|
2012-02-17 10:28:21 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void noMatchingTestCases( std::string const& s ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::noMatchingTestCases( s );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunStarting( TestRunInfo const& testInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testRunStarting( testInfo );
|
|
|
|
m_xml.startElement( "Catch" );
|
|
|
|
if( !m_config->name().empty() )
|
|
|
|
m_xml.writeAttribute( "name", m_config->name() );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupStarting( GroupInfo const& groupInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testGroupStarting( groupInfo );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.startElement( "Group" )
|
2014-10-11 23:20:55 +02:00
|
|
|
.writeAttribute( "name", groupInfo.name );
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseStarting( TestCaseInfo const& testInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testCaseStarting(testInfo);
|
2016-08-30 11:54:02 +02:00
|
|
|
m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.name );
|
2014-10-12 01:57:45 +02:00
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
m_testCaseTimer.start();
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionStarting( SectionInfo const& sectionInfo ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
2013-07-24 20:13:08 +02:00
|
|
|
if( m_sectionDepth++ > 0 ) {
|
|
|
|
m_xml.startElement( "Section" )
|
2014-10-11 23:20:55 +02:00
|
|
|
.writeAttribute( "name", trim( sectionInfo.name ) )
|
|
|
|
.writeAttribute( "description", sectionInfo.description );
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2010-12-10 21:01:40 +01:00
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void assertionStarting( AssertionInfo const& ) CATCH_OVERRIDE { }
|
2014-10-11 23:20:55 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual bool assertionEnded( AssertionStats const& assertionStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
const AssertionResult& assertionResult = assertionStats.assertionResult;
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Print any info messages in <Info> tags.
|
|
|
|
if( assertionStats.assertionResult.getResultType() != ResultWas::Ok ) {
|
|
|
|
for( std::vector<MessageInfo>::const_iterator it = assertionStats.infoMessages.begin(), itEnd = assertionStats.infoMessages.end();
|
|
|
|
it != itEnd;
|
|
|
|
++it ) {
|
|
|
|
if( it->type == ResultWas::Info ) {
|
|
|
|
m_xml.scopedElement( "Info" )
|
|
|
|
.writeText( it->message );
|
|
|
|
} else if ( it->type == ResultWas::Warning ) {
|
|
|
|
m_xml.scopedElement( "Warning" )
|
|
|
|
.writeText( it->message );
|
|
|
|
}
|
|
|
|
}
|
2013-07-24 20:13:08 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Drop out if result was successful but we're not printing them.
|
|
|
|
if( !m_config->includeSuccessfulResults() && isOk(assertionResult.getResultType()) )
|
|
|
|
return true;
|
2010-11-10 00:24:00 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// Print the expression if there is one.
|
2012-10-16 09:31:05 +02:00
|
|
|
if( assertionResult.hasExpression() ) {
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.startElement( "Expression" )
|
2012-11-13 10:44:52 +01:00
|
|
|
.writeAttribute( "success", assertionResult.succeeded() )
|
2017-01-26 23:13:12 +01:00
|
|
|
.writeAttribute( "type", assertionResult.getTestMacroName() )
|
2012-10-24 22:59:47 +02:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line );
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Original" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getExpression() );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Expanded" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getExpandedExpression() );
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
// And... Print a result applicable to each result type.
|
2012-10-16 09:31:05 +02:00
|
|
|
switch( assertionResult.getResultType() ) {
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::ThrewException:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Exception" )
|
2012-10-24 22:59:47 +02:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
2014-08-22 09:07:39 +02:00
|
|
|
case ResultWas::FatalErrorCondition:
|
2016-10-14 19:45:08 +02:00
|
|
|
m_xml.scopedElement( "FatalErrorCondition" )
|
2014-08-22 09:07:39 +02:00
|
|
|
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
|
|
|
|
.writeAttribute( "line", assertionResult.getSourceInfo().line )
|
|
|
|
.writeText( assertionResult.getMessage() );
|
|
|
|
break;
|
2010-11-10 00:24:00 +01:00
|
|
|
case ResultWas::Info:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Info" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
2014-12-30 19:24:31 +01:00
|
|
|
// Warning will already have been written
|
2010-11-10 00:24:00 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.scopedElement( "Failure" )
|
2012-10-16 09:31:05 +02:00
|
|
|
.writeText( assertionResult.getMessage() );
|
2010-12-28 15:42:46 +01:00
|
|
|
break;
|
2014-12-30 19:24:31 +01:00
|
|
|
default:
|
|
|
|
break;
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2012-10-16 09:31:05 +02:00
|
|
|
if( assertionResult.hasExpression() )
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void sectionEnded( SectionStats const& sectionStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::sectionEnded( sectionStats );
|
|
|
|
if( --m_sectionDepth > 0 ) {
|
2014-10-12 01:57:45 +02:00
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" );
|
|
|
|
e.writeAttribute( "successes", sectionStats.assertions.passed );
|
|
|
|
e.writeAttribute( "failures", sectionStats.assertions.failed );
|
|
|
|
e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk );
|
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds );
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2010-11-10 00:24:00 +01:00
|
|
|
}
|
2012-06-01 20:40:27 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testCaseEnded( TestCaseStats const& testCaseStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
2014-10-12 01:57:45 +02:00
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
|
2014-12-30 19:24:31 +01:00
|
|
|
e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() );
|
2014-10-12 01:57:45 +02:00
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() );
|
|
|
|
|
2017-02-06 17:14:06 +01:00
|
|
|
if( !testCaseStats.stdOut.empty() )
|
|
|
|
m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), false );
|
|
|
|
if( !testCaseStats.stdErr.empty() )
|
|
|
|
m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), false );
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
m_xml.endElement();
|
2012-06-01 20:40:27 +02:00
|
|
|
}
|
2013-07-03 20:14:59 +02:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testGroupEnded( TestGroupStats const& testGroupStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testGroupEnded( testGroupStats );
|
|
|
|
// TODO: Check testGroupStats.aborting and act accordingly.
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
|
|
|
.writeAttribute( "successes", testGroupStats.totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", testGroupStats.totals.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk );
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2015-08-07 09:20:56 +02:00
|
|
|
virtual void testRunEnded( TestRunStats const& testRunStats ) CATCH_OVERRIDE {
|
2014-10-11 23:20:55 +02:00
|
|
|
StreamingReporterBase::testRunEnded( testRunStats );
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
|
|
|
.writeAttribute( "successes", testRunStats.totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures", testRunStats.totals.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk );
|
2010-12-10 21:01:40 +01:00
|
|
|
m_xml.endElement();
|
2013-07-03 20:14:59 +02:00
|
|
|
}
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
private:
|
2014-10-12 01:57:45 +02:00
|
|
|
Timer m_testCaseTimer;
|
2010-12-10 21:01:40 +01:00
|
|
|
XmlWriter m_xml;
|
2013-07-24 20:13:08 +02:00
|
|
|
int m_sectionDepth;
|
2010-11-10 00:24:00 +01:00
|
|
|
};
|
|
|
|
|
2014-10-11 23:20:55 +02:00
|
|
|
INTERNAL_CATCH_REGISTER_REPORTER( "xml", XmlReporter )
|
|
|
|
|
2010-11-10 00:24:00 +01:00
|
|
|
} // end namespace Catch
|
|
|
|
|
2010-12-28 15:42:46 +01:00
|
|
|
#endif // TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED
|