TestXmlTestReporter.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "../UnitTest++.h"
  2. #include "../XmlTestReporter.h"
  3. #include <sstream>
  4. using namespace UnitTest;
  5. using std::ostringstream;
  6. namespace
  7. {
  8. #ifdef UNITTEST_USE_CUSTOM_STREAMS
  9. // Overload to let MemoryOutStream accept std::string
  10. MemoryOutStream& operator<<(MemoryOutStream& s, const std::string& value)
  11. {
  12. s << value.c_str();
  13. return s;
  14. }
  15. #endif
  16. struct XmlTestReporterFixture
  17. {
  18. XmlTestReporterFixture()
  19. : reporter(output)
  20. {
  21. }
  22. ostringstream output;
  23. XmlTestReporter reporter;
  24. };
  25. TEST_FIXTURE(XmlTestReporterFixture, MultipleCharactersAreEscaped)
  26. {
  27. TestDetails const details("TestName", "suite", "filename.h", 4321);
  28. reporter.ReportTestStart(details);
  29. reporter.ReportFailure(details, "\"\"\'\'&&<<>>");
  30. reporter.ReportTestFinish(details, 0.1f);
  31. reporter.ReportSummary(1, 2, 3, 0.1f);
  32. char const* expected =
  33. "<?xml version=\"1.0\"?>"
  34. "<unittest-results tests=\"1\" failedtests=\"2\" failures=\"3\" time=\"0.1\">"
  35. "<test suite=\"suite\" name=\"TestName\" time=\"0.1\">"
  36. "<failure message=\"filename.h(4321) : "
  37. "&quot;&quot;&apos;&apos;&amp;&amp;&lt;&lt;&gt;&gt;\"/>"
  38. "</test>"
  39. "</unittest-results>";
  40. CHECK_EQUAL(expected, output.str());
  41. }
  42. TEST_FIXTURE(XmlTestReporterFixture, OutputIsCachedUntilReportSummaryIsCalled)
  43. {
  44. TestDetails const details("", "", "", 0);
  45. reporter.ReportTestStart(details);
  46. reporter.ReportFailure(details, "message");
  47. reporter.ReportTestFinish(details, 1.0F);
  48. CHECK(output.str().empty());
  49. reporter.ReportSummary(1, 1, 1, 1.0f);
  50. CHECK(!output.str().empty());
  51. }
  52. TEST_FIXTURE(XmlTestReporterFixture, EmptyReportSummaryFormat)
  53. {
  54. reporter.ReportSummary(0, 0, 0, 0.1f);
  55. const char *expected =
  56. "<?xml version=\"1.0\"?>"
  57. "<unittest-results tests=\"0\" failedtests=\"0\" failures=\"0\" time=\"0.1\">"
  58. "</unittest-results>";
  59. CHECK_EQUAL(expected, output.str());
  60. }
  61. TEST_FIXTURE(XmlTestReporterFixture, SingleSuccessfulTestReportSummaryFormat)
  62. {
  63. TestDetails const details("TestName", "DefaultSuite", "", 0);
  64. reporter.ReportTestStart(details);
  65. reporter.ReportSummary(1, 0, 0, 0.1f);
  66. const char *expected =
  67. "<?xml version=\"1.0\"?>"
  68. "<unittest-results tests=\"1\" failedtests=\"0\" failures=\"0\" time=\"0.1\">"
  69. "<test suite=\"DefaultSuite\" name=\"TestName\" time=\"0\"/>"
  70. "</unittest-results>";
  71. CHECK_EQUAL(expected, output.str());
  72. }
  73. TEST_FIXTURE(XmlTestReporterFixture, SingleFailedTestReportSummaryFormat)
  74. {
  75. TestDetails const details("A Test", "suite", "A File", 4321);
  76. reporter.ReportTestStart(details);
  77. reporter.ReportFailure(details, "A Failure");
  78. reporter.ReportSummary(1, 1, 1, 0.1f);
  79. const char *expected =
  80. "<?xml version=\"1.0\"?>"
  81. "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"1\" time=\"0.1\">"
  82. "<test suite=\"suite\" name=\"A Test\" time=\"0\">"
  83. "<failure message=\"A File(4321) : A Failure\"/>"
  84. "</test>"
  85. "</unittest-results>";
  86. CHECK_EQUAL(expected, output.str());
  87. }
  88. TEST_FIXTURE(XmlTestReporterFixture, FailureMessageIsXMLEscaped)
  89. {
  90. TestDetails const details("TestName", "suite", "filename.h", 4321);
  91. reporter.ReportTestStart(details);
  92. reporter.ReportFailure(details, "\"\'&<>");
  93. reporter.ReportTestFinish(details, 0.1f);
  94. reporter.ReportSummary(1, 1, 1, 0.1f);
  95. char const* expected =
  96. "<?xml version=\"1.0\"?>"
  97. "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"1\" time=\"0.1\">"
  98. "<test suite=\"suite\" name=\"TestName\" time=\"0.1\">"
  99. "<failure message=\"filename.h(4321) : &quot;&apos;&amp;&lt;&gt;\"/>"
  100. "</test>"
  101. "</unittest-results>";
  102. CHECK_EQUAL(expected, output.str());
  103. }
  104. TEST_FIXTURE(XmlTestReporterFixture, OneFailureAndOneSuccess)
  105. {
  106. TestDetails const failedDetails("FailedTest", "suite", "fail.h", 1);
  107. reporter.ReportTestStart(failedDetails);
  108. reporter.ReportFailure(failedDetails, "expected 1 but was 2");
  109. reporter.ReportTestFinish(failedDetails, 0.1f);
  110. TestDetails const succeededDetails("SucceededTest", "suite", "", 0);
  111. reporter.ReportTestStart(succeededDetails);
  112. reporter.ReportTestFinish(succeededDetails, 1.0f);
  113. reporter.ReportSummary(2, 1, 1, 1.1f);
  114. char const* expected =
  115. "<?xml version=\"1.0\"?>"
  116. "<unittest-results tests=\"2\" failedtests=\"1\" failures=\"1\" time=\"1.1\">"
  117. "<test suite=\"suite\" name=\"FailedTest\" time=\"0.1\">"
  118. "<failure message=\"fail.h(1) : expected 1 but was 2\"/>"
  119. "</test>"
  120. "<test suite=\"suite\" name=\"SucceededTest\" time=\"1\"/>"
  121. "</unittest-results>";
  122. CHECK_EQUAL(expected, output.str());
  123. }
  124. TEST_FIXTURE(XmlTestReporterFixture, MultipleFailures)
  125. {
  126. TestDetails const failedDetails1("FailedTest", "suite", "fail.h", 1);
  127. TestDetails const failedDetails2("FailedTest", "suite", "fail.h", 31);
  128. reporter.ReportTestStart(failedDetails1);
  129. reporter.ReportFailure(failedDetails1, "expected 1 but was 2");
  130. reporter.ReportFailure(failedDetails2, "expected one but was two");
  131. reporter.ReportTestFinish(failedDetails1, 0.1f);
  132. reporter.ReportSummary(1, 1, 2, 1.1f);
  133. char const* expected =
  134. "<?xml version=\"1.0\"?>"
  135. "<unittest-results tests=\"1\" failedtests=\"1\" failures=\"2\" time=\"1.1\">"
  136. "<test suite=\"suite\" name=\"FailedTest\" time=\"0.1\">"
  137. "<failure message=\"fail.h(1) : expected 1 but was 2\"/>"
  138. "<failure message=\"fail.h(31) : expected one but was two\"/>"
  139. "</test>"
  140. "</unittest-results>";
  141. CHECK_EQUAL(expected, output.str());
  142. }
  143. }