2020-08-30 15:43:45 +02:00
|
|
|
|
|
|
|
// Copyright Catch2 Authors
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// https://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2020-04-04 21:53:23 +02:00
|
|
|
#include <catch2/reporters/catch_reporter_xml.hpp>
|
2013-12-03 18:52:41 +00:00
|
|
|
|
2020-08-23 22:43:56 +02:00
|
|
|
#include <catch2/reporters/catch_reporter_helpers.hpp>
|
2020-07-30 08:46:07 +02:00
|
|
|
#include <catch2/interfaces/catch_interfaces_config.hpp>
|
|
|
|
#include <catch2/catch_test_spec.hpp>
|
2020-03-30 10:34:21 +02:00
|
|
|
#include <catch2/internal/catch_string_manip.hpp>
|
|
|
|
#include <catch2/internal/catch_list.hpp>
|
2020-07-29 14:30:19 +02:00
|
|
|
#include <catch2/catch_test_case_info.hpp>
|
2022-04-12 21:22:43 +02:00
|
|
|
#include <catch2/internal/catch_move_and_forward.hpp>
|
2022-05-03 19:44:55 +02:00
|
|
|
#include <catch2/catch_version.hpp>
|
2010-11-09 23:24:00 +00:00
|
|
|
|
2017-10-30 12:14:20 +01:00
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch
|
|
|
|
// Note that 4062 (not all labels are handled
|
|
|
|
// and default is missing) is enabled
|
|
|
|
#endif
|
|
|
|
|
2012-05-16 14:53:59 +01:00
|
|
|
namespace Catch {
|
2022-04-12 21:22:43 +02:00
|
|
|
XmlReporter::XmlReporter( ReporterConfig&& _config )
|
|
|
|
: StreamingReporterBase( CATCH_MOVE(_config) ),
|
2022-02-23 23:46:45 +01:00
|
|
|
m_xml(m_stream)
|
2017-11-14 17:56:27 +01:00
|
|
|
{
|
2020-07-28 09:24:57 +02:00
|
|
|
m_preferences.shouldRedirectStdOut = true;
|
|
|
|
m_preferences.shouldReportAllAssertions = true;
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
2017-12-07 00:02:19 +00:00
|
|
|
XmlReporter::~XmlReporter() = default;
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
std::string XmlReporter::getDescription() {
|
|
|
|
return "Reports test results as an XML document";
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string XmlReporter::getStylesheetRef() const {
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) {
|
|
|
|
m_xml
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "filename"_sr, sourceInfo.file )
|
|
|
|
.writeAttribute( "line"_sr, sourceInfo.line );
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testRunStarting( testInfo );
|
|
|
|
std::string stylesheetRef = getStylesheetRef();
|
|
|
|
if( !stylesheetRef.empty() )
|
|
|
|
m_xml.writeStylesheetRef( stylesheetRef );
|
2021-10-09 00:02:30 +02:00
|
|
|
m_xml.startElement("Catch2TestRun")
|
|
|
|
.writeAttribute("name"_sr, m_config->name())
|
2022-05-03 19:44:55 +02:00
|
|
|
.writeAttribute("rng-seed"_sr, m_config->rngSeed())
|
|
|
|
.writeAttribute("catch2-version"_sr, libraryVersion());
|
2019-04-01 22:33:57 +03:00
|
|
|
if (m_config->testSpec().hasFilters())
|
2021-05-30 13:43:14 +02:00
|
|
|
m_xml.writeAttribute( "filters"_sr, serializeFilters( m_config->getTestsOrTags() ) );
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
|
|
|
|
StreamingReporterBase::testCaseStarting(testInfo);
|
|
|
|
m_xml.startElement( "TestCase" )
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "name"_sr, trim( testInfo.name ) )
|
|
|
|
.writeAttribute( "tags"_sr, testInfo.tagsAsString() );
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
writeSourceInfo( testInfo.lineInfo );
|
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
|
|
|
m_testCaseTimer.start();
|
|
|
|
m_xml.ensureTagClosed();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) {
|
|
|
|
StreamingReporterBase::sectionStarting( sectionInfo );
|
|
|
|
if( m_sectionDepth++ > 0 ) {
|
|
|
|
m_xml.startElement( "Section" )
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "name"_sr, trim( sectionInfo.name ) );
|
2017-11-14 17:56:27 +01:00
|
|
|
writeSourceInfo( sectionInfo.lineInfo );
|
2017-02-13 15:56:25 +00:00
|
|
|
m_xml.ensureTagClosed();
|
2010-12-10 20:01:40 +00:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
2013-07-03 19:14:59 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
void XmlReporter::assertionStarting( AssertionInfo const& ) { }
|
2014-10-11 17:20:55 -04:00
|
|
|
|
2021-09-15 18:57:56 +02:00
|
|
|
void XmlReporter::assertionEnded( AssertionStats const& assertionStats ) {
|
2015-11-04 18:01:28 +00:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
AssertionResult const& result = assertionStats.assertionResult;
|
2017-03-03 14:12:47 +00:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
bool includeResults = m_config->includeSuccessfulResults() || !result.isOk();
|
2017-03-03 14:12:47 +00:00
|
|
|
|
2017-12-06 14:30:17 +00:00
|
|
|
if( includeResults || result.getResultType() == ResultWas::Warning ) {
|
2017-11-14 17:56:27 +01:00
|
|
|
// Print any info messages in <Info> tags.
|
|
|
|
for( auto const& msg : assertionStats.infoMessages ) {
|
2017-12-06 14:30:17 +00:00
|
|
|
if( msg.type == ResultWas::Info && includeResults ) {
|
2017-11-14 17:56:27 +01:00
|
|
|
m_xml.scopedElement( "Info" )
|
|
|
|
.writeText( msg.message );
|
|
|
|
} else if ( msg.type == ResultWas::Warning ) {
|
|
|
|
m_xml.scopedElement( "Warning" )
|
|
|
|
.writeText( msg.message );
|
2014-10-11 17:20:55 -04:00
|
|
|
}
|
2013-07-24 19:13:08 +01:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
2013-07-03 19:14:59 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// Drop out if result was successful but we're not printing them.
|
|
|
|
if( !includeResults && result.getResultType() != ResultWas::Warning )
|
2021-09-15 18:57:56 +02:00
|
|
|
return;
|
2013-07-03 19:14:59 +01:00
|
|
|
|
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// Print the expression if there is one.
|
|
|
|
if( result.hasExpression() ) {
|
|
|
|
m_xml.startElement( "Expression" )
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "success"_sr, result.succeeded() )
|
|
|
|
.writeAttribute( "type"_sr, result.getTestMacroName() );
|
2015-11-04 18:01:28 +00:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
2015-11-04 18:01:28 +00:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
m_xml.scopedElement( "Original" )
|
|
|
|
.writeText( result.getExpression() );
|
|
|
|
m_xml.scopedElement( "Expanded" )
|
|
|
|
.writeText( result.getExpandedExpression() );
|
2014-10-11 17:20:55 -04:00
|
|
|
}
|
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
// And... Print a result applicable to each result type.
|
|
|
|
switch( result.getResultType() ) {
|
|
|
|
case ResultWas::ThrewException:
|
|
|
|
m_xml.startElement( "Exception" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
2014-10-11 17:20:55 -04:00
|
|
|
m_xml.endElement();
|
2017-11-14 17:56:27 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::FatalErrorCondition:
|
|
|
|
m_xml.startElement( "FatalErrorCondition" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
|
|
|
m_xml.endElement();
|
|
|
|
break;
|
|
|
|
case ResultWas::Info:
|
|
|
|
m_xml.scopedElement( "Info" )
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeText( result.getMessage() );
|
2017-11-14 17:56:27 +01:00
|
|
|
break;
|
|
|
|
case ResultWas::Warning:
|
|
|
|
// Warning will already have been written
|
|
|
|
break;
|
|
|
|
case ResultWas::ExplicitFailure:
|
|
|
|
m_xml.startElement( "Failure" );
|
|
|
|
writeSourceInfo( result.getSourceInfo() );
|
|
|
|
m_xml.writeText( result.getMessage() );
|
|
|
|
m_xml.endElement();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2010-11-09 23:24:00 +00:00
|
|
|
}
|
2012-06-01 19:40:27 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
if( result.hasExpression() )
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2017-02-06 16:14:06 +00:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
void XmlReporter::sectionEnded( SectionStats const& sectionStats ) {
|
|
|
|
StreamingReporterBase::sectionEnded( sectionStats );
|
|
|
|
if( --m_sectionDepth > 0 ) {
|
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" );
|
2021-05-30 13:43:14 +02:00
|
|
|
e.writeAttribute( "successes"_sr, sectionStats.assertions.passed );
|
|
|
|
e.writeAttribute( "failures"_sr, sectionStats.assertions.failed );
|
|
|
|
e.writeAttribute( "expectedFailures"_sr, sectionStats.assertions.failedButOk );
|
2013-07-03 19:14:59 +01:00
|
|
|
|
2017-11-14 17:56:27 +01:00
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
2021-05-30 13:43:14 +02:00
|
|
|
e.writeAttribute( "durationInSeconds"_sr, sectionStats.durationInSeconds );
|
2015-11-04 18:01:28 +00:00
|
|
|
|
2010-12-10 20:01:40 +00:00
|
|
|
m_xml.endElement();
|
2013-07-03 19:14:59 +01:00
|
|
|
}
|
2017-11-14 17:56:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
|
|
|
|
StreamingReporterBase::testCaseEnded( testCaseStats );
|
|
|
|
XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
|
2021-05-30 13:43:14 +02:00
|
|
|
e.writeAttribute( "success"_sr, testCaseStats.totals.assertions.allOk() );
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
if ( m_config->showDurations() == ShowDurations::Always )
|
2021-05-30 13:43:14 +02:00
|
|
|
e.writeAttribute( "durationInSeconds"_sr, m_testCaseTimer.getElapsedSeconds() );
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
if( !testCaseStats.stdOut.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), XmlFormatting::Newline );
|
2017-11-14 17:56:27 +01:00
|
|
|
if( !testCaseStats.stdErr.empty() )
|
2019-06-22 14:39:34 +02:00
|
|
|
m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), XmlFormatting::Newline );
|
2017-11-14 17:56:27 +01:00
|
|
|
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) {
|
|
|
|
StreamingReporterBase::testRunEnded( testRunStats );
|
|
|
|
m_xml.scopedElement( "OverallResults" )
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "successes"_sr, testRunStats.totals.assertions.passed )
|
|
|
|
.writeAttribute( "failures"_sr, testRunStats.totals.assertions.failed )
|
|
|
|
.writeAttribute( "expectedFailures"_sr, testRunStats.totals.assertions.failedButOk );
|
2020-06-11 16:58:56 -05:00
|
|
|
m_xml.scopedElement( "OverallResultsCases")
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute( "successes"_sr, testRunStats.totals.testCases.passed )
|
|
|
|
.writeAttribute( "failures"_sr, testRunStats.totals.testCases.failed )
|
|
|
|
.writeAttribute( "expectedFailures"_sr, testRunStats.totals.testCases.failedButOk );
|
2017-11-14 17:56:27 +01:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
2013-07-03 19:14:59 +01:00
|
|
|
|
2021-05-30 23:45:55 +02:00
|
|
|
void XmlReporter::benchmarkPreparing( StringRef name ) {
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.startElement("BenchmarkResults")
|
2021-05-30 23:45:55 +02:00
|
|
|
.writeAttribute("name"_sr, name);
|
2019-07-29 12:42:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::benchmarkStarting(BenchmarkInfo const &info) {
|
2021-05-30 13:43:14 +02:00
|
|
|
m_xml.writeAttribute("samples"_sr, info.samples)
|
|
|
|
.writeAttribute("resamples"_sr, info.resamples)
|
|
|
|
.writeAttribute("iterations"_sr, info.iterations)
|
|
|
|
.writeAttribute("clockResolution"_sr, info.clockResolution)
|
|
|
|
.writeAttribute("estimatedDuration"_sr, info.estimatedDuration)
|
|
|
|
.writeComment("All values in nano seconds"_sr);
|
2019-04-23 23:41:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void XmlReporter::benchmarkEnded(BenchmarkStats<> const& benchmarkStats) {
|
|
|
|
m_xml.startElement("mean")
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute("value"_sr, benchmarkStats.mean.point.count())
|
|
|
|
.writeAttribute("lowerBound"_sr, benchmarkStats.mean.lower_bound.count())
|
|
|
|
.writeAttribute("upperBound"_sr, benchmarkStats.mean.upper_bound.count())
|
|
|
|
.writeAttribute("ci"_sr, benchmarkStats.mean.confidence_interval);
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.startElement("standardDeviation")
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute("value"_sr, benchmarkStats.standardDeviation.point.count())
|
|
|
|
.writeAttribute("lowerBound"_sr, benchmarkStats.standardDeviation.lower_bound.count())
|
|
|
|
.writeAttribute("upperBound"_sr, benchmarkStats.standardDeviation.upper_bound.count())
|
|
|
|
.writeAttribute("ci"_sr, benchmarkStats.standardDeviation.confidence_interval);
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.startElement("outliers")
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeAttribute("variance"_sr, benchmarkStats.outlierVariance)
|
|
|
|
.writeAttribute("lowMild"_sr, benchmarkStats.outliers.low_mild)
|
|
|
|
.writeAttribute("lowSevere"_sr, benchmarkStats.outliers.low_severe)
|
|
|
|
.writeAttribute("highMild"_sr, benchmarkStats.outliers.high_mild)
|
|
|
|
.writeAttribute("highSevere"_sr, benchmarkStats.outliers.high_severe);
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
2021-05-30 23:45:55 +02:00
|
|
|
void XmlReporter::benchmarkFailed(StringRef error) {
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.scopedElement("failed").
|
2021-05-30 13:43:14 +02:00
|
|
|
writeAttribute("message"_sr, error);
|
2019-04-23 23:41:13 +02:00
|
|
|
m_xml.endElement();
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:48:22 +01:00
|
|
|
void XmlReporter::listReporters(std::vector<ReporterDescription> const& descriptions) {
|
2019-06-22 15:31:11 +02:00
|
|
|
auto outerTag = m_xml.scopedElement("AvailableReporters");
|
|
|
|
for (auto const& reporter : descriptions) {
|
|
|
|
auto inner = m_xml.scopedElement("Reporter");
|
|
|
|
m_xml.startElement("Name", XmlFormatting::Indent)
|
|
|
|
.writeText(reporter.name, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Description", XmlFormatting::Indent)
|
|
|
|
.writeText(reporter.description, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:48:22 +01:00
|
|
|
void XmlReporter::listTests(std::vector<TestCaseHandle> const& tests) {
|
2019-06-22 15:31:11 +02:00
|
|
|
auto outerTag = m_xml.scopedElement("MatchingTests");
|
|
|
|
for (auto const& test : tests) {
|
|
|
|
auto innerTag = m_xml.scopedElement("TestCase");
|
|
|
|
auto const& testInfo = test.getTestCaseInfo();
|
|
|
|
m_xml.startElement("Name", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.name, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("ClassName", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.className, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Tags", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.tagsAsString(), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
|
|
|
|
auto sourceTag = m_xml.scopedElement("SourceInfo");
|
|
|
|
m_xml.startElement("File", XmlFormatting::Indent)
|
|
|
|
.writeText(testInfo.lineInfo.file, XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
m_xml.startElement("Line", XmlFormatting::Indent)
|
|
|
|
.writeText(std::to_string(testInfo.lineInfo.line), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-16 15:48:22 +01:00
|
|
|
void XmlReporter::listTags(std::vector<TagInfo> const& tags) {
|
2019-06-22 15:31:11 +02:00
|
|
|
auto outerTag = m_xml.scopedElement("TagsFromMatchingTests");
|
|
|
|
for (auto const& tag : tags) {
|
|
|
|
auto innerTag = m_xml.scopedElement("Tag");
|
|
|
|
m_xml.startElement("Count", XmlFormatting::Indent)
|
|
|
|
.writeText(std::to_string(tag.count), XmlFormatting::None)
|
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
auto aliasTag = m_xml.scopedElement("Aliases");
|
|
|
|
for (auto const& alias : tag.spellings) {
|
|
|
|
m_xml.startElement("Alias", XmlFormatting::Indent)
|
2021-05-30 13:43:14 +02:00
|
|
|
.writeText(alias, XmlFormatting::None)
|
2019-06-22 15:31:11 +02:00
|
|
|
.endElement(XmlFormatting::Newline);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-09 23:24:00 +00:00
|
|
|
} // end namespace Catch
|
2017-10-30 12:14:20 +01:00
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
#pragma warning(pop)
|
|
|
|
#endif
|