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.
30 // The Google C++ Testing and Mocking Framework (Google Test)
32 // This file verifies Google Test event listeners receive events at the
37 #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 size_t expected_data_size) {
180 const size_t 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 size_t 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 #%lu: %s\n",
197 static_cast<unsigned long>(i), data[i].c_str());
201 int main(int argc, char **argv) {
202 std::vector<std::string> events;
204 InitGoogleTest(&argc, argv);
206 UnitTest::GetInstance()->listeners().Append(
207 new EventRecordingListener("1st"));
208 UnitTest::GetInstance()->listeners().Append(
209 new EventRecordingListener("2nd"));
211 AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
213 GTEST_CHECK_(events.size() == 0)
214 << "AddGlobalTestEnvironment should not generate any events itself.";
216 ::testing::GTEST_FLAG(repeat) = 2;
217 int ret_val = RUN_ALL_TESTS();
219 const char* const expected_events[] = {
220 "1st.OnTestProgramStart",
221 "2nd.OnTestProgramStart",
222 "1st.OnTestIterationStart(0)",
223 "2nd.OnTestIterationStart(0)",
224 "1st.OnEnvironmentsSetUpStart",
225 "2nd.OnEnvironmentsSetUpStart",
226 "Environment::SetUp",
227 "2nd.OnEnvironmentsSetUpEnd",
228 "1st.OnEnvironmentsSetUpEnd",
229 "1st.OnTestCaseStart",
230 "2nd.OnTestCaseStart",
231 "ListenerTest::SetUpTestCase",
234 "ListenerTest::SetUp",
235 "ListenerTest::* Test Body",
236 "1st.OnTestPartResult",
237 "2nd.OnTestPartResult",
238 "ListenerTest::TearDown",
243 "ListenerTest::SetUp",
244 "ListenerTest::* Test Body",
245 "1st.OnTestPartResult",
246 "2nd.OnTestPartResult",
247 "ListenerTest::TearDown",
250 "ListenerTest::TearDownTestCase",
253 "1st.OnEnvironmentsTearDownStart",
254 "2nd.OnEnvironmentsTearDownStart",
255 "Environment::TearDown",
256 "2nd.OnEnvironmentsTearDownEnd",
257 "1st.OnEnvironmentsTearDownEnd",
258 "2nd.OnTestIterationEnd(0)",
259 "1st.OnTestIterationEnd(0)",
260 "1st.OnTestIterationStart(1)",
261 "2nd.OnTestIterationStart(1)",
262 "1st.OnEnvironmentsSetUpStart",
263 "2nd.OnEnvironmentsSetUpStart",
264 "Environment::SetUp",
265 "2nd.OnEnvironmentsSetUpEnd",
266 "1st.OnEnvironmentsSetUpEnd",
267 "1st.OnTestCaseStart",
268 "2nd.OnTestCaseStart",
269 "ListenerTest::SetUpTestCase",
272 "ListenerTest::SetUp",
273 "ListenerTest::* Test Body",
274 "1st.OnTestPartResult",
275 "2nd.OnTestPartResult",
276 "ListenerTest::TearDown",
281 "ListenerTest::SetUp",
282 "ListenerTest::* Test Body",
283 "1st.OnTestPartResult",
284 "2nd.OnTestPartResult",
285 "ListenerTest::TearDown",
288 "ListenerTest::TearDownTestCase",
291 "1st.OnEnvironmentsTearDownStart",
292 "2nd.OnEnvironmentsTearDownStart",
293 "Environment::TearDown",
294 "2nd.OnEnvironmentsTearDownEnd",
295 "1st.OnEnvironmentsTearDownEnd",
296 "2nd.OnTestIterationEnd(1)",
297 "1st.OnTestIterationEnd(1)",
298 "2nd.OnTestProgramEnd",
299 "1st.OnTestProgramEnd"
301 VerifyResults(events,
303 sizeof(expected_events)/sizeof(expected_events[0]));
305 // We need to check manually for ad hoc test failures that happen after
306 // RUN_ALL_TESTS finishes.
307 if (UnitTest::GetInstance()->Failed())