1 // Copyright 2009 Google Inc. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 // Author: vladl@google.com (Vlad Losev)
31 // The Google C++ Testing Framework (Google Test)
33 // This file verifies Google Test event listeners receive events at the
36 #include "gtest/gtest.h"
39 using ::testing::AddGlobalTestEnvironment
;
40 using ::testing::Environment
;
41 using ::testing::InitGoogleTest
;
42 using ::testing::Test
;
43 using ::testing::TestCase
;
44 using ::testing::TestEventListener
;
45 using ::testing::TestInfo
;
46 using ::testing::TestPartResult
;
47 using ::testing::UnitTest
;
49 // Used by tests to register their events.
50 std::vector
<std::string
>* g_events
= NULL
;
55 class EventRecordingListener
: public TestEventListener
{
57 explicit EventRecordingListener(const char* name
) : name_(name
) {}
60 virtual void OnTestProgramStart(const UnitTest
& /*unit_test*/) {
61 g_events
->push_back(GetFullMethodName("OnTestProgramStart"));
64 virtual void OnTestIterationStart(const UnitTest
& /*unit_test*/,
67 message
<< GetFullMethodName("OnTestIterationStart")
68 << "(" << iteration
<< ")";
69 g_events
->push_back(message
.GetString());
72 virtual void OnEnvironmentsSetUpStart(const UnitTest
& /*unit_test*/) {
73 g_events
->push_back(GetFullMethodName("OnEnvironmentsSetUpStart"));
76 virtual void OnEnvironmentsSetUpEnd(const UnitTest
& /*unit_test*/) {
77 g_events
->push_back(GetFullMethodName("OnEnvironmentsSetUpEnd"));
80 virtual void OnTestCaseStart(const TestCase
& /*test_case*/) {
81 g_events
->push_back(GetFullMethodName("OnTestCaseStart"));
84 virtual void OnTestStart(const TestInfo
& /*test_info*/) {
85 g_events
->push_back(GetFullMethodName("OnTestStart"));
88 virtual void OnTestPartResult(const TestPartResult
& /*test_part_result*/) {
89 g_events
->push_back(GetFullMethodName("OnTestPartResult"));
92 virtual void OnTestEnd(const TestInfo
& /*test_info*/) {
93 g_events
->push_back(GetFullMethodName("OnTestEnd"));
96 virtual void OnTestCaseEnd(const TestCase
& /*test_case*/) {
97 g_events
->push_back(GetFullMethodName("OnTestCaseEnd"));
100 virtual void OnEnvironmentsTearDownStart(const UnitTest
& /*unit_test*/) {
101 g_events
->push_back(GetFullMethodName("OnEnvironmentsTearDownStart"));
104 virtual void OnEnvironmentsTearDownEnd(const UnitTest
& /*unit_test*/) {
105 g_events
->push_back(GetFullMethodName("OnEnvironmentsTearDownEnd"));
108 virtual void OnTestIterationEnd(const UnitTest
& /*unit_test*/,
111 message
<< GetFullMethodName("OnTestIterationEnd")
112 << "(" << iteration
<< ")";
113 g_events
->push_back(message
.GetString());
116 virtual void OnTestProgramEnd(const UnitTest
& /*unit_test*/) {
117 g_events
->push_back(GetFullMethodName("OnTestProgramEnd"));
121 std::string
GetFullMethodName(const char* name
) {
122 return name_
+ "." + name
;
128 class EnvironmentInvocationCatcher
: public Environment
{
130 virtual void SetUp() {
131 g_events
->push_back("Environment::SetUp");
134 virtual void TearDown() {
135 g_events
->push_back("Environment::TearDown");
139 class ListenerTest
: public Test
{
141 static void SetUpTestCase() {
142 g_events
->push_back("ListenerTest::SetUpTestCase");
145 static void TearDownTestCase() {
146 g_events
->push_back("ListenerTest::TearDownTestCase");
149 virtual void SetUp() {
150 g_events
->push_back("ListenerTest::SetUp");
153 virtual void TearDown() {
154 g_events
->push_back("ListenerTest::TearDown");
158 TEST_F(ListenerTest
, DoesFoo
) {
159 // Test execution order within a test case is not guaranteed so we are not
160 // recording the test name.
161 g_events
->push_back("ListenerTest::* Test Body");
162 SUCCEED(); // Triggers OnTestPartResult.
165 TEST_F(ListenerTest
, DoesBar
) {
166 g_events
->push_back("ListenerTest::* Test Body");
167 SUCCEED(); // Triggers OnTestPartResult.
170 } // namespace internal
172 } // namespace testing
174 using ::testing::internal::EnvironmentInvocationCatcher
;
175 using ::testing::internal::EventRecordingListener
;
177 void VerifyResults(const std::vector
<std::string
>& data
,
178 const char* const* expected_data
,
179 int expected_data_size
) {
180 const int actual_size
= data
.size();
181 // If the following assertion fails, a new entry will be appended to
182 // data. Hence we save data.size() first.
183 EXPECT_EQ(expected_data_size
, actual_size
);
185 // Compares the common prefix.
186 const int shorter_size
= expected_data_size
<= actual_size
?
187 expected_data_size
: actual_size
;
189 for (; i
< shorter_size
; ++i
) {
190 ASSERT_STREQ(expected_data
[i
], data
[i
].c_str())
191 << "at position " << i
;
194 // Prints extra elements in the actual data.
195 for (; i
< actual_size
; ++i
) {
196 printf(" Actual event #%d: %s\n", i
, data
[i
].c_str());
200 int main(int argc
, char **argv
) {
201 std::vector
<std::string
> events
;
203 InitGoogleTest(&argc
, argv
);
205 UnitTest::GetInstance()->listeners().Append(
206 new EventRecordingListener("1st"));
207 UnitTest::GetInstance()->listeners().Append(
208 new EventRecordingListener("2nd"));
210 AddGlobalTestEnvironment(new EnvironmentInvocationCatcher
);
212 GTEST_CHECK_(events
.size() == 0)
213 << "AddGlobalTestEnvironment should not generate any events itself.";
215 ::testing::GTEST_FLAG(repeat
) = 2;
216 int ret_val
= RUN_ALL_TESTS();
218 const char* const expected_events
[] = {
219 "1st.OnTestProgramStart",
220 "2nd.OnTestProgramStart",
221 "1st.OnTestIterationStart(0)",
222 "2nd.OnTestIterationStart(0)",
223 "1st.OnEnvironmentsSetUpStart",
224 "2nd.OnEnvironmentsSetUpStart",
225 "Environment::SetUp",
226 "2nd.OnEnvironmentsSetUpEnd",
227 "1st.OnEnvironmentsSetUpEnd",
228 "1st.OnTestCaseStart",
229 "2nd.OnTestCaseStart",
230 "ListenerTest::SetUpTestCase",
233 "ListenerTest::SetUp",
234 "ListenerTest::* Test Body",
235 "1st.OnTestPartResult",
236 "2nd.OnTestPartResult",
237 "ListenerTest::TearDown",
242 "ListenerTest::SetUp",
243 "ListenerTest::* Test Body",
244 "1st.OnTestPartResult",
245 "2nd.OnTestPartResult",
246 "ListenerTest::TearDown",
249 "ListenerTest::TearDownTestCase",
252 "1st.OnEnvironmentsTearDownStart",
253 "2nd.OnEnvironmentsTearDownStart",
254 "Environment::TearDown",
255 "2nd.OnEnvironmentsTearDownEnd",
256 "1st.OnEnvironmentsTearDownEnd",
257 "2nd.OnTestIterationEnd(0)",
258 "1st.OnTestIterationEnd(0)",
259 "1st.OnTestIterationStart(1)",
260 "2nd.OnTestIterationStart(1)",
261 "1st.OnEnvironmentsSetUpStart",
262 "2nd.OnEnvironmentsSetUpStart",
263 "Environment::SetUp",
264 "2nd.OnEnvironmentsSetUpEnd",
265 "1st.OnEnvironmentsSetUpEnd",
266 "1st.OnTestCaseStart",
267 "2nd.OnTestCaseStart",
268 "ListenerTest::SetUpTestCase",
271 "ListenerTest::SetUp",
272 "ListenerTest::* Test Body",
273 "1st.OnTestPartResult",
274 "2nd.OnTestPartResult",
275 "ListenerTest::TearDown",
280 "ListenerTest::SetUp",
281 "ListenerTest::* Test Body",
282 "1st.OnTestPartResult",
283 "2nd.OnTestPartResult",
284 "ListenerTest::TearDown",
287 "ListenerTest::TearDownTestCase",
290 "1st.OnEnvironmentsTearDownStart",
291 "2nd.OnEnvironmentsTearDownStart",
292 "Environment::TearDown",
293 "2nd.OnEnvironmentsTearDownEnd",
294 "1st.OnEnvironmentsTearDownEnd",
295 "2nd.OnTestIterationEnd(1)",
296 "1st.OnTestIterationEnd(1)",
297 "2nd.OnTestProgramEnd",
298 "1st.OnTestProgramEnd"
300 VerifyResults(events
,
302 sizeof(expected_events
)/sizeof(expected_events
[0]));
304 // We need to check manually for ad hoc test failures that happen after
305 // RUN_ALL_TESTS finishes.
306 if (UnitTest::GetInstance()->Failed())