我们又看到了熟悉的impl(UnitTestImpl),具体案例该怎么执行,还是得靠UnitTestImpl。
int UnitTestImpl::RunAllTests() { // ... printer->OnUnitTestStart(parent_); // 计时 const TimeInMillis start = GetTimeInMillis(); printer->OnGlobalSetUpStart(parent_); // 执行全局的SetUp事件 environments_.ForEach(SetUpEnvironment); printer->OnGlobalSetUpEnd(parent_); // 全局的SetUp事件执行成功的话 if (!Test::HasFatalFailure()) { // 执行每个测试案例 test_cases_.ForEach(TestCase::RunTestCase); } // 执行全局的TearDown事件 printer->OnGlobalTearDownStart(parent_); environments_in_reverse_order_.ForEach(TearDownEnvironment); printer->OnGlobalTearDownEnd(parent_); elapsed_time_ = GetTimeInMillis() - start; // 执行完成 printer->OnUnitTestEnd(parent_); // Gets the result and clears it. if (!Passed()) { failed = true; } ClearResult(); // 返回测试结果 return failed ? 1 : 0; } |
上面,我们很开心的看到了我们前面讲到的全局事件的调用。environments_是一个Environment的链表结构(List),它的内容是我们在main中通过:
testing::AddGlobalTestEnvironment(new FooEnvironment); |
添加进去的。test_cases_我们之前也了解过了,是一个TestCase的链表结构(List)。gtest实现了一个链表,并且提供了一个Foreach方法,迭代调用某个函数,并将里面的元素作为函数的参数:
template <typename F> // F is the type of the function/functor void ForEach(F functor) const { for ( const ListNode<E> * node = Head(); node != NULL; node = node->next() ) { functor(node->element()); } } |