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 // Unit test for Google Test XML output.
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.
36 // This program will be invoked from a Python unit test. Don't run it
39 #include "gtest/gtest.h"
41 using ::testing::InitGoogleTest
;
42 using ::testing::TestEventListeners
;
43 using ::testing::TestWithParam
;
44 using ::testing::UnitTest
;
45 using ::testing::Test
;
46 using ::testing::Values
;
48 class SuccessfulTest
: public Test
{
51 TEST_F(SuccessfulTest
, Succeeds
) {
52 SUCCEED() << "This is a success.";
56 class FailedTest
: public Test
{
59 TEST_F(FailedTest
, Fails
) {
63 class DisabledTest
: public Test
{
66 TEST_F(DisabledTest
, DISABLED_test_not_run
) {
67 FAIL() << "Unexpected failure: Disabled test should not be run";
70 class SkippedTest
: public Test
{
73 TEST_F(SkippedTest
, Skipped
) {
77 TEST_F(SkippedTest
, SkippedWithMessage
) {
78 GTEST_SKIP() << "It is good practice to tell why you skip a test.";
81 TEST_F(SkippedTest
, SkippedAfterFailure
) {
83 GTEST_SKIP() << "It is good practice to tell why you skip a test.";
86 TEST(MixedResultTest
, Succeeds
) {
91 TEST(MixedResultTest
, Fails
) {
96 TEST(MixedResultTest
, DISABLED_test
) {
97 FAIL() << "Unexpected failure: Disabled test should not be run";
100 TEST(XmlQuotingTest
, OutputsCData
) {
101 FAIL() << "XML output: "
102 "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
105 // Helps to test that invalid characters produced by test code do not make
106 // it into the XML file.
107 TEST(InvalidCharactersTest
, InvalidCharactersInMessage
) {
108 FAIL() << "Invalid characters in brackets [\x1\x2]";
111 class PropertyRecordingTest
: public Test
{
113 static void SetUpTestSuite() { RecordProperty("SetUpTestSuite", "yes"); }
114 static void TearDownTestSuite() {
115 RecordProperty("TearDownTestSuite", "aye");
119 TEST_F(PropertyRecordingTest
, OneProperty
) {
120 RecordProperty("key_1", "1");
123 TEST_F(PropertyRecordingTest
, IntValuedProperty
) {
124 RecordProperty("key_int", 1);
127 TEST_F(PropertyRecordingTest
, ThreeProperties
) {
128 RecordProperty("key_1", "1");
129 RecordProperty("key_2", "2");
130 RecordProperty("key_3", "3");
133 TEST_F(PropertyRecordingTest
, TwoValuesForOneKeyUsesLastValue
) {
134 RecordProperty("key_1", "1");
135 RecordProperty("key_1", "2");
138 TEST(NoFixtureTest
, RecordProperty
) {
139 RecordProperty("key", "1");
142 void ExternalUtilityThatCallsRecordProperty(const std::string
& key
, int value
) {
143 testing::Test::RecordProperty(key
, value
);
146 void ExternalUtilityThatCallsRecordProperty(const std::string
& key
,
147 const std::string
& value
) {
148 testing::Test::RecordProperty(key
, value
);
151 TEST(NoFixtureTest
, ExternalUtilityThatCallsRecordIntValuedProperty
) {
152 ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
155 TEST(NoFixtureTest
, ExternalUtilityThatCallsRecordStringValuedProperty
) {
156 ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
159 // Verifies that the test parameter value is output in the 'value_param'
160 // XML attribute for value-parameterized tests.
161 class ValueParamTest
: public TestWithParam
<int> {};
162 TEST_P(ValueParamTest
, HasValueParamAttribute
) {}
163 TEST_P(ValueParamTest
, AnotherTestThatHasValueParamAttribute
) {}
164 INSTANTIATE_TEST_SUITE_P(Single
, ValueParamTest
, Values(33, 42));
166 #if GTEST_HAS_TYPED_TEST
167 // Verifies that the type parameter name is output in the 'type_param'
168 // XML attribute for typed tests.
169 template <typename T
> class TypedTest
: public Test
{};
170 typedef testing::Types
<int, long> TypedTestTypes
;
171 TYPED_TEST_SUITE(TypedTest
, TypedTestTypes
);
172 TYPED_TEST(TypedTest
, HasTypeParamAttribute
) {}
175 #if GTEST_HAS_TYPED_TEST_P
176 // Verifies that the type parameter name is output in the 'type_param'
177 // XML attribute for type-parameterized tests.
178 template <typename T
>
179 class TypeParameterizedTestSuite
: public Test
{};
180 TYPED_TEST_SUITE_P(TypeParameterizedTestSuite
);
181 TYPED_TEST_P(TypeParameterizedTestSuite
, HasTypeParamAttribute
) {}
182 REGISTER_TYPED_TEST_SUITE_P(TypeParameterizedTestSuite
, HasTypeParamAttribute
);
183 typedef testing::Types
<int, long> TypeParameterizedTestSuiteTypes
; // NOLINT
184 INSTANTIATE_TYPED_TEST_SUITE_P(Single
, TypeParameterizedTestSuite
,
185 TypeParameterizedTestSuiteTypes
);
188 int main(int argc
, char** argv
) {
189 InitGoogleTest(&argc
, argv
);
191 if (argc
> 1 && strcmp(argv
[1], "--shut_down_xml") == 0) {
192 TestEventListeners
& listeners
= UnitTest::GetInstance()->listeners();
193 delete listeners
.Release(listeners
.default_xml_generator());
195 testing::Test::RecordProperty("ad_hoc_property", "42");
196 return RUN_ALL_TESTS();