gtest_xml_output_unittest_.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. TEST(MixedResultTest, Succeeds) {
  61. EXPECT_EQ(1, 1);
  62. ASSERT_EQ(1, 1);
  63. }
  64. TEST(MixedResultTest, Fails) {
  65. EXPECT_EQ(1, 2);
  66. ASSERT_EQ(2, 3);
  67. }
  68. TEST(MixedResultTest, DISABLED_test) {
  69. FAIL() << "Unexpected failure: Disabled test should not be run";
  70. }
  71. TEST(XmlQuotingTest, OutputsCData) {
  72. FAIL() << "XML output: "
  73. "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
  74. }
  75. // Helps to test that invalid characters produced by test code do not make
  76. // it into the XML file.
  77. TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
  78. FAIL() << "Invalid characters in brackets [\x1\x2]";
  79. }
  80. class PropertyRecordingTest : public Test {
  81. public:
  82. static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
  83. static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
  84. };
  85. TEST_F(PropertyRecordingTest, OneProperty) {
  86. RecordProperty("key_1", "1");
  87. }
  88. TEST_F(PropertyRecordingTest, IntValuedProperty) {
  89. RecordProperty("key_int", 1);
  90. }
  91. TEST_F(PropertyRecordingTest, ThreeProperties) {
  92. RecordProperty("key_1", "1");
  93. RecordProperty("key_2", "2");
  94. RecordProperty("key_3", "3");
  95. }
  96. TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
  97. RecordProperty("key_1", "1");
  98. RecordProperty("key_1", "2");
  99. }
  100. TEST(NoFixtureTest, RecordProperty) {
  101. RecordProperty("key", "1");
  102. }
  103. void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
  104. testing::Test::RecordProperty(key, value);
  105. }
  106. void ExternalUtilityThatCallsRecordProperty(const std::string& key,
  107. const std::string& value) {
  108. testing::Test::RecordProperty(key, value);
  109. }
  110. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
  111. ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
  112. }
  113. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
  114. ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
  115. }
  116. // Verifies that the test parameter value is output in the 'value_param'
  117. // XML attribute for value-parameterized tests.
  118. class ValueParamTest : public TestWithParam<int> {};
  119. TEST_P(ValueParamTest, HasValueParamAttribute) {}
  120. TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
  121. INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
  122. #if GTEST_HAS_TYPED_TEST
  123. // Verifies that the type parameter name is output in the 'type_param'
  124. // XML attribute for typed tests.
  125. template <typename T> class TypedTest : public Test {};
  126. typedef testing::Types<int, long> TypedTestTypes;
  127. TYPED_TEST_CASE(TypedTest, TypedTestTypes);
  128. TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
  129. #endif
  130. #if GTEST_HAS_TYPED_TEST_P
  131. // Verifies that the type parameter name is output in the 'type_param'
  132. // XML attribute for type-parameterized tests.
  133. template <typename T> class TypeParameterizedTestCase : public Test {};
  134. TYPED_TEST_CASE_P(TypeParameterizedTestCase);
  135. TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
  136. REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
  137. typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
  138. INSTANTIATE_TYPED_TEST_CASE_P(Single,
  139. TypeParameterizedTestCase,
  140. TypeParameterizedTestCaseTypes);
  141. #endif
  142. int main(int argc, char** argv) {
  143. InitGoogleTest(&argc, argv);
  144. if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
  145. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  146. delete listeners.Release(listeners.default_xml_generator());
  147. }
  148. testing::Test::RecordProperty("ad_hoc_property", "42");
  149. return RUN_ALL_TESTS();
  150. }