Wire Sysio Wire Sysion 1.0.0
Loading...
Searching...
No Matches
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
41using ::testing::InitGoogleTest;
42using ::testing::TestEventListeners;
43using ::testing::TestWithParam;
44using ::testing::UnitTest;
45using ::testing::Test;
46using ::testing::Values;
47
48class SuccessfulTest : public Test {
49};
50
52 SUCCEED() << "This is a success.";
53 ASSERT_EQ(1, 1);
54}
55
56class FailedTest : public Test {
57};
58
60 ASSERT_EQ(1, 2);
61}
62
63class DisabledTest : public Test {
64};
65
66TEST_F(DisabledTest, DISABLED_test_not_run) {
67 FAIL() << "Unexpected failure: Disabled test should not be run";
68}
69
70TEST(MixedResultTest, Succeeds) {
71 EXPECT_EQ(1, 1);
72 ASSERT_EQ(1, 1);
73}
74
75TEST(MixedResultTest, Fails) {
76 EXPECT_EQ(1, 2);
77 ASSERT_EQ(2, 3);
78}
79
80TEST(MixedResultTest, DISABLED_test) {
81 FAIL() << "Unexpected failure: Disabled test should not be run";
82}
83
84TEST(XmlQuotingTest, OutputsCData) {
85 FAIL() << "XML output: "
86 "<?xml encoding=\"utf-8\"><top><![CDATA[cdata text]]></top>";
87}
88
89// Helps to test that invalid characters produced by test code do not make
90// it into the XML file.
91TEST(InvalidCharactersTest, InvalidCharactersInMessage) {
92 FAIL() << "Invalid characters in brackets [\x1\x2]";
93}
94
96 public:
97 static void SetUpTestCase() { RecordProperty("SetUpTestCase", "yes"); }
98 static void TearDownTestCase() { RecordProperty("TearDownTestCase", "aye"); }
99};
100
102 RecordProperty("key_1", "1");
103}
104
105TEST_F(PropertyRecordingTest, IntValuedProperty) {
106 RecordProperty("key_int", 1);
107}
108
109TEST_F(PropertyRecordingTest, ThreeProperties) {
110 RecordProperty("key_1", "1");
111 RecordProperty("key_2", "2");
112 RecordProperty("key_3", "3");
113}
114
115TEST_F(PropertyRecordingTest, TwoValuesForOneKeyUsesLastValue) {
116 RecordProperty("key_1", "1");
117 RecordProperty("key_1", "2");
118}
119
120TEST(NoFixtureTest, RecordProperty) {
121 RecordProperty("key", "1");
122}
123
124void ExternalUtilityThatCallsRecordProperty(const std::string& key, int value) {
126}
127
128void ExternalUtilityThatCallsRecordProperty(const std::string& key,
129 const std::string& value) {
131}
132
133TEST(NoFixtureTest, ExternalUtilityThatCallsRecordIntValuedProperty) {
134 ExternalUtilityThatCallsRecordProperty("key_for_utility_int", 1);
135}
136
137TEST(NoFixtureTest, ExternalUtilityThatCallsRecordStringValuedProperty) {
138 ExternalUtilityThatCallsRecordProperty("key_for_utility_string", "1");
139}
140
141// Verifies that the test parameter value is output in the 'value_param'
142// XML attribute for value-parameterized tests.
143class ValueParamTest : public TestWithParam<int> {};
144TEST_P(ValueParamTest, HasValueParamAttribute) {}
145TEST_P(ValueParamTest, AnotherTestThatHasValueParamAttribute) {}
147
148#if GTEST_HAS_TYPED_TEST
149// Verifies that the type parameter name is output in the 'type_param'
150// XML attribute for typed tests.
151template <typename T> class TypedTest : public Test {};
152typedef testing::Types<int, long> TypedTestTypes;
153TYPED_TEST_CASE(TypedTest, TypedTestTypes);
154TYPED_TEST(TypedTest, HasTypeParamAttribute) {}
155#endif
156
157#if GTEST_HAS_TYPED_TEST_P
158// Verifies that the type parameter name is output in the 'type_param'
159// XML attribute for type-parameterized tests.
160template <typename T> class TypeParameterizedTestCase : public Test {};
161TYPED_TEST_CASE_P(TypeParameterizedTestCase);
162TYPED_TEST_P(TypeParameterizedTestCase, HasTypeParamAttribute) {}
163REGISTER_TYPED_TEST_CASE_P(TypeParameterizedTestCase, HasTypeParamAttribute);
164typedef testing::Types<int, long> TypeParameterizedTestCaseTypes;
166 TypeParameterizedTestCase,
167 TypeParameterizedTestCaseTypes);
168#endif
169
170int main(int argc, char** argv) {
171 InitGoogleTest(&argc, argv);
172
173 if (argc > 1 && strcmp(argv[1], "--shut_down_xml") == 0) {
174 TestEventListeners& listeners = UnitTest::GetInstance()->listeners();
175 delete listeners.Release(listeners.default_xml_generator());
176 }
177 testing::Test::RecordProperty("ad_hoc_property", "42");
178 return RUN_ALL_TESTS();
179}
static void RecordProperty(const std::string &key, const std::string &value)
Definition gtest.cc:2253
#define INSTANTIATE_TEST_CASE_P(prefix, test_case_name, generator,...)
#define TEST_P(test_case_name, test_name)
#define TEST_F(test_fixture, test_name)
Definition gtest.h:2304
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1988
#define FAIL()
Definition gtest.h:1858
#define EXPECT_EQ(val1, val2)
Definition gtest.h:1954
#define SUCCEED()
Definition gtest.h:1867
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition gtest.h:2328
#define TEST(test_case_name, test_name)
Definition gtest.h:2275
REGISTER_TYPED_TEST_CASE_P(TypeParamTest, TestA, TestB)
TYPED_TEST(TypedTest, TestA)
TYPED_TEST_CASE(TypedTest, MyTypes)
TYPED_TEST_CASE_P(TypeParamTest)
TYPED_TEST_P(TypeParamTest, TestA)
INSTANTIATE_TYPED_TEST_CASE_P(My, TypeParamTest, MyTypes)
void ExternalUtilityThatCallsRecordProperty(const std::string &key, int value)
char ** argv
#define value
Definition pkcs11.h:157