因此,我们关注一下:environments_.ForEach(SetUpEnvironment),其实是迭代调用了SetUpEnvironment函数:
static void SetUpEnvironment(Environment* env) { env->SetUp(); } |
最终调用了我们定义的SetUp()函数。
再看看test_cases_.ForEach(TestCase::RunTestCase)的TestCase::RunTestCase实现:
static void RunTestCase(TestCase * test_case) { test_case->Run(); } |
再看TestCase的Run实现:
void TestCase::Run() { if (!should_run_) return; internal::UnitTestImpl* const impl = internal::GetUnitTestImpl(); impl->set_current_test_case(this); UnitTestEventListenerInterface * const result_printer = impl->result_printer(); result_printer->OnTestCaseStart(this); impl->os_stack_trace_getter()->UponLeavingGTest(); // 哈!SetUpTestCases事件在这里调用 set_up_tc_(); const internal::TimeInMillis start = internal::GetTimeInMillis(); // 嗯,前面分析的一个TestCase对应多个TestInfo,因此,在这里迭代对TestInfo调用RunTest方法 test_info_list_->ForEach(internal::TestInfoImpl::RunTest); elapsed_time_ = internal::GetTimeInMillis() - start; impl->os_stack_trace_getter()->UponLeavingGTest(); // TearDownTestCases事件在这里调用 tear_down_tc_(); result_printer->OnTestCaseEnd(this); impl->set_current_test_case(NULL); } |
第二种事件机制又浮出我们眼前,非常兴奋。可以看出,SetUpTestCases和TearDownTestCaess是在一个TestCase之前和之后调用的。接着看test_info_list_->ForEach(internal::TestInfoImpl::RunTest):
static void RunTest(TestInfo * test_info) { test_info->impl()->Run(); } |