gtest_xml_output_unittest_.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Unit test for Google Test XML output.
  30. //
  31. // A user can specify XML output in a Google Test program to run via
  32. // either the GTEST_OUTPUT environment variable or the --gtest_output
  33. // flag. This is used for testing such functionality.
  34. //
  35. // This program will be invoked from a Python unit test. Don't run it
  36. // directly.
  37. #include "gtest/gtest.h"
  38. using ::testing::InitGoogleTest;
  39. using ::testing::TestEventListeners;
  40. using ::testing::TestWithParam;
  41. using ::testing::UnitTest;
  42. using ::testing::Test;
  43. using ::testing::Values;
  44. class SuccessfulTest : public Test {
  45. };
  46. TEST_F(SuccessfulTest, Succeeds) {
  47. SUCCEED() << "This is a success.";
  48. ASSERT_EQ(1, 1);
  49. }
  50. class FailedTest : public Test {
  51. };
  52. TEST_F(FailedTest, Fails) {
  53. ASSERT_EQ(1, 2);
  54. }
  55. class DisabledTest : public Test {
  56. };
  57. TEST_F(DisabledTest, DISABLED_test_not_run) {
  58. FAIL() << "Unexpected failure: Disabled test should not be run";
  59. }
  60. class SkippedTest : public Test {
  61. };
  62. TEST_F(SkippedTest, Skipped) {
  63. GTEST_SKIP();
  64. }
  65. TEST_F(SkippedTest, SkippedWithMessage) {
  66. GTEST_SKIP() << "It is good practice to tell why you skip a test.";
  67. }
  68. TEST_F(SkippedTest, SkippedAfterFailure) {
  69. EXPECT_EQ(1, 2);
  70. GTEST_SKIP() << "It is good practice to tell why you skip a test.";
  71. }
  72. TEST(MixedResultTest, Succeeds) {
  73. EXPECT_EQ(1, 1);
  74. ASSERT_EQ(1, 1);
  75. }
  76. TEST(MixedResultTest, Fails) {
  77. EXPECT_EQ(1, 2);
  78. ASSERT_EQ(2, 3);
  79. }
  80. TEST(MixedResultTest, DISABLED_test) {
  81. FAIL() << "Unexpected failure: Disabled test should not be run";
  82. }
  83. TEST(XmlQuotingTest, OutputsCData) {
  84. FAIL() << "XML output: "
  85. "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
  86. }
  87. // Helps to test that invalid characters produced by test code do not make
  88. // it into the XML file.
  89. TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
  90. FAIL() << "Invalid characters in brackets [\x1\x2]";
  91. }
  92. class PropertyRecordingTest : public Test {
  93. public:
  94. static void SetUpTestSuite() { RecordProperty("SetUpTestSuite", "yes"); }
  95. static void TearDownTestSuite() {
  96. RecordProperty("TearDownTestSuite", "aye");
  97. }
  98. };
  99. TEST_F(PropertyRecordingTest, OneProperty) {
  100. RecordProperty("key_1", "1");
  101. }
  102. TEST_F(PropertyRecordingTest, IntValuedProperty) {
  103. RecordProperty("key_int", 1);
  104. }
  105. TEST_F(PropertyRecordingTest, ThreeProperties) {
  106. RecordProperty("key_1", "1");
  107. RecordProperty("key_2", "2");
  108. RecordProperty("key_3", "3");
  109. }
  110. TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
  111. RecordProperty("key_1", "1");
  112. RecordProperty("key_1", "2");
  113. }
  114. TEST(NoFixtureTest, RecordProperty) {
  115. RecordProperty("key", "1");
  116. }
  117. void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
  118. testing::Test::RecordProperty(key, value);
  119. }
  120. void ExternalUtilityThatCallsRecordProperty(const std::string& key,
  121. const std::string& value) {
  122. testing::Test::RecordProperty(key, value);
  123. }
  124. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
  125. ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
  126. }
  127. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
  128. ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
  129. }
  130. // Verifies that the test parameter value is output in the 'value_param'
  131. // XML attribute for value-parameterized tests.
  132. class ValueParamTest : public TestWithParam<int> {};
  133. TEST_P(ValueParamTest, HasValueParamAttribute) {}
  134. TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
  135. INSTANTIATE_TEST_SUITE_P(Single, ValueParamTest, Values(33, 42));
  136. // Verifies that the type parameter name is output in the 'type_param'
  137. // XML attribute for typed tests.
  138. template <typename T> class TypedTest : public Test {};
  139. typedef testing::Types<int, long> TypedTestTypes;
  140. TYPED_TEST_SUITE(TypedTest, TypedTestTypes);
  141. TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
  142. // Verifies that the type parameter name is output in the 'type_param'
  143. // XML attribute for type-parameterized tests.
  144. template <typename T>
  145. class TypeParameterizedTestSuite : public Test {};
  146. TYPED_TEST_SUITE_P(TypeParameterizedTestSuite);
  147. TYPED_TEST_P(TypeParameterizedTestSuite, HasTypeParamAttribute) {}
  148. REGISTER_TYPED_TEST_SUITE_P(TypeParameterizedTestSuite, HasTypeParamAttribute);
  149. typedef testing::Types<int, long> TypeParameterizedTestSuiteTypes; // NOLINT
  150. INSTANTIATE_TYPED_TEST_SUITE_P(Single, TypeParameterizedTestSuite,
  151. TypeParameterizedTestSuiteTypes);
  152. int main(int argc, char** argv) {
  153. InitGoogleTest(&argc, argv);
  154. if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
  155. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  156. delete listeners.Release(listeners.default_xml_generator());
  157. }
  158. testing::Test::RecordProperty("ad_hoc_property", "42");
  159. return RUN_ALL_TESTS();
  160. }