catch_reporter_xml.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Created by Phil on 28/10/2010.
  3. * Copyright 2010 Two Blue Cubes Ltd. All rights reserved.
  4. *
  5. * Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. */
  8. #include "catch_reporter_xml.h"
  9. #include "../internal/catch_capture.hpp"
  10. #include "../internal/catch_reporter_registrars.hpp"
  11. #if defined(_MSC_VER)
  12. #pragma warning(push)
  13. #pragma warning(disable:4061) // Not all labels are EXPLICITLY handled in switch
  14. // Note that 4062 (not all labels are handled
  15. // and default is missing) is enabled
  16. #endif
  17. namespace Catch {
  18. XmlReporter::XmlReporter( ReporterConfig const& _config )
  19. : StreamingReporterBase( _config ),
  20. m_xml(_config.stream())
  21. {
  22. m_reporterPrefs.shouldRedirectStdOut = true;
  23. m_reporterPrefs.shouldReportAllAssertions = true;
  24. }
  25. XmlReporter::~XmlReporter() = default;
  26. std::string XmlReporter::getDescription() {
  27. return "Reports test results as an XML document";
  28. }
  29. std::string XmlReporter::getStylesheetRef() const {
  30. return std::string();
  31. }
  32. void XmlReporter::writeSourceInfo( SourceLineInfo const& sourceInfo ) {
  33. m_xml
  34. .writeAttribute( "filename", sourceInfo.file )
  35. .writeAttribute( "line", sourceInfo.line );
  36. }
  37. void XmlReporter::noMatchingTestCases( std::string const& s ) {
  38. StreamingReporterBase::noMatchingTestCases( s );
  39. }
  40. void XmlReporter::testRunStarting( TestRunInfo const& testInfo ) {
  41. StreamingReporterBase::testRunStarting( testInfo );
  42. std::string stylesheetRef = getStylesheetRef();
  43. if( !stylesheetRef.empty() )
  44. m_xml.writeStylesheetRef( stylesheetRef );
  45. m_xml.startElement( "Catch" );
  46. if( !m_config->name().empty() )
  47. m_xml.writeAttribute( "name", m_config->name() );
  48. if (m_config->testSpec().hasFilters())
  49. m_xml.writeAttribute( "filters", serializeFilters( m_config->getTestsOrTags() ) );
  50. if( m_config->rngSeed() != 0 )
  51. m_xml.scopedElement( "Randomness" )
  52. .writeAttribute( "seed", m_config->rngSeed() );
  53. }
  54. void XmlReporter::testGroupStarting( GroupInfo const& groupInfo ) {
  55. StreamingReporterBase::testGroupStarting( groupInfo );
  56. m_xml.startElement( "Group" )
  57. .writeAttribute( "name", groupInfo.name );
  58. }
  59. void XmlReporter::testCaseStarting( TestCaseInfo const& testInfo ) {
  60. StreamingReporterBase::testCaseStarting(testInfo);
  61. m_xml.startElement( "TestCase" )
  62. .writeAttribute( "name", trim( testInfo.name ) )
  63. .writeAttribute( "description", testInfo.description )
  64. .writeAttribute( "tags", testInfo.tagsAsString() );
  65. writeSourceInfo( testInfo.lineInfo );
  66. if ( m_config->showDurations() == ShowDurations::Always )
  67. m_testCaseTimer.start();
  68. m_xml.ensureTagClosed();
  69. }
  70. void XmlReporter::sectionStarting( SectionInfo const& sectionInfo ) {
  71. StreamingReporterBase::sectionStarting( sectionInfo );
  72. if( m_sectionDepth++ > 0 ) {
  73. m_xml.startElement( "Section" )
  74. .writeAttribute( "name", trim( sectionInfo.name ) );
  75. writeSourceInfo( sectionInfo.lineInfo );
  76. m_xml.ensureTagClosed();
  77. }
  78. }
  79. void XmlReporter::assertionStarting( AssertionInfo const& ) { }
  80. bool XmlReporter::assertionEnded( AssertionStats const& assertionStats ) {
  81. AssertionResult const& result = assertionStats.assertionResult;
  82. bool includeResults = m_config->includeSuccessfulResults() || !result.isOk();
  83. if( includeResults || result.getResultType() == ResultWas::Warning ) {
  84. // Print any info messages in <Info> tags.
  85. for( auto const& msg : assertionStats.infoMessages ) {
  86. if( msg.type == ResultWas::Info && includeResults ) {
  87. m_xml.scopedElement( "Info" )
  88. .writeText( msg.message );
  89. } else if ( msg.type == ResultWas::Warning ) {
  90. m_xml.scopedElement( "Warning" )
  91. .writeText( msg.message );
  92. }
  93. }
  94. }
  95. // Drop out if result was successful but we're not printing them.
  96. if( !includeResults && result.getResultType() != ResultWas::Warning )
  97. return true;
  98. // Print the expression if there is one.
  99. if( result.hasExpression() ) {
  100. m_xml.startElement( "Expression" )
  101. .writeAttribute( "success", result.succeeded() )
  102. .writeAttribute( "type", result.getTestMacroName() );
  103. writeSourceInfo( result.getSourceInfo() );
  104. m_xml.scopedElement( "Original" )
  105. .writeText( result.getExpression() );
  106. m_xml.scopedElement( "Expanded" )
  107. .writeText( result.getExpandedExpression() );
  108. }
  109. // And... Print a result applicable to each result type.
  110. switch( result.getResultType() ) {
  111. case ResultWas::ThrewException:
  112. m_xml.startElement( "Exception" );
  113. writeSourceInfo( result.getSourceInfo() );
  114. m_xml.writeText( result.getMessage() );
  115. m_xml.endElement();
  116. break;
  117. case ResultWas::FatalErrorCondition:
  118. m_xml.startElement( "FatalErrorCondition" );
  119. writeSourceInfo( result.getSourceInfo() );
  120. m_xml.writeText( result.getMessage() );
  121. m_xml.endElement();
  122. break;
  123. case ResultWas::Info:
  124. m_xml.scopedElement( "Info" )
  125. .writeText( result.getMessage() );
  126. break;
  127. case ResultWas::Warning:
  128. // Warning will already have been written
  129. break;
  130. case ResultWas::ExplicitFailure:
  131. m_xml.startElement( "Failure" );
  132. writeSourceInfo( result.getSourceInfo() );
  133. m_xml.writeText( result.getMessage() );
  134. m_xml.endElement();
  135. break;
  136. default:
  137. break;
  138. }
  139. if( result.hasExpression() )
  140. m_xml.endElement();
  141. return true;
  142. }
  143. void XmlReporter::sectionEnded( SectionStats const& sectionStats ) {
  144. StreamingReporterBase::sectionEnded( sectionStats );
  145. if( --m_sectionDepth > 0 ) {
  146. XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResults" );
  147. e.writeAttribute( "successes", sectionStats.assertions.passed );
  148. e.writeAttribute( "failures", sectionStats.assertions.failed );
  149. e.writeAttribute( "expectedFailures", sectionStats.assertions.failedButOk );
  150. if ( m_config->showDurations() == ShowDurations::Always )
  151. e.writeAttribute( "durationInSeconds", sectionStats.durationInSeconds );
  152. m_xml.endElement();
  153. }
  154. }
  155. void XmlReporter::testCaseEnded( TestCaseStats const& testCaseStats ) {
  156. StreamingReporterBase::testCaseEnded( testCaseStats );
  157. XmlWriter::ScopedElement e = m_xml.scopedElement( "OverallResult" );
  158. e.writeAttribute( "success", testCaseStats.totals.assertions.allOk() );
  159. if ( m_config->showDurations() == ShowDurations::Always )
  160. e.writeAttribute( "durationInSeconds", m_testCaseTimer.getElapsedSeconds() );
  161. if( !testCaseStats.stdOut.empty() )
  162. m_xml.scopedElement( "StdOut" ).writeText( trim( testCaseStats.stdOut ), XmlFormatting::Newline );
  163. if( !testCaseStats.stdErr.empty() )
  164. m_xml.scopedElement( "StdErr" ).writeText( trim( testCaseStats.stdErr ), XmlFormatting::Newline );
  165. m_xml.endElement();
  166. }
  167. void XmlReporter::testGroupEnded( TestGroupStats const& testGroupStats ) {
  168. StreamingReporterBase::testGroupEnded( testGroupStats );
  169. // TODO: Check testGroupStats.aborting and act accordingly.
  170. m_xml.scopedElement( "OverallResults" )
  171. .writeAttribute( "successes", testGroupStats.totals.assertions.passed )
  172. .writeAttribute( "failures", testGroupStats.totals.assertions.failed )
  173. .writeAttribute( "expectedFailures", testGroupStats.totals.assertions.failedButOk );
  174. m_xml.scopedElement( "OverallResultsCases")
  175. .writeAttribute( "successes", testGroupStats.totals.testCases.passed )
  176. .writeAttribute( "failures", testGroupStats.totals.testCases.failed )
  177. .writeAttribute( "expectedFailures", testGroupStats.totals.testCases.failedButOk );
  178. m_xml.endElement();
  179. }
  180. void XmlReporter::testRunEnded( TestRunStats const& testRunStats ) {
  181. StreamingReporterBase::testRunEnded( testRunStats );
  182. m_xml.scopedElement( "OverallResults" )
  183. .writeAttribute( "successes", testRunStats.totals.assertions.passed )
  184. .writeAttribute( "failures", testRunStats.totals.assertions.failed )
  185. .writeAttribute( "expectedFailures", testRunStats.totals.assertions.failedButOk );
  186. m_xml.scopedElement( "OverallResultsCases")
  187. .writeAttribute( "successes", testRunStats.totals.testCases.passed )
  188. .writeAttribute( "failures", testRunStats.totals.testCases.failed )
  189. .writeAttribute( "expectedFailures", testRunStats.totals.testCases.failedButOk );
  190. m_xml.endElement();
  191. }
  192. #if defined(CATCH_CONFIG_ENABLE_BENCHMARKING)
  193. void XmlReporter::benchmarkPreparing(std::string const& name) {
  194. m_xml.startElement("BenchmarkResults")
  195. .writeAttribute("name", name);
  196. }
  197. void XmlReporter::benchmarkStarting(BenchmarkInfo const &info) {
  198. m_xml.writeAttribute("samples", info.samples)
  199. .writeAttribute("resamples", info.resamples)
  200. .writeAttribute("iterations", info.iterations)
  201. .writeAttribute("clockResolution", info.clockResolution)
  202. .writeAttribute("estimatedDuration", info.estimatedDuration)
  203. .writeComment("All values in nano seconds");
  204. }
  205. void XmlReporter::benchmarkEnded(BenchmarkStats<> const& benchmarkStats) {
  206. m_xml.startElement("mean")
  207. .writeAttribute("value", benchmarkStats.mean.point.count())
  208. .writeAttribute("lowerBound", benchmarkStats.mean.lower_bound.count())
  209. .writeAttribute("upperBound", benchmarkStats.mean.upper_bound.count())
  210. .writeAttribute("ci", benchmarkStats.mean.confidence_interval);
  211. m_xml.endElement();
  212. m_xml.startElement("standardDeviation")
  213. .writeAttribute("value", benchmarkStats.standardDeviation.point.count())
  214. .writeAttribute("lowerBound", benchmarkStats.standardDeviation.lower_bound.count())
  215. .writeAttribute("upperBound", benchmarkStats.standardDeviation.upper_bound.count())
  216. .writeAttribute("ci", benchmarkStats.standardDeviation.confidence_interval);
  217. m_xml.endElement();
  218. m_xml.startElement("outliers")
  219. .writeAttribute("variance", benchmarkStats.outlierVariance)
  220. .writeAttribute("lowMild", benchmarkStats.outliers.low_mild)
  221. .writeAttribute("lowSevere", benchmarkStats.outliers.low_severe)
  222. .writeAttribute("highMild", benchmarkStats.outliers.high_mild)
  223. .writeAttribute("highSevere", benchmarkStats.outliers.high_severe);
  224. m_xml.endElement();
  225. m_xml.endElement();
  226. }
  227. void XmlReporter::benchmarkFailed(std::string const &error) {
  228. m_xml.scopedElement("failed").
  229. writeAttribute("message", error);
  230. m_xml.endElement();
  231. }
  232. #endif // CATCH_CONFIG_ENABLE_BENCHMARKING
  233. CATCH_REGISTER_REPORTER( "xml", XmlReporter )
  234. } // end namespace Catch
  235. #if defined(_MSC_VER)
  236. #pragma warning(pop)
  237. #endif