catch_reporter_teamcity.hpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. * Created by Phil Nash on 19th December 2014
  3. * Copyright 2014 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. #ifndef TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
  9. #define TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED
  10. // Don't #include any Catch headers here - we can assume they are already
  11. // included before this header.
  12. // This is not good practice in general but is necessary in this case so this
  13. // file can be distributed as a single header that works with the main
  14. // Catch single header.
  15. #include <cstring>
  16. #ifdef __clang__
  17. # pragma clang diagnostic push
  18. # pragma clang diagnostic ignored "-Wpadded"
  19. #endif
  20. namespace Catch {
  21. struct TeamCityReporter : StreamingReporterBase<TeamCityReporter> {
  22. TeamCityReporter( ReporterConfig const& _config )
  23. : StreamingReporterBase( _config )
  24. {
  25. m_reporterPrefs.shouldRedirectStdOut = true;
  26. }
  27. static std::string escape( std::string const& str ) {
  28. std::string escaped = str;
  29. replaceInPlace( escaped, "|", "||" );
  30. replaceInPlace( escaped, "'", "|'" );
  31. replaceInPlace( escaped, "\n", "|n" );
  32. replaceInPlace( escaped, "\r", "|r" );
  33. replaceInPlace( escaped, "[", "|[" );
  34. replaceInPlace( escaped, "]", "|]" );
  35. return escaped;
  36. }
  37. ~TeamCityReporter() override;
  38. static std::string getDescription() {
  39. return "Reports test results as TeamCity service messages";
  40. }
  41. void skipTest( TestCaseInfo const& /* testInfo */ ) override {
  42. }
  43. void noMatchingTestCases( std::string const& /* spec */ ) override {}
  44. void testGroupStarting( GroupInfo const& groupInfo ) override {
  45. StreamingReporterBase::testGroupStarting( groupInfo );
  46. stream << "##teamcity[testSuiteStarted name='"
  47. << escape( groupInfo.name ) << "']\n";
  48. }
  49. void testGroupEnded( TestGroupStats const& testGroupStats ) override {
  50. StreamingReporterBase::testGroupEnded( testGroupStats );
  51. stream << "##teamcity[testSuiteFinished name='"
  52. << escape( testGroupStats.groupInfo.name ) << "']\n";
  53. }
  54. void assertionStarting( AssertionInfo const& ) override {}
  55. bool assertionEnded( AssertionStats const& assertionStats ) override {
  56. AssertionResult const& result = assertionStats.assertionResult;
  57. if( !result.isOk() ) {
  58. ReusableStringStream msg;
  59. if( !m_headerPrintedForThisSection )
  60. printSectionHeader( msg.get() );
  61. m_headerPrintedForThisSection = true;
  62. msg << result.getSourceInfo() << "\n";
  63. switch( result.getResultType() ) {
  64. case ResultWas::ExpressionFailed:
  65. msg << "expression failed";
  66. break;
  67. case ResultWas::ThrewException:
  68. msg << "unexpected exception";
  69. break;
  70. case ResultWas::FatalErrorCondition:
  71. msg << "fatal error condition";
  72. break;
  73. case ResultWas::DidntThrowException:
  74. msg << "no exception was thrown where one was expected";
  75. break;
  76. case ResultWas::ExplicitFailure:
  77. msg << "explicit failure";
  78. break;
  79. // We shouldn't get here because of the isOk() test
  80. case ResultWas::Ok:
  81. case ResultWas::Info:
  82. case ResultWas::Warning:
  83. CATCH_ERROR( "Internal error in TeamCity reporter" );
  84. // These cases are here to prevent compiler warnings
  85. case ResultWas::Unknown:
  86. case ResultWas::FailureBit:
  87. case ResultWas::Exception:
  88. CATCH_ERROR( "Not implemented" );
  89. }
  90. if( assertionStats.infoMessages.size() == 1 )
  91. msg << " with message:";
  92. if( assertionStats.infoMessages.size() > 1 )
  93. msg << " with messages:";
  94. for( auto const& messageInfo : assertionStats.infoMessages )
  95. msg << "\n \"" << messageInfo.message << "\"";
  96. if( result.hasExpression() ) {
  97. msg <<
  98. "\n " << result.getExpressionInMacro() << "\n"
  99. "with expansion:\n" <<
  100. " " << result.getExpandedExpression() << "\n";
  101. }
  102. if( currentTestCaseInfo->okToFail() ) {
  103. msg << "- failure ignore as test marked as 'ok to fail'\n";
  104. stream << "##teamcity[testIgnored"
  105. << " name='" << escape( currentTestCaseInfo->name )<< "'"
  106. << " message='" << escape( msg.str() ) << "'"
  107. << "]\n";
  108. }
  109. else {
  110. stream << "##teamcity[testFailed"
  111. << " name='" << escape( currentTestCaseInfo->name )<< "'"
  112. << " message='" << escape( msg.str() ) << "'"
  113. << "]\n";
  114. }
  115. }
  116. stream.flush();
  117. return true;
  118. }
  119. void sectionStarting( SectionInfo const& sectionInfo ) override {
  120. m_headerPrintedForThisSection = false;
  121. StreamingReporterBase::sectionStarting( sectionInfo );
  122. }
  123. void testCaseStarting( TestCaseInfo const& testInfo ) override {
  124. m_testTimer.start();
  125. StreamingReporterBase::testCaseStarting( testInfo );
  126. stream << "##teamcity[testStarted name='"
  127. << escape( testInfo.name ) << "']\n";
  128. stream.flush();
  129. }
  130. void testCaseEnded( TestCaseStats const& testCaseStats ) override {
  131. StreamingReporterBase::testCaseEnded( testCaseStats );
  132. if( !testCaseStats.stdOut.empty() )
  133. stream << "##teamcity[testStdOut name='"
  134. << escape( testCaseStats.testInfo.name )
  135. << "' out='" << escape( testCaseStats.stdOut ) << "']\n";
  136. if( !testCaseStats.stdErr.empty() )
  137. stream << "##teamcity[testStdErr name='"
  138. << escape( testCaseStats.testInfo.name )
  139. << "' out='" << escape( testCaseStats.stdErr ) << "']\n";
  140. stream << "##teamcity[testFinished name='"
  141. << escape( testCaseStats.testInfo.name ) << "' duration='"
  142. << m_testTimer.getElapsedMilliseconds() << "']\n";
  143. stream.flush();
  144. }
  145. private:
  146. void printSectionHeader( std::ostream& os ) {
  147. assert( !m_sectionStack.empty() );
  148. if( m_sectionStack.size() > 1 ) {
  149. os << getLineOfChars<'-'>() << "\n";
  150. std::vector<SectionInfo>::const_iterator
  151. it = m_sectionStack.begin()+1, // Skip first section (test case)
  152. itEnd = m_sectionStack.end();
  153. for( ; it != itEnd; ++it )
  154. printHeaderString( os, it->name );
  155. os << getLineOfChars<'-'>() << "\n";
  156. }
  157. SourceLineInfo lineInfo = m_sectionStack.front().lineInfo;
  158. os << lineInfo << "\n";
  159. os << getLineOfChars<'.'>() << "\n\n";
  160. }
  161. // if string has a : in first line will set indent to follow it on
  162. // subsequent lines
  163. static void printHeaderString( std::ostream& os, std::string const& _string, std::size_t indent = 0 ) {
  164. std::size_t i = _string.find( ": " );
  165. if( i != std::string::npos )
  166. i+=2;
  167. else
  168. i = 0;
  169. os << Column( _string )
  170. .indent( indent+i)
  171. .initialIndent( indent ) << "\n";
  172. }
  173. private:
  174. bool m_headerPrintedForThisSection = false;
  175. Timer m_testTimer;
  176. };
  177. #ifdef CATCH_IMPL
  178. TeamCityReporter::~TeamCityReporter() {}
  179. #endif
  180. CATCH_REGISTER_REPORTER( "teamcity", TeamCityReporter )
  181. } // end namespace Catch
  182. #ifdef __clang__
  183. # pragma clang diagnostic pop
  184. #endif
  185. #endif // TWOBLUECUBES_CATCH_REPORTER_TEAMCITY_HPP_INCLUDED