catch2/include/reporters/catch_reporter_xml.hpp

146 lines
5.9 KiB
C++
Raw Normal View History

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
#include "../internal/catch_capture.hpp"
#include "../internal/catch_interfaces_reporter.h"
#include "../internal/catch_reporter_registrars.hpp"
#include "../internal/catch_xmlwriter.hpp"
2010-11-10 00:24:00 +01:00
2012-05-16 15:53:59 +02:00
namespace Catch {
class XmlReporter : public SharedImpl<IReporter> {
2010-11-10 00:24:00 +01:00
public:
2012-07-20 20:07:42 +02:00
XmlReporter( const ReporterConfig& config ) : m_config( config ) {}
2010-11-10 00:24:00 +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";
}
virtual ~XmlReporter();
2010-11-10 00:24:00 +01:00
private: // IReporter
2010-11-10 00:24:00 +01:00
2012-05-16 15:53:59 +02:00
virtual bool shouldRedirectStdout() const {
return true;
}
2012-05-16 15:53:59 +02:00
virtual void StartTesting() {
2012-07-20 20:07:42 +02:00
m_xml = XmlWriter( m_config.stream );
m_xml.startElement( "Catch" );
2012-07-20 20:07:42 +02:00
if( !m_config.name.empty() )
m_xml.writeAttribute( "name", m_config.name );
}
2012-05-16 15:53:59 +02:00
virtual void EndTesting( const Totals& totals ) {
m_xml.scopedElement( "OverallResults" )
.writeAttribute( "successes", totals.assertions.passed )
.writeAttribute( "failures", totals.assertions.failed );
m_xml.endElement();
}
2012-05-16 15:53:59 +02:00
virtual void StartGroup( const std::string& groupName ) {
m_xml.startElement( "Group" )
.writeAttribute( "name", groupName );
}
2012-05-16 15:53:59 +02:00
virtual void EndGroup( const std::string&, const Totals& totals ) {
m_xml.scopedElement( "OverallResults" )
.writeAttribute( "successes", totals.assertions.passed )
.writeAttribute( "failures", totals.assertions.failed );
m_xml.endElement();
}
2012-05-16 15:53:59 +02:00
virtual void StartSection( const std::string& sectionName, const std::string& description ) {
m_xml.startElement( "Section" )
.writeAttribute( "name", sectionName )
.writeAttribute( "description", description );
}
virtual void NoAssertionsInSection( const std::string& ) {}
virtual void NoAssertionsInTestCase( const std::string& ) {}
2012-05-16 15:53:59 +02:00
virtual void EndSection( const std::string& /*sectionName*/, const Counts& assertions ) {
m_xml.scopedElement( "OverallResults" )
.writeAttribute( "successes", assertions.passed )
.writeAttribute( "failures", assertions.failed );
m_xml.endElement();
}
2012-05-16 15:53:59 +02:00
virtual void StartTestCase( const Catch::TestCaseInfo& testInfo ) {
m_xml.startElement( "TestCase" ).writeAttribute( "name", testInfo.getName() );
2010-11-10 00:24:00 +01:00
m_currentTestSuccess = true;
}
virtual void Result( const Catch::AssertionResult& assertionResult ) {
if( !m_config.includeSuccessfulResults && assertionResult.getResultType() == ResultWas::Ok )
2010-11-10 00:24:00 +01:00
return;
if( assertionResult.hasExpression() ) {
m_xml.startElement( "Expression" )
.writeAttribute( "success", assertionResult.ok() )
2012-10-24 22:59:47 +02:00
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
.writeAttribute( "line", assertionResult.getSourceInfo().line );
m_xml.scopedElement( "Original" )
.writeText( assertionResult.getExpression() );
m_xml.scopedElement( "Expanded" )
.writeText( assertionResult.getExpandedExpression() );
m_currentTestSuccess &= assertionResult.ok();
2010-11-10 00:24:00 +01:00
}
switch( assertionResult.getResultType() ) {
2010-11-10 00:24:00 +01:00
case ResultWas::ThrewException:
m_xml.scopedElement( "Exception" )
2012-10-24 22:59:47 +02:00
.writeAttribute( "filename", assertionResult.getSourceInfo().file )
.writeAttribute( "line", assertionResult.getSourceInfo().line )
.writeText( assertionResult.getMessage() );
2010-11-10 00:24:00 +01:00
m_currentTestSuccess = false;
break;
case ResultWas::Info:
m_xml.scopedElement( "Info" )
.writeText( assertionResult.getMessage() );
2010-11-10 00:24:00 +01:00
break;
case ResultWas::Warning:
m_xml.scopedElement( "Warning" )
.writeText( assertionResult.getMessage() );
2010-11-10 00:24:00 +01:00
break;
case ResultWas::ExplicitFailure:
m_xml.scopedElement( "Failure" )
.writeText( assertionResult.getMessage() );
2010-11-10 00:24:00 +01:00
m_currentTestSuccess = false;
break;
2011-09-29 09:58:40 +02:00
case ResultWas::Unknown:
case ResultWas::Ok:
case ResultWas::FailureBit:
case ResultWas::ExpressionFailed:
case ResultWas::Exception:
case ResultWas::DidntThrowException:
break;
2010-11-10 00:24:00 +01:00
}
if( assertionResult.hasExpression() )
m_xml.endElement();
2010-11-10 00:24:00 +01:00
}
virtual void Aborted() {
// !TBD
}
2010-11-10 00:24:00 +01:00
2012-05-16 15:53:59 +02:00
virtual void EndTestCase( const Catch::TestCaseInfo&, const Totals&, const std::string&, const std::string& ) {
m_xml.scopedElement( "OverallResult" ).writeAttribute( "success", m_currentTestSuccess );
m_xml.endElement();
2010-11-10 00:24:00 +01:00
}
private:
2012-07-20 20:07:42 +02:00
ReporterConfig m_config;
2010-11-10 00:24:00 +01:00
bool m_currentTestSuccess;
XmlWriter m_xml;
2010-11-10 00:24:00 +01:00
};
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_XML_HPP_INCLUDED