catch2/catch_reporter_junit.hpp

264 lines
9.9 KiB
C++
Raw Normal View History

2010-12-10 09:01:42 +01:00
/*
* catch_reporter_junit.hpp
* Catch
*
* 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
#include "internal/catch_capture.hpp"
#include "internal/catch_interfaces_reporter.h"
#include "internal/catch_reporter_registrars.hpp"
2010-12-10 09:01:42 +01:00
#include "internal/catch_xmlwriter.hpp"
namespace Catch
{
class JunitReporter : public Catch::IReporter
2010-12-10 09:01:42 +01:00
{
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
};
struct TestCaseStats
{
TestCaseStats( const std::string& name = std::string() )
2011-01-31 11:10:20 +01:00
: 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
};
struct Stats
{
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 )
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:
///////////////////////////////////////////////////////////////////////////
JunitReporter( const IReporterConfig& config )
2010-12-10 09:01:42 +01:00
: m_config( config ),
m_testSuiteStats( "AllTests" ),
m_currentStats( &m_testSuiteStats )
{
}
///////////////////////////////////////////////////////////////////////////
static std::string getDescription()
{
return "Reports test results in an XML format that looks like Ant's junitreport target";
}
private: // IReporter
2010-12-10 09:01:42 +01:00
///////////////////////////////////////////////////////////////////////////
virtual void StartTesting()
{
}
///////////////////////////////////////////////////////////////////////////
virtual void StartGroup( const std::string& groupName )
{
m_statsForSuites.push_back( Stats( groupName ) );
m_currentStats = &m_statsForSuites.back();
2010-12-10 09:01:42 +01:00
}
///////////////////////////////////////////////////////////////////////////
virtual void EndGroup( const std::string&, std::size_t succeeded, std::size_t failed )
2010-12-10 09:01:42 +01:00
{
2011-01-31 11:10:20 +01:00
m_currentStats->m_testsCount = failed+succeeded;
2010-12-10 09:01:42 +01:00
m_currentStats = &m_testSuiteStats;
}
2010-12-28 18:21:29 +01:00
virtual void StartSection( const std::string& /*sectionName*/, const std::string /*description*/ )
{
}
2010-12-28 18:21:29 +01:00
virtual void EndSection( const std::string& /*sectionName*/, std::size_t /*succeeded*/, std::size_t /*failed*/ )
{
}
2010-12-10 09:01:42 +01:00
///////////////////////////////////////////////////////////////////////////
virtual void StartTestCase( const Catch::TestCaseInfo& testInfo )
{
2011-01-31 11:10:20 +01:00
m_currentStats->m_testCaseStats.push_back( TestCaseStats( testInfo.getName() ) );
2010-12-10 09:01:42 +01:00
}
///////////////////////////////////////////////////////////////////////////
virtual void Result( const Catch::ResultInfo& resultInfo )
{
2010-12-27 23:05:13 +01:00
if( resultInfo.getResultType() != ResultWas::Ok || m_config.includeSuccessfulResults() )
2010-12-10 09:01:42 +01:00
{
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 ";
}
oss << 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();
2010-12-10 09:01:42 +01:00
switch( resultInfo.getResultType() )
{
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;
default:
2011-01-31 11:10:20 +01:00
stats.m_element = "unknown";
2010-12-10 09:01:42 +01:00
break;
}
2011-01-31 11:10:20 +01:00
testCaseStats.m_testStats.push_back( stats );
2010-12-10 09:01:42 +01:00
}
}
///////////////////////////////////////////////////////////////////////////
virtual void EndTestCase( const Catch::TestCaseInfo&, std::size_t /* succeeded */, std::size_t /* failed */, 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";
}
///////////////////////////////////////////////////////////////////////////
virtual void EndTesting( std::size_t /* succeeded */, std::size_t /* failed */ )
{
std::ostream& str = m_config.stream();
{
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();
for(; it != itEnd; ++it )
{
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 );
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() ) );
xml.scopedElement( "system-err" ).writeText( trim( m_stdOut.str() ) );
}
}
///////////////////////////////////////////////////////////////////////////
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();
2010-12-10 09:01:42 +01:00
for(; it != itEnd; ++it )
{
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 );
}
}
///////////////////////////////////////////////////////////////////////////
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();
2010-12-10 09:01:42 +01:00
for(; it != itEnd; ++it )
{
2011-01-31 11:10:20 +01:00
if( it->m_element != "success" )
2010-12-10 09:01:42 +01:00
{
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:
const IReporterConfig& 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;
};
INTERNAL_CATCH_REGISTER_REPORTER( "junit", JunitReporter )
2010-12-10 09:01:42 +01:00
} // end namespace Catch
#endif // TWOBLUECUBES_CATCH_REPORTER_JUNIT_HPP_INCLUDED