207-Rpt-TeamCityReporter.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // 207-Rpt-TeamCityReporter.cpp
  2. // Catch has built-in and external reporters:
  3. // Built-in:
  4. // - compact
  5. // - console
  6. // - junit
  7. // - xml
  8. // External:
  9. // - automake
  10. // - tap
  11. // - teamcity (this example)
  12. // main() and reporter code provided in 200-Rpt-CatchMain.cpp
  13. #include <catch2/catch.hpp>
  14. #ifdef _MSC_VER
  15. # pragma warning (disable : 4702) // Disable warning: unreachable code
  16. #endif
  17. TEST_CASE( "TeamCity passes unconditionally succeeding assertion", "[teamcity]" ) {
  18. SUCCEED();
  19. }
  20. TEST_CASE( "TeamCity reports unconditionally failing assertion", "[teamcity]" ) {
  21. FAIL();
  22. }
  23. TEST_CASE( "TeamCity reports failing check", "[teamcity]" ) {
  24. REQUIRE( 3 == 7 );
  25. }
  26. TEST_CASE( "TeamCity reports failing check-false", "[teamcity]" ) {
  27. REQUIRE_FALSE( 3 == 3 );
  28. }
  29. TEST_CASE( "TeamCity reports failing check-that", "[teamcity]" ) {
  30. using namespace Catch;
  31. REQUIRE_THAT( "hello", Contains( "world" ) );
  32. }
  33. TEST_CASE( "TeamCity reports unexpected exception", "[teamcity]" ) {
  34. REQUIRE( (throw std::runtime_error("surprise!"), true) );
  35. }
  36. TEST_CASE( "TeamCity reports undesired exception", "[teamcity]" ) {
  37. REQUIRE_NOTHROW( (throw std::runtime_error("surprise!"), true) );
  38. }
  39. TEST_CASE( "TeamCity reports missing expected exception", "[teamcity]" ) {
  40. REQUIRE_THROWS( true );
  41. }
  42. TEST_CASE( "TeamCity reports missing specific expected exception", "[teamcity]" ) {
  43. REQUIRE_THROWS_AS( throw std::bad_alloc(), std::runtime_error );
  44. }
  45. TEST_CASE( "TeamCity reports unexpected message in expected exception", "[teamcity]" ) {
  46. using namespace Catch;
  47. CHECK_THROWS_WITH( throw std::runtime_error("hello"), "world" );
  48. CHECK_THROWS_WITH( throw std::runtime_error("hello"), Contains("world") );
  49. }
  50. struct MyException: public std::runtime_error
  51. {
  52. MyException( char const * text )
  53. : std::runtime_error( text ) {}
  54. ~MyException() override;
  55. };
  56. // prevent -Wweak-vtables:
  57. MyException::~MyException() = default;
  58. struct MyExceptionMatcher : Catch::MatcherBase< std::runtime_error >
  59. {
  60. std::string m_text;
  61. MyExceptionMatcher( char const * text )
  62. : m_text( text )
  63. {}
  64. ~MyExceptionMatcher() override;
  65. bool match( std::runtime_error const & arg ) const override
  66. {
  67. return m_text == arg.what() ;
  68. }
  69. std::string describe() const override
  70. {
  71. return "it's me";
  72. }
  73. };
  74. // prevent -Wweak-vtables:
  75. MyExceptionMatcher::~MyExceptionMatcher() = default;
  76. TEST_CASE( "TeamCity failing check-throws-matches", "[teamcity]" ) {
  77. CHECK_THROWS_MATCHES( throw MyException("hello"), MyException, MyExceptionMatcher("world") );
  78. }
  79. // [!throws] - lets Catch know that this test is likely to throw an exception even if successful.
  80. // This causes the test to be excluded when running with -e or --nothrow.
  81. // No special effects for the reporter.
  82. TEST_CASE( "TeamCity throwing exception with tag [!throws]", "[teamcity][!throws]" ) {
  83. REQUIRE_THROWS( throw std::runtime_error("unsurprisingly") );
  84. }
  85. // [!mayfail] - doesn't fail the test if any given assertion fails (but still reports it). This can be useful to flag a work-in-progress, or a known issue that you don't want to immediately fix but still want to track in your tests.
  86. TEST_CASE( "TeamCity failing assertion with tag [!mayfail]", "[teamcity][!mayfail] " ) {
  87. REQUIRE( 3 == 7 ); // doesn't fail test case this time, reports: testIgnored
  88. REQUIRE( 3 == 3 );
  89. }
  90. // [!shouldfail] - like [!mayfail] but fails the test if it passes.
  91. // This can be useful if you want to be notified of accidental, or third-party, fixes.
  92. TEST_CASE( "TeamCity succeeding assertion with tag [!shouldfail]", "[teamcity][!shouldfail]" ) {
  93. SUCCEED( "Marked [!shouldfail]" );
  94. }
  95. // Compile & run:
  96. // - g++ -std=c++11 -Wall -I$(CATCH_ROOT) -DCATCH_EXAMPLE_RPT_1=\"include/reporters/catch_reporter_teamcity.hpp\" -o 200-Rpt-CatchMainTeamCity.o -c 200-Rpt-CatchMain.cpp
  97. // - g++ -std=c++11 -Wall -I$(CATCH_ROOT) -o 207-Rpt-TeamCityReporter 207-Rpt-TeamCityReporter.cpp 200-Rpt-CatchMainTeamCity.o && 207-Rpt-TeamCityReporter --list-reporters
  98. //
  99. // - cl -EHsc -I%CATCH_ROOT% -DCATCH_EXAMPLE_RPT_1=\"include/reporters/catch_reporter_teamcity.hpp\" -Fo200-Rpt-CatchMainTeamCity.obj -c 200-Rpt-CatchMain.cpp
  100. // - cl -EHsc -I%CATCH_ROOT% 207-Rpt-TeamCityReporter.cpp 200-Rpt-CatchMainTeamCity.o && 207-Rpt-TeamCityReporter --list-reporters
  101. // Compilation output (--list-reporters):
  102. // Available reporters:
  103. // compact: Reports test results on a single line, suitable for IDEs
  104. // console: Reports test results as plain lines of text
  105. // junit: Reports test results in an XML format that looks like Ant's
  106. // junitreport target
  107. // teamcity: Reports test results as TeamCity service messages
  108. // xml: Reports test results as an XML document
  109. // Expected output (abbreviated and broken into shorter lines):
  110. //
  111. // prompt> 207-Rpt-TeamCityReporter.exe --reporter teamcity
  112. // ##teamcity[testSuiteStarted name='207-Rpt-TeamCityReporter.exe']
  113. // ##teamcity[testStarted name='TeamCity passes unconditionally succeeding assertion']
  114. // ##teamcity[testFinished name='TeamCity passes unconditionally succeeding assertion' duration='1']
  115. // ##teamcity[testStarted name='TeamCity reports unconditionally failing assertion']
  116. // ##teamcity[testFailed name='TeamCity reports unconditionally failing assertion' /
  117. // message='.../examples/207-Rpt-TeamCityReporter.cpp:23|n/
  118. // ...............................................................................|n|n/
  119. // .../examples/207-Rpt-TeamCityReporter.cpp:25|nexplicit failure']
  120. // ##teamcity[testFinished name='TeamCity reports unconditionally failing assertion' duration='3']
  121. // ...