1 // Copyright 2006, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
30 // Author: eefacm@gmail.com (Sean Mcafee)
32 // Unit test for Google Test XML output.
34 // A user can specify XML output in a Google Test program to run via
35 // either the GTEST_OUTPUT environment variable or the --gtest_output
36 // flag. This is used for testing such functionality.
38 // This program will be invoked from a Python unit test. Don't run it
41 #include "gtest/gtest.h"
43 using ::testing::InitGoogleTest;
44 using ::testing::TestEventListeners;
45 using ::testing::TestWithParam;
46 using ::testing::UnitTest;
47 using ::testing::Test;
48 using ::testing::Values;
50 class SuccessfulTest : public Test {
53 TEST_F(SuccessfulTest, Succeeds) {
54 SUCCEED() << "This is a success.";
58 class FailedTest : public Test {
61 TEST_F(FailedTest, Fails) {
65 class DisabledTest : public Test {
68 TEST_F(DisabledTest, DISABLED_test_not_run) {
69 FAIL() << "Unexpected failure: Disabled test should not be run";
72 TEST(MixedResultTest, Succeeds) {
77 TEST(MixedResultTest, Fails) {
82 TEST(MixedResultTest, DISABLED_test) {
83 FAIL() << "Unexpected failure: Disabled test should not be run";
86 TEST(XmlQuotingTest, OutputsCData) {
87 FAIL() << "XML output: "
88 "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
91 // Helps to test that invalid characters produced by test code do not make
92 // it into the XML file.
93 TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
94 FAIL() << "Invalid characters in brackets [\x1\x2]";
97 class PropertyRecordingTest : public Test {
99 static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
100 static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
103 TEST_F(PropertyRecordingTest, OneProperty) {
104 RecordProperty("key_1", "1");
107 TEST_F(PropertyRecordingTest, IntValuedProperty) {
108 RecordProperty("key_int", 1);
111 TEST_F(PropertyRecordingTest, ThreeProperties) {
112 RecordProperty("key_1", "1");
113 RecordProperty("key_2", "2");
114 RecordProperty("key_3", "3");
117 TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
118 RecordProperty("key_1", "1");
119 RecordProperty("key_1", "2");
122 TEST(NoFixtureTest, RecordProperty) {
123 RecordProperty("key", "1");
126 void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
127 testing::Test::RecordProperty(key, value);
130 void ExternalUtilityThatCallsRecordProperty(const std::string& key,
131 const std::string& value) {
132 testing::Test::RecordProperty(key, value);
135 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
136 ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
139 TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
140 ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
143 // Verifies that the test parameter value is output in the 'value_param'
144 // XML attribute for value-parameterized tests.
145 class ValueParamTest : public TestWithParam<int> {};
146 TEST_P(ValueParamTest, HasValueParamAttribute) {}
147 TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
148 INSTANTIATE_TEST_CASE_P(Single, ValueParamTest, Values(33, 42));
150 #if GTEST_HAS_TYPED_TEST
151 // Verifies that the type parameter name is output in the 'type_param'
152 // XML attribute for typed tests.
153 template <typename T> class TypedTest : public Test {};
154 typedef testing::Types<int, long> TypedTestTypes;
155 TYPED_TEST_CASE(TypedTest, TypedTestTypes);
156 TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
159 #if GTEST_HAS_TYPED_TEST_P
160 // Verifies that the type parameter name is output in the 'type_param'
161 // XML attribute for type-parameterized tests.
162 template <typename T> class TypeParameterizedTestCase : public Test {};
163 TYPED_TEST_CASE_P(TypeParameterizedTestCase);
164 TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
165 REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
166 typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
167 INSTANTIATE_TYPED_TEST_CASE_P(Single,
168 TypeParameterizedTestCase,
169 TypeParameterizedTestCaseTypes);
172 int main(int argc, char** argv) {
173 InitGoogleTest(&argc, argv);
175 if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
176 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
177 delete listeners.Release(listeners.default_xml_generator());
179 testing::Test::RecordProperty("ad_hoc_property", "42");
180 return RUN_ALL_TESTS();