gtest_xml_output_unittest_.cc
Go to the documentation of this file.
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 
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 
39 #include "gtest/gtest.h"
40 
42 using ::testing::TestEventListeners;
43 using ::testing::TestWithParam;
44 using ::testing::UnitTest;
45 using ::testing::Test;
47 
48 class SuccessfulTest : public Test {
49 };
50 
51 TEST_F(SuccessfulTest, Succeeds) {
52  SUCCEED() << "This is a success.";
53  ASSERT_EQ(1, 1);
54 }
55 
56 class FailedTest : public Test {
57 };
58 
59 TEST_F(FailedTest, Fails) {
60  ASSERT_EQ(1, 2);
61 }
62 
63 class DisabledTest : public Test {
64 };
65 
66 TEST_F(DisabledTest, DISABLED_test_not_run) {
67  FAIL() << "Unexpected failure: Disabled test should not be run";
68 }
69 
70 class SkippedTest : public Test {
71 };
72 
73 TEST_F(SkippedTest, Skipped) {
74  GTEST_SKIP();
75 }
76 
77 TEST(MixedResultTest, Succeeds) {
78  EXPECT_EQ(1, 1);
79  ASSERT_EQ(1, 1);
80 }
81 
82 TEST(MixedResultTest, Fails) {
83  EXPECT_EQ(1, 2);
84  ASSERT_EQ(2, 3);
85 }
86 
87 TEST(MixedResultTest, DISABLED_test) {
88  FAIL() << "Unexpected failure: Disabled test should not be run";
89 }
90 
91 TEST(XmlQuotingTest, OutputsCData) {
92  FAIL() << "XML output: "
93  "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
94 }
95 
96 // Helps to test that invalid characters produced by test code do not make
97 // it into the XML file.
98 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
99  FAIL() << "Invalid characters in brackets [\x1\x2]";
100 }
101 
102 class PropertyRecordingTest : public Test {
103  public:
104  static void SetUpTestSuite() { RecordProperty("SetUpTestSuite", "yes"); }
105  static void TearDownTestSuite() {
106  RecordProperty("TearDownTestSuite", "aye");
107  }
108 };
109 
111  RecordProperty("key_1", "1");
112 }
113 
114 TEST_F(PropertyRecordingTest, IntValuedProperty) {
115  RecordProperty("key_int", 1);
116 }
117 
118 TEST_F(PropertyRecordingTest, ThreeProperties) {
119  RecordProperty("key_1", "1");
120  RecordProperty("key_2", "2");
121  RecordProperty("key_3", "3");
122 }
123 
124 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
125  RecordProperty("key_1", "1");
126  RecordProperty("key_1", "2");
127 }
128 
129 TEST(NoFixtureTest, RecordProperty) {
130  RecordProperty("key", "1");
131 }
132 
135 }
136 
138  const std::string& value) {
140 }
141 
142 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
143  ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
144 }
145 
146 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
147  ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
148 }
149 
150 // Verifies that the test parameter value is output in the 'value_param'
151 // XML attribute for value-parameterized tests.
152 class ValueParamTest : public TestWithParam<int> {};
153 TEST_P(ValueParamTest, HasValueParamAttribute) {}
154 TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
156 
157 #if GTEST_HAS_TYPED_TEST
158 // Verifies that the type parameter name is output in the 'type_param'
159 // XML attribute for typed tests.
160 template <typename T> class TypedTest : public Test {};
161 typedef testing::Types<int, long> TypedTestTypes;
162 TYPED_TEST_SUITE(TypedTest, TypedTestTypes);
163 TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
164 #endif
165 
166 #if GTEST_HAS_TYPED_TEST_P
167 // Verifies that the type parameter name is output in the 'type_param'
168 // XML attribute for type-parameterized tests.
169 template <typename T>
170 class TypeParameterizedTestSuite : public Test {};
171 TYPED_TEST_SUITE_P(TypeParameterizedTestSuite);
172 TYPED_TEST_P(TypeParameterizedTestSuite, HasTypeParamAttribute) {}
173 REGISTER_TYPED_TEST_SUITE_P(TypeParameterizedTestSuite, HasTypeParamAttribute);
174 typedef testing::Types<int, long> TypeParameterizedTestSuiteTypes; // NOLINT
175 INSTANTIATE_TYPED_TEST_SUITE_P(Single, TypeParameterizedTestSuite,
176  TypeParameterizedTestSuiteTypes);
177 #endif
178 
179 int main(int argc, char** argv) {
180  InitGoogleTest(&argc, argv);
181 
182  if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
183  TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
184  delete listeners.Release(listeners.default_xml_generator());
185  }
186  testing::Test::RecordProperty("ad_hoc_property", "42");
187  return RUN_ALL_TESTS();
188 }
REGISTER_TYPED_TEST_SUITE_P
REGISTER_TYPED_TEST_SUITE_P(TypeParamTest, TestA, TestB)
ExternalUtilityThatCallsRecordProperty
void ExternalUtilityThatCallsRecordProperty(const std::string &key, int value)
Definition: gtest_xml_output_unittest_.cc:133
TEST
TEST(MixedResultTest, Succeeds)
Definition: gtest_xml_output_unittest_.cc:77
FAIL
#define FAIL()
Definition: gtest.h:1952
TEST_F
TEST_F(SuccessfulTest, Succeeds)
Definition: gtest_xml_output_unittest_.cc:51
gtest.h
EXPECT_EQ
#define EXPECT_EQ(val1, val2)
Definition: glog/src/googletest.h:155
InitGoogleTest
_START_GOOGLE_NAMESPACE_ void InitGoogleTest(int *, char **)
Definition: glog/src/googletest.h:124
testing::Test::RecordProperty
static void RecordProperty(const std::string &key, const std::string &value)
Definition: gtest.cc:2259
TYPED_TEST_SUITE_P
TYPED_TEST_SUITE_P(TypeParamTest)
string
GLsizei const GLchar *const * string
Definition: glcorearb.h:3083
PropertyRecordingTest
Definition: gtest_xml_output_unittest_.cc:102
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:2082
testing::Test::Test
Test()
Definition: gtest.cc:2236
ValueParamTest
Definition: googletest-list-tests-unittest_.cc:93
INSTANTIATE_TEST_SUITE_P
INSTANTIATE_TEST_SUITE_P(Single, ValueParamTest, Values(33, 42))
GTEST_SKIP
#define GTEST_SKIP()
Definition: gtest.h:1918
PropertyRecordingTest::TearDownTestSuite
static void TearDownTestSuite()
Definition: gtest_xml_output_unittest_.cc:105
TYPED_TEST
TYPED_TEST(TypedTest, TestA)
Definition: googletest-list-tests-unittest_.cc:128
SkippedTest
Definition: gtest_xml_output_unittest_.cc:70
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2502
TEST_P
TEST_P(ValueParamTest, HasValueParamAttribute)
Definition: gtest_xml_output_unittest_.cc:153
main
int main(int argc, char **argv)
Definition: gtest_xml_output_unittest_.cc:179
PropertyRecordingTest::SetUpTestSuite
static void SetUpTestSuite()
Definition: gtest_xml_output_unittest_.cc:104
TypedTest
Definition: googletest-list-tests-unittest_.cc:116
INSTANTIATE_TYPED_TEST_SUITE_P
INSTANTIATE_TYPED_TEST_SUITE_P(My, TypeParamTest, MyTypes)
key
const SETUP_TEARDOWN_TESTCONTEXT char * key
Definition: test_wss_transport.cpp:10
TYPED_TEST_SUITE
TYPED_TEST_SUITE(TypedTest, MyTypes)
FailedTest
Definition: gtest_xml_output_unittest_.cc:56
DisabledTest
Definition: gtest_xml_output_unittest_.cc:63
google::protobuf::python::descriptor::Values
static PyObject * Values(PyContainer *self, PyObject *args)
Definition: descriptor_containers.cc:489
TYPED_TEST_P
TYPED_TEST_P(TypeParamTest, TestA)
Definition: googletest-list-tests-unittest_.cc:142
SUCCEED
#define SUCCEED()
Definition: gtest.h:1961
value
GLsizei const GLfloat * value
Definition: glcorearb.h:3093
SuccessfulTest
Definition: gtest_xml_output_unittest_.cc:48


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:54