Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 // clang-format off
39 
40 #include <string>
41 
42 #include "gtest/gtest.h"
43 
45 using ::testing::Test;
46 using ::testing::TestEventListeners;
47 using ::testing::TestWithParam;
48 using ::testing::UnitTest;
50 
51 class SuccessfulTest : public Test {};
52 
53 TEST_F(SuccessfulTest, Succeeds) {
54  SUCCEED() << "This is a success.";
55  ASSERT_EQ(1, 1);
56 }
57 
58 class FailedTest : public Test {
59 };
60 
61 TEST_F(FailedTest, Fails) {
62  ASSERT_EQ(1, 2);
63 }
64 
65 class DisabledTest : public Test {
66 };
67 
68 TEST_F(DisabledTest, DISABLED_test_not_run) {
69  FAIL() << "Unexpected failure: Disabled test should not be run";
70 }
71 
72 class SkippedTest : public Test {
73 };
74 
75 TEST_F(SkippedTest, Skipped) {
76  GTEST_SKIP();
77 }
78 
79 TEST_F(SkippedTest, SkippedWithMessage) {
80  GTEST_SKIP() << "It is good practice to tell why you skip a test.";
81 }
82 
83 TEST_F(SkippedTest, SkippedAfterFailure) {
84  EXPECT_EQ(1, 2);
85  GTEST_SKIP() << "It is good practice to tell why you skip a test.";
86 }
87 
88 TEST(MixedResultTest, Succeeds) {
89  EXPECT_EQ(1, 1);
90  ASSERT_EQ(1, 1);
91 }
92 
93 TEST(MixedResultTest, Fails) {
94  EXPECT_EQ(1, 2);
95  ASSERT_EQ(2, 3);
96 }
97 
98 TEST(MixedResultTest, DISABLED_test) {
99  FAIL() << "Unexpected failure: Disabled test should not be run";
100 }
101 
102 TEST(XmlQuotingTest, OutputsCData) {
103  FAIL() << "XML output: "
104  "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
105 }
106 
107 // Helps to test that invalid characters produced by test code do not make
108 // it into the XML file.
109 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
110  FAIL() << "Invalid characters in brackets [\x1\x2]";
111 }
112 
113 class PropertyRecordingTest : public Test {
114  public:
115  static void SetUpTestSuite() {
116  RecordProperty("SetUpTestSuite (with whitespace)", "yes and yes");
117  RecordProperty("SetUpTestSuite", "yes");
118  }
119  static void TearDownTestSuite() {
120  RecordProperty("TearDownTestSuite (with whitespace)", "aye and aye");
121  RecordProperty("TearDownTestSuite", "aye");
122  }
123 };
124 
126  RecordProperty("key_1", "1");
127 }
128 
129 TEST_F(PropertyRecordingTest, IntValuedProperty) {
130  RecordProperty("key_int", 1);
131 }
132 
133 TEST_F(PropertyRecordingTest, ThreeProperties) {
134  RecordProperty("key_1", "1");
135  RecordProperty("key_2", "2");
136  RecordProperty("key_3", "3");
137 }
138 
139 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
140  RecordProperty("key_1", "1");
141  RecordProperty("key_1", "2");
142 }
143 
144 TEST(NoFixtureTest, RecordProperty) {
145  RecordProperty("key", "1");
146 }
147 
148 void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
149  testing::Test::RecordProperty(key, value);
150 }
151 
152 void ExternalUtilityThatCallsRecordProperty(const std::string& key,
153  const std::string& value) {
154  testing::Test::RecordProperty(key, value);
155 }
156 
157 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
158  ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
159 }
160 
161 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
162  ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
163 }
164 
165 // Ensures that SetUpTestSuite and TearDownTestSuite failures are reported in
166 // the XML output.
167 class SetupFailTest : public ::testing::Test {
168  protected:
169  static void SetUpTestSuite() { ASSERT_EQ(1, 2); }
170 };
171 
172 TEST_F(SetupFailTest, NoopPassingTest) {}
173 
174 class TearDownFailTest : public ::testing::Test {
175  protected:
176  static void TearDownTestSuite() { ASSERT_EQ(1, 2); }
177 };
178 
179 TEST_F(TearDownFailTest, NoopPassingTest) {}
180 
181 // Verifies that the test parameter value is output in the 'value_param'
182 // XML attribute for value-parameterized tests.
183 class ValueParamTest : public TestWithParam<int> {};
184 TEST_P(ValueParamTest, HasValueParamAttribute) {}
185 TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
187 
188 // Verifies that the type parameter name is output in the 'type_param'
189 // XML attribute for typed tests.
190 template <typename T> class TypedTest : public Test {};
192 TYPED_TEST_SUITE(TypedTest, TypedTestTypes);
193 TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
194 
195 // Verifies that the type parameter name is output in the 'type_param'
196 // XML attribute for type-parameterized tests.
197 template <typename T>
198 class TypeParameterizedTestSuite : public Test {};
200 TYPED_TEST_P(TypeParameterizedTestSuite, HasTypeParamAttribute) {}
204  TypeParameterizedTestSuiteTypes);
205 
206 int main(int argc, char** argv) {
207  InitGoogleTest(&argc, argv);
208 
209  if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
210  TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
211  delete listeners.Release(listeners.default_xml_generator());
212  }
213  testing::Test::RecordProperty("ad_hoc_property", "42");
214  return RUN_ALL_TESTS();
215 }
216 
217 // clang-format on
#define TYPED_TEST_P(SuiteName, TestName)
internal::ValueArray< T...> Values(T...v)
#define TEST_F(test_fixture, test_name)
Definition: gtest.h:2224
testing::Types< int, bool > TypeParameterizedTestSuiteTypes
#define TEST(test_suite_name, test_name)
Definition: gtest.h:2192
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1914
testing::Types< int, bool > TypedTestTypes
#define TYPED_TEST_SUITE(CaseName, Types,...)
#define INSTANTIATE_TYPED_TEST_SUITE_P(Prefix, SuiteName, Types,...)
int main()
Definition: ad_example.cpp:171
int value
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1884
#define TEST_P(test_suite_name, test_name)
#define TYPED_TEST(CaseName, TestName)
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: gtest.h:2334
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: gtest.cc:6885
void ExternalUtilityThatCallsRecordProperty(const std::string &key, int value)
#define TYPED_TEST_SUITE_P(SuiteName)
#define REGISTER_TYPED_TEST_SUITE_P(SuiteName,...)
#define GTEST_SKIP()
Definition: gtest.h:1730
static void RecordProperty(const std::string &key, const std::string &value)
Definition: gtest.cc:2520
#define FAIL()
Definition: gtest.h:1769
#define SUCCEED()
Definition: gtest.h:1779
#define INSTANTIATE_TEST_SUITE_P(prefix, test_suite_name,...)