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)
|
|
|
|
*/
|
|
|
|
|
2013-12-03 19:52:41 +01:00
|
|
|
#include "catch_reporter_bases.hpp"
|
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
#include "catch_reporter_junit.h"
|
|
|
|
|
2014-04-23 07:51:58 +02:00
|
|
|
#include "../internal/catch_tostring.h"
|
2011-06-02 09:49:47 +02:00
|
|
|
#include "../internal/catch_reporter_registrars.hpp"
|
2019-08-07 22:39:01 +02:00
|
|
|
#include "../internal/catch_text.h"
|
2010-12-10 09:01:42 +01:00
|
|
|
|
2018-04-18 14:59:10 +02:00
|
|
|
#include <cassert>
|
2017-11-07 19:01:10 +01:00
|
|
|
#include <sstream>
|
2017-07-19 10:13:47 +02:00
|
|
|
#include <ctime>
|
2017-07-27 22:31:27 +02:00
|
|
|
#include <algorithm>
|
2017-07-19 10:13:47 +02:00
|
|
|
|
2012-05-16 15:53:59 +02:00
|
|
|
namespace Catch {
|
|
|
|
|
2017-01-16 20:21:43 +01:00
|
|
|
namespace {
|
|
|
|
std::string getCurrentTimestamp() {
|
|
|
|
// Beware, this is not reentrant because of backward compatibility issues
|
|
|
|
// Also, UTC only, again because of backward compatibility (%z is C++11)
|
|
|
|
time_t rawtime;
|
|
|
|
std::time(&rawtime);
|
2017-09-18 18:13:17 +02:00
|
|
|
auto const timeStampSize = sizeof("2017-01-16T17:06:45Z");
|
2017-01-16 20:21:43 +01:00
|
|
|
|
2017-02-06 01:43:53 +01:00
|
|
|
#ifdef _MSC_VER
|
2017-01-16 20:21:43 +01:00
|
|
|
std::tm timeInfo = {};
|
|
|
|
gmtime_s(&timeInfo, &rawtime);
|
|
|
|
#else
|
|
|
|
std::tm* timeInfo;
|
|
|
|
timeInfo = std::gmtime(&rawtime);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
char timeStamp[timeStampSize];
|
|
|
|
const char * const fmt = "%Y-%m-%dT%H:%M:%SZ";
|
|
|
|
|
2017-02-06 01:43:53 +01:00
|
|
|
#ifdef _MSC_VER
|
2017-01-16 20:21:43 +01:00
|
|
|
std::strftime(timeStamp, timeStampSize, fmt, &timeInfo);
|
|
|
|
#else
|
|
|
|
std::strftime(timeStamp, timeStampSize, fmt, timeInfo);
|
|
|
|
#endif
|
|
|
|
return std::string(timeStamp);
|
|
|
|
}
|
|
|
|
|
2017-07-27 22:31:27 +02:00
|
|
|
std::string fileNameTag(const std::vector<std::string> &tags) {
|
|
|
|
auto it = std::find_if(begin(tags),
|
|
|
|
end(tags),
|
|
|
|
[] (std::string const& tag) {return tag.front() == '#'; });
|
|
|
|
if (it != tags.end())
|
|
|
|
return it->substr(1);
|
|
|
|
return std::string();
|
|
|
|
}
|
2017-11-14 17:12:51 +01:00
|
|
|
} // anonymous namespace
|
2017-01-16 20:21:43 +01:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
JunitReporter::JunitReporter( ReporterConfig const& _config )
|
2013-08-15 19:39:55 +02:00
|
|
|
: CumulativeReporterBase( _config ),
|
2017-04-25 19:56:53 +02:00
|
|
|
xml( _config.stream() )
|
2015-08-07 09:20:56 +02:00
|
|
|
{
|
|
|
|
m_reporterPrefs.shouldRedirectStdOut = true;
|
2018-07-14 20:51:02 +02:00
|
|
|
m_reporterPrefs.shouldReportAllAssertions = true;
|
2015-08-07 09:20:56 +02:00
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2018-02-03 18:16:33 +01:00
|
|
|
JunitReporter::~JunitReporter() {}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
std::string JunitReporter::getDescription() {
|
|
|
|
return "Reports test results in an XML format that looks like Ant's junitreport target";
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::noMatchingTestCases( std::string const& /*spec*/ ) {}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testRunStarting( TestRunInfo const& runInfo ) {
|
|
|
|
CumulativeReporterBase::testRunStarting( runInfo );
|
|
|
|
xml.startElement( "testsuites" );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testGroupStarting( GroupInfo const& groupInfo ) {
|
|
|
|
suiteTimer.start();
|
|
|
|
stdOutForSuite.clear();
|
|
|
|
stdErrForSuite.clear();
|
|
|
|
unexpectedExceptions = 0;
|
|
|
|
CumulativeReporterBase::testGroupStarting( groupInfo );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testCaseStarting( TestCaseInfo const& testCaseInfo ) {
|
|
|
|
m_okToFail = testCaseInfo.okToFail();
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
bool JunitReporter::assertionEnded( AssertionStats const& assertionStats ) {
|
|
|
|
if( assertionStats.assertionResult.getResultType() == ResultWas::ThrewException && !m_okToFail )
|
|
|
|
unexpectedExceptions++;
|
|
|
|
return CumulativeReporterBase::assertionEnded( assertionStats );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
|
|
|
|
stdOutForSuite += testCaseStats.stdOut;
|
|
|
|
stdErrForSuite += testCaseStats.stdErr;
|
|
|
|
CumulativeReporterBase::testCaseEnded( testCaseStats );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testGroupEnded( TestGroupStats const& testGroupStats ) {
|
|
|
|
double suiteTime = suiteTimer.getElapsedSeconds();
|
|
|
|
CumulativeReporterBase::testGroupEnded( testGroupStats );
|
|
|
|
writeGroup( *m_testGroups.back(), suiteTime );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::testRunEndedCumulative() {
|
|
|
|
xml.endElement();
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::writeGroup( TestGroupNode const& groupNode, double suiteTime ) {
|
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testsuite" );
|
2019-04-11 13:04:54 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
TestGroupStats const& stats = groupNode.value;
|
|
|
|
xml.writeAttribute( "name", stats.groupInfo.name );
|
|
|
|
xml.writeAttribute( "errors", unexpectedExceptions );
|
|
|
|
xml.writeAttribute( "failures", stats.totals.assertions.failed-unexpectedExceptions );
|
|
|
|
xml.writeAttribute( "tests", stats.totals.assertions.total() );
|
|
|
|
xml.writeAttribute( "hostname", "tbd" ); // !TBD
|
|
|
|
if( m_config->showDurations() == ShowDurations::Never )
|
|
|
|
xml.writeAttribute( "time", "" );
|
|
|
|
else
|
|
|
|
xml.writeAttribute( "time", suiteTime );
|
|
|
|
xml.writeAttribute( "timestamp", getCurrentTimestamp() );
|
|
|
|
|
2019-04-11 13:04:54 +02:00
|
|
|
// Write properties if there are any
|
|
|
|
if (m_config->hasTestFilters() || m_config->rngSeed() != 0) {
|
|
|
|
auto properties = xml.scopedElement("properties");
|
|
|
|
if (m_config->hasTestFilters()) {
|
|
|
|
xml.scopedElement("property")
|
|
|
|
.writeAttribute("name", "filters")
|
|
|
|
.writeAttribute("value", serializeFilters(m_config->getTestsOrTags()));
|
|
|
|
}
|
|
|
|
if (m_config->rngSeed() != 0) {
|
|
|
|
xml.scopedElement("property")
|
|
|
|
.writeAttribute("name", "random-seed")
|
|
|
|
.writeAttribute("value", m_config->rngSeed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
// Write test cases
|
|
|
|
for( auto const& child : groupNode.children )
|
|
|
|
writeTestCase( *child );
|
|
|
|
|
2019-06-22 14:39:34 +02:00
|
|
|
xml.scopedElement( "system-out" ).writeText( trim( stdOutForSuite ), XmlFormatting::Newline );
|
|
|
|
xml.scopedElement( "system-err" ).writeText( trim( stdErrForSuite ), XmlFormatting::Newline );
|
2017-11-14 17:12:51 +01:00
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::writeTestCase( TestCaseNode const& testCaseNode ) {
|
|
|
|
TestCaseStats const& stats = testCaseNode.value;
|
|
|
|
|
|
|
|
// All test cases have exactly one section - which represents the
|
|
|
|
// test case itself. That section may have 0-n nested sections
|
|
|
|
assert( testCaseNode.children.size() == 1 );
|
|
|
|
SectionNode const& rootSection = *testCaseNode.children.front();
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
std::string className = stats.testInfo.className;
|
|
|
|
|
|
|
|
if( className.empty() ) {
|
|
|
|
className = fileNameTag(stats.testInfo.tags);
|
|
|
|
if ( className.empty() )
|
|
|
|
className = "global";
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
if ( !m_config->name().empty() )
|
|
|
|
className = m_config->name() + "." + className;
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
writeSection( className, "", rootSection );
|
|
|
|
}
|
2015-11-04 19:01:28 +01:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::writeSection( std::string const& className,
|
|
|
|
std::string const& rootName,
|
|
|
|
SectionNode const& sectionNode ) {
|
|
|
|
std::string name = trim( sectionNode.stats.sectionInfo.name );
|
|
|
|
if( !rootName.empty() )
|
|
|
|
name = rootName + '/' + name;
|
|
|
|
|
|
|
|
if( !sectionNode.assertions.empty() ||
|
|
|
|
!sectionNode.stdOut.empty() ||
|
|
|
|
!sectionNode.stdErr.empty() ) {
|
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( "testcase" );
|
2013-08-15 19:39:55 +02:00
|
|
|
if( className.empty() ) {
|
2017-11-14 17:12:51 +01:00
|
|
|
xml.writeAttribute( "classname", name );
|
|
|
|
xml.writeAttribute( "name", "root" );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
xml.writeAttribute( "classname", className );
|
|
|
|
xml.writeAttribute( "name", name );
|
2013-08-15 19:39:55 +02:00
|
|
|
}
|
2017-11-14 17:12:51 +01:00
|
|
|
xml.writeAttribute( "time", ::Catch::Detail::stringify( sectionNode.stats.durationInSeconds ) );
|
2017-05-30 21:40:28 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
writeAssertions( sectionNode );
|
2017-05-30 21:40:28 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
if( !sectionNode.stdOut.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
xml.scopedElement( "system-out" ).writeText( trim( sectionNode.stdOut ), XmlFormatting::Newline );
|
2017-11-14 17:12:51 +01:00
|
|
|
if( !sectionNode.stdErr.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
xml.scopedElement( "system-err" ).writeText( trim( sectionNode.stdErr ), XmlFormatting::Newline );
|
2013-08-15 19:39:55 +02:00
|
|
|
}
|
2017-11-14 17:12:51 +01:00
|
|
|
for( auto const& childNode : sectionNode.childSections )
|
|
|
|
if( className.empty() )
|
|
|
|
writeSection( name, "", *childNode );
|
|
|
|
else
|
|
|
|
writeSection( className, name, *childNode );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::writeAssertions( SectionNode const& sectionNode ) {
|
|
|
|
for( auto const& assertion : sectionNode.assertions )
|
|
|
|
writeAssertion( assertion );
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
void JunitReporter::writeAssertion( AssertionStats const& stats ) {
|
|
|
|
AssertionResult const& result = stats.assertionResult;
|
|
|
|
if( !result.isOk() ) {
|
|
|
|
std::string elementName;
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
case ResultWas::FatalErrorCondition:
|
|
|
|
elementName = "error";
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
case ResultWas::ExpressionFailed:
|
|
|
|
case ResultWas::DidntThrowException:
|
|
|
|
elementName = "failure";
|
|
|
|
break;
|
|
|
|
|
|
|
|
// We should never see these here:
|
|
|
|
case ResultWas::Info:
|
|
|
|
case ResultWas::Warning:
|
|
|
|
case ResultWas::Ok:
|
|
|
|
case ResultWas::Unknown:
|
|
|
|
case ResultWas::FailureBit:
|
|
|
|
case ResultWas::Exception:
|
|
|
|
elementName = "internalError";
|
|
|
|
break;
|
2013-08-15 19:39:55 +02:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
XmlWriter::ScopedElement e = xml.scopedElement( elementName );
|
|
|
|
|
2019-08-07 22:39:01 +02:00
|
|
|
xml.writeAttribute( "message", result.getExpression() );
|
2017-11-14 17:12:51 +01:00
|
|
|
xml.writeAttribute( "type", result.getTestMacroName() );
|
|
|
|
|
|
|
|
ReusableStringStream rss;
|
2019-08-07 22:39:01 +02:00
|
|
|
if (stats.totals.assertions.total() > 0) {
|
|
|
|
rss << "FAILED" << ":\n";
|
|
|
|
if (result.hasExpression()) {
|
|
|
|
rss << " ";
|
|
|
|
rss << result.getExpressionInMacro();
|
|
|
|
rss << '\n';
|
|
|
|
}
|
|
|
|
if (result.hasExpandedExpression()) {
|
|
|
|
rss << "with expansion:\n";
|
|
|
|
rss << Column(result.getExpandedExpression()).indent(2) << '\n';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rss << '\n';
|
|
|
|
}
|
|
|
|
|
2017-11-14 17:12:51 +01:00
|
|
|
if( !result.getMessage().empty() )
|
|
|
|
rss << result.getMessage() << '\n';
|
|
|
|
for( auto const& msg : stats.infoMessages )
|
|
|
|
if( msg.type == ResultWas::Info )
|
|
|
|
rss << msg.message << '\n';
|
|
|
|
|
|
|
|
rss << "at " << result.getSourceInfo();
|
2019-06-22 14:39:34 +02:00
|
|
|
xml.writeText( rss.str(), XmlFormatting::Newline );
|
2017-11-14 17:12:51 +01:00
|
|
|
}
|
|
|
|
}
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2017-08-17 18:07:24 +02:00
|
|
|
CATCH_REGISTER_REPORTER( "junit", JunitReporter )
|
2013-08-15 19:39:55 +02:00
|
|
|
|
2010-12-10 09:01:42 +01:00
|
|
|
} // end namespace Catch
|