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;
48 using ::testing::internal::String;
50 // Used by tests to register their events.
51 std::vector<String>* g_events = NULL;
56 class EventRecordingListener : public TestEventListener {
58 EventRecordingListener(const char* name) : name_(name) {}
61 virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {
62 g_events->push_back(GetFullMethodName("OnTestProgramStart"));
65 virtual void OnTestIterationStart(const UnitTest& /*unit_test*/,
68 message << GetFullMethodName("OnTestIterationStart")
69 << "(" << iteration << ")";
70 g_events->push_back(message.GetString());
73 virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {
74 g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpStart"));
77 virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {
78 g_events->push_back(GetFullMethodName("OnEnvironmentsSetUpEnd"));
81 virtual void OnTestCaseStart(const TestCase& /*test_case*/) {
82 g_events->push_back(GetFullMethodName("OnTestCaseStart"));
85 virtual void OnTestStart(const TestInfo& /*test_info*/) {
86 g_events->push_back(GetFullMethodName("OnTestStart"));
89 virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {
90 g_events->push_back(GetFullMethodName("OnTestPartResult"));
93 virtual void OnTestEnd(const TestInfo& /*test_info*/) {
94 g_events->push_back(GetFullMethodName("OnTestEnd"));
97 virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {
98 g_events->push_back(GetFullMethodName("OnTestCaseEnd"));
101 virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {
102 g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownStart"));
105 virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {
106 g_events->push_back(GetFullMethodName("OnEnvironmentsTearDownEnd"));
109 virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/,
112 message << GetFullMethodName("OnTestIterationEnd")
113 << "(" << iteration << ")";
114 g_events->push_back(message.GetString());
117 virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {
118 g_events->push_back(GetFullMethodName("OnTestProgramEnd"));
122 String GetFullMethodName(const char* name) {
124 message << name_ << "." << name;
125 return message.GetString();
131 class EnvironmentInvocationCatcher : public Environment {
133 virtual void SetUp() {
134 g_events->push_back(String("Environment::SetUp"));
137 virtual void TearDown() {
138 g_events->push_back(String("Environment::TearDown"));
142 class ListenerTest : public Test {
144 static void SetUpTestCase() {
145 g_events->push_back(String("ListenerTest::SetUpTestCase"));
148 static void TearDownTestCase() {
149 g_events->push_back(String("ListenerTest::TearDownTestCase"));
152 virtual void SetUp() {
153 g_events->push_back(String("ListenerTest::SetUp"));
156 virtual void TearDown() {
157 g_events->push_back(String("ListenerTest::TearDown"));
161 TEST_F(ListenerTest, DoesFoo) {
162 // Test execution order within a test case is not guaranteed so we are not
163 // recording the test name.
164 g_events->push_back(String("ListenerTest::* Test Body"));
165 SUCCEED(); // Triggers OnTestPartResult.
168 TEST_F(ListenerTest, DoesBar) {
169 g_events->push_back(String("ListenerTest::* Test Body"));
170 SUCCEED(); // Triggers OnTestPartResult.
173 } // namespace internal
175 } // namespace testing
177 using ::testing::internal::EnvironmentInvocationCatcher;
178 using ::testing::internal::EventRecordingListener;
180 void VerifyResults(const std::vector<String>& data,
181 const char* const* expected_data,
182 int expected_data_size) {
183 const int actual_size = data.size();
184 // If the following assertion fails, a new entry will be appended to
185 // data. Hence we save data.size() first.
186 EXPECT_EQ(expected_data_size, actual_size);
188 // Compares the common prefix.
189 const int shorter_size = expected_data_size <= actual_size ?
190 expected_data_size : actual_size;
192 for (; i < shorter_size; ++i) {
193 ASSERT_STREQ(expected_data[i], data[i].c_str())
194 << "at position " << i;
197 // Prints extra elements in the actual data.
198 for (; i < actual_size; ++i) {
199 printf(" Actual event #%d: %s\n", i, data[i].c_str());
203 int main(int argc, char **argv) {
204 std::vector<String> events;
206 InitGoogleTest(&argc, argv);
208 UnitTest::GetInstance()->listeners().Append(
209 new EventRecordingListener("1st"));
210 UnitTest::GetInstance()->listeners().Append(
211 new EventRecordingListener("2nd"));
213 AddGlobalTestEnvironment(new EnvironmentInvocationCatcher);
215 GTEST_CHECK_(events.size() == 0)
216 << "AddGlobalTestEnvironment should not generate any events itself.";
218 ::testing::GTEST_FLAG(repeat) = 2;
219 int ret_val = RUN_ALL_TESTS();
221 const char* const expected_events[] = {
222 "1st.OnTestProgramStart",
223 "2nd.OnTestProgramStart",
224 "1st.OnTestIterationStart(0)",
225 "2nd.OnTestIterationStart(0)",
226 "1st.OnEnvironmentsSetUpStart",
227 "2nd.OnEnvironmentsSetUpStart",
228 "Environment::SetUp",
229 "2nd.OnEnvironmentsSetUpEnd",
230 "1st.OnEnvironmentsSetUpEnd",
231 "1st.OnTestCaseStart",
232 "2nd.OnTestCaseStart",
233 "ListenerTest::SetUpTestCase",
236 "ListenerTest::SetUp",
237 "ListenerTest::* Test Body",
238 "1st.OnTestPartResult",
239 "2nd.OnTestPartResult",
240 "ListenerTest::TearDown",
245 "ListenerTest::SetUp",
246 "ListenerTest::* Test Body",
247 "1st.OnTestPartResult",
248 "2nd.OnTestPartResult",
249 "ListenerTest::TearDown",
252 "ListenerTest::TearDownTestCase",
255 "1st.OnEnvironmentsTearDownStart",
256 "2nd.OnEnvironmentsTearDownStart",
257 "Environment::TearDown",
258 "2nd.OnEnvironmentsTearDownEnd",
259 "1st.OnEnvironmentsTearDownEnd",
260 "2nd.OnTestIterationEnd(0)",
261 "1st.OnTestIterationEnd(0)",
262 "1st.OnTestIterationStart(1)",
263 "2nd.OnTestIterationStart(1)",
264 "1st.OnEnvironmentsSetUpStart",
265 "2nd.OnEnvironmentsSetUpStart",
266 "Environment::SetUp",
267 "2nd.OnEnvironmentsSetUpEnd",
268 "1st.OnEnvironmentsSetUpEnd",
269 "1st.OnTestCaseStart",
270 "2nd.OnTestCaseStart",
271 "ListenerTest::SetUpTestCase",
274 "ListenerTest::SetUp",
275 "ListenerTest::* Test Body",
276 "1st.OnTestPartResult",
277 "2nd.OnTestPartResult",
278 "ListenerTest::TearDown",
283 "ListenerTest::SetUp",
284 "ListenerTest::* Test Body",
285 "1st.OnTestPartResult",
286 "2nd.OnTestPartResult",
287 "ListenerTest::TearDown",
290 "ListenerTest::TearDownTestCase",
293 "1st.OnEnvironmentsTearDownStart",
294 "2nd.OnEnvironmentsTearDownStart",
295 "Environment::TearDown",
296 "2nd.OnEnvironmentsTearDownEnd",
297 "1st.OnEnvironmentsTearDownEnd",
298 "2nd.OnTestIterationEnd(1)",
299 "1st.OnTestIterationEnd(1)",
300 "2nd.OnTestProgramEnd",
301 "1st.OnTestProgramEnd"
303 VerifyResults(events,
305 sizeof(expected_events)/sizeof(expected_events[0]));
307 // We need to check manually for ad hoc test failures that happen after
308 // RUN_ALL_TESTS finishes.
309 if (UnitTest::GetInstance()->Failed())