gtest_xml_output_unittest_.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // Author: eefacm@gmail.com (Sean Mcafee)
  30. // Unit test for Google Test XML output.
  31. //
  32. // A user can specify XML output in a Google Test program to run via
  33. // either the GTEST_OUTPUT environment variable or the --gtest_output
  34. // flag. This is used for testing such functionality.
  35. //
  36. // This program will be invoked from a Python unit test. Don't run it
  37. // directly.
  38. #include "gtest/gtest.h"
  39. using ::testing::InitGoogleTest;
  40. using ::testing::TestEventListeners;
  41. using ::testing::TestWithParam;
  42. using ::testing::UnitTest;
  43. using ::testing::Test;
  44. using ::testing::Values;
  45. class SuccessfulTest : public Test {
  46. };
  47. TEST_F(SuccessfulTest, Succeeds) {
  48. SUCCEED() << "This is a success.";
  49. ASSERT_EQ(1, 1);
  50. }
  51. class FailedTest : public Test {
  52. };
  53. TEST_F(FailedTest, Fails) {
  54. ASSERT_EQ(1, 2);
  55. }
  56. class DisabledTest : public Test {
  57. };
  58. TEST_F(DisabledTest, DISABLED_test_not_run) {
  59. FAIL() << "Unexpected failure: Disabled test should not be run";
  60. }
  61. TEST(MixedResultTest, Succeeds) {
  62. EXPECT_EQ(1, 1);
  63. ASSERT_EQ(1, 1);
  64. }
  65. TEST(MixedResultTest, Fails) {
  66. EXPECT_EQ(1, 2);
  67. ASSERT_EQ(2, 3);
  68. }
  69. TEST(MixedResultTest, DISABLED_test) {
  70. FAIL() << "Unexpected failure: Disabled test should not be run";
  71. }
  72. TEST(XmlQuotingTest, OutputsCData) {
  73. FAIL() << "XML output: "
  74. "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
  75. }
  76. // Helps to test that invalid characters produced by test code do not make
  77. // it into the XML file.
  78. TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
  79. FAIL() << "Invalid characters in brackets [\x1\x2]";
  80. }
  81. class PropertyRecordingTest : public Test {
  82. public:
  83. static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
  84. static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
  85. };
  86. TEST_F(PropertyRecordingTest, OneProperty) {
  87. RecordProperty("key_1", "1");
  88. }
  89. TEST_F(PropertyRecordingTest, IntValuedProperty) {
  90. RecordProperty("key_int", 1);
  91. }
  92. TEST_F(PropertyRecordingTest, ThreeProperties) {
  93. RecordProperty("key_1", "1");
  94. RecordProperty("key_2", "2");
  95. RecordProperty("key_3", "3");
  96. }
  97. TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
  98. RecordProperty("key_1", "1");
  99. RecordProperty("key_1", "2");
  100. }
  101. TEST(NoFixtureTest, RecordProperty) {
  102. RecordProperty("key", "1");
  103. }
  104. void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
  105. testing::Test::RecordProperty(key, value);
  106. }
  107. void ExternalUtilityThatCallsRecordProperty(const std::string& key,
  108. const std::string& value) {
  109. testing::Test::RecordProperty(key, value);
  110. }
  111. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
  112. ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
  113. }
  114. TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
  115. ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
  116. }
  117. // Verifies that the test parameter value is output in the 'value_param'
  118. // XML attribute for value-parameterized tests.
  119. class ValueParamTest : public TestWithParam<int> {};
  120. TEST_P(ValueParamTest, HasValueParamAttribute) {}
  121. TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
  122. INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
  123. #if GTEST_HAS_TYPED_TEST
  124. // Verifies that the type parameter name is output in the 'type_param'
  125. // XML attribute for typed tests.
  126. template <typename T> class TypedTest : public Test {};
  127. typedef testing::Types<int, long> TypedTestTypes;
  128. TYPED_TEST_CASE(TypedTest, TypedTestTypes);
  129. TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
  130. #endif
  131. #if GTEST_HAS_TYPED_TEST_P
  132. // Verifies that the type parameter name is output in the 'type_param'
  133. // XML attribute for type-parameterized tests.
  134. template <typename T> class TypeParameterizedTestCase : public Test {};
  135. TYPED_TEST_CASE_P(TypeParameterizedTestCase);
  136. TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
  137. REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
  138. typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
  139. INSTANTIATE_TYPED_TEST_CASE_P(Single,
  140. TypeParameterizedTestCase,
  141. TypeParameterizedTestCaseTypes);
  142. #endif
  143. int main(int argc, char** argv) {
  144. InitGoogleTest(&argc, argv);
  145. if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
  146. TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
  147. delete listeners.Release(listeners.default_xml_generator());
  148. }
  149. testing::Test::RecordProperty("ad_hoc_property", "42");
  150. return RUN_ALL_TESTS();
  151. }