三 Google Test源码浅析( 二 )

< 0;for (int i = 0; forever || i != repeat; i++) {//清除所有非AddHoc的测试结果 , 为后续运行做准备 。ClearNonAdHocTestResult(); const TimeInMillis start = GetTimeInMillis();if (has_tests_to_run && GTEST_FLAG(shuffle)) {random()->Reseed(random_seed_);ShuffleTests();}repeater->OnTestIterationStart(*parent_, i);if (has_tests_to_run) {repeater->OnEnvironmentsSetUpStart(*parent_);ForEach(environments_, SetUpEnvironment);repeater->OnEnvironmentsSetUpEnd(*parent_);if (!Test::HasFatalFailure()) { //核心代码for (int test_index = 0; test_index < total_test_case_count();test_index++) {GetMutableTestCase(test_index)->Run(); //逐个运行测试用例 , 执行后面的TestBody()}}repeater->OnEnvironmentsTearDownStart(*parent_);std::for_each(environments_.rbegin(), environments_.rend(),TearDownEnvironment);repeater->OnEnvironmentsTearDownEnd(*parent_);}elapsed_time_ = GetTimeInMillis() - start;repeater->OnTestIterationEnd(*parent_, i);if (!Passed()) {failed = true;}UnshuffleTests();if (GTEST_FLAG(shuffle)) {// Picks a new random seed for each iteration.random_seed_ = GetNextRandomSeed(random_seed_);}}repeater->OnTestProgramEnd(*parent_);return !failed;}
上述源码中 , 提取出最核心的代码 , 如下:
for (int test_index = 0; test_index < total_test_case_count();test_index++) {GetMutableTestCase(test_index)->Run(); //逐个运行测试用例 , 执行后面的TestBody()}
分析:上一篇看TEST宏的源码中 , 我们看到所有的测试用例都存放在一个数组当中 , 这里就利用这一特点 , 通过返回对象成员变量中的元素——各个测试用例对象指针 , 然后调用测试用例的Run方法 。
std::vector test_cases_;
2.4 ::Run()
void TestCase::Run() {if (!should_run_) return;internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();impl->set_current_test_case(this);TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();repeater->OnTestCaseStart(*this);impl->os_stack_trace_getter()->UponLeavingGTest();internal::HandleExceptionsInMethodIfSupported(this, &TestCase::RunSetUpTestCase, "SetUpTestCase()");const internal::TimeInMillis start = internal::GetTimeInMillis();//核心代码for (int i = 0; i < total_test_count(); i++) {GetMutableTestInfo(i)->Run();}elapsed_time_ = internal::GetTimeInMillis() - start;impl->os_stack_trace_getter()->UponLeavingGTest();internal::HandleExceptionsInMethodIfSupported(this, &TestCase::RunTearDownTestCase, "TearDownTestCase()");repeater->OnTestCaseEnd(*this);impl->set_current_test_case(NULL);}
分析:和上面 ::思想类似 , 其中核心的源码如下 , 这里还是获取特例信息 , 调用Run方法
for (int i = 0; i < total_test_count(); i++) {GetMutableTestInfo(i)->Run();}
2.3 ::Run()
源码:
void TestInfo::Run() {if (!should_run_) return;// Tells UnitTest where to store test result.internal::UnitTestImpl* const impl = internal::GetUnitTestImpl();impl->set_current_test_info(this);TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();// Notifies the unit test event listeners that a test is about to start.repeater->OnTestStart(*this);const TimeInMillis start = internal::GetTimeInMillis(); const TimeInMillis start = internal::GetTimeInMillis();impl->os_stack_trace_getter()->UponLeavingGTest();// Creates the test object. 核心代码 , 后面单独进行分析Test* const test = internal::HandleExceptionsInMethodIfSupported(factory_, &internal::TestFactoryBase::CreateTest,"the test fixture's constructor");if ((test != NULL) && !Test::HasFatalFailure()) {test->Run();}// Deletes the test object.impl->os_stack_trace_getter()->UponLeavingGTest();internal::HandleExceptionsInMethodIfSupported(test, &Test::DeleteSelf_, "the test fixture's destructor");result_.set_elapsed_time(internal::GetTimeInMillis() - start);// Notifies the unit test event listener that a test has just finished.repeater->OnTestEnd(*this);// Tells UnitTest to stop associating assertion results to this// test.impl->set_current_test_info(NULL);}