三 Google Test源码浅析

三、总结
一、例子引入:
TEST(TestCaseName1, TestName1){cout << "hello1" << endl;}int main(int argc,char* argv[]){testing::InitGoogleTest(&argc, argv);return RUN_ALL_TESTS();}
分析:上述例子当中 , 看到()这个宏 , 从名字上来看 , 这是运行所有的测试用例 , 这才是我们运行测试用例的真正入口 , 下面来看一下具体的源码实现
二、源码分析
()之前是一个宏 , 将其实现为函数 , 在这里 , 调用了单例的Run函数
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_;inline int RUN_ALL_TESTS() {return ::testing::UnitTest::GetInstance()->Run();}
接着去看调用过程 , 可以看到 , 依次调用的过程是:
1. ::Run()
2. ::()
3. ::Run()
4. ::Run()
5. Test::Run()
2.1 ::Run()
int UnitTest::Run() {//关于死亡测试const bool in_death_test_child_process =internal::GTEST_FLAG(internal_run_death_test).length() > 0;const internal::ScopedPrematureExitFile premature_exit_file(in_death_test_child_process ?NULL :internal::posix::GetEnv("TEST_PREMATURE_EXIT_FILE"));impl()->set_catch_exceptions(GTEST_FLAG(catch_exceptions)); //捕获异常#if GTEST_HAS_SEHif (impl()->catch_exceptions() || in_death_test_child_process) {# if !GTEST_OS_WINDOWS_MOBILE && !GTEST_OS_WINDOWS_PHONE && !GTEST_OS_WINDOWS_RTSetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOALIGNMENTFAULTEXCEPT |SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);# endif// !GTEST_OS_WINDOWS_MOBILE# if (defined(_MSC_VER) || GTEST_OS_WINDOWS_MINGW) && !GTEST_OS_WINDOWS_MOBILE_set_error_mode(_OUT_TO_STDERR);# endif# if _MSC_VER >= 1400 && !GTEST_OS_WINDOWS_MOBILEif (!GTEST_FLAG(break_on_failure))_set_abort_behavior(0x0,// Clear the following flags:_WRITE_ABORT_MSG | _CALL_REPORTFAULT);// pop-up window, core dump.# endif}#endif// GTEST_HAS_SEH//以上全是关于死亡测试和跨平台异常的的一些设定 , 下面才是真正实现其功能的地方return internal::HandleExceptionsInMethodIfSupported(impl(),&internal::UnitTestImpl::RunAllTests,"auxiliary test code (environments or event listeners)") ? 0 : 1;}
如果不涉及源码中的死亡测试以及跨平台可能出现异常的一些问题 , 其简化为
int UnitTest::Run() {return internal::HandleExceptionsInMethodIfSupported(impl(),&internal::UnitTestImpl::RunAllTests,"auxiliary test code (environments or event listeners)") ? 0 : 1;}
在将其简化 , 可以发现由impl对象指针调用了();
int UnitTest::Run() {return impl->RunAllTests();}
这里使用了impl()方法 , 该方法返回了一个对象指针impl_ , 由指针调用了 , 
【三Google Test源码浅析】UnitTest::UnitTest() {impl_ = new internal::UnitTestImpl(this);}
2.2 ::()
源码:
bool UnitTestImpl::RunAllTests() {//准备工作// Makes sure InitGoogleTest() was called.if (!GTestIsInitialized()) {printf("%s","\nThis test program did NOT call ::testing::InitGoogleTest ""before calling RUN_ALL_TESTS().Please fix it.\n");return false;}// Do not run any test if the --help flag was specified.if (g_help_flag)return true;//重复对后标记解析初始化的调用 , 以防万一用户没有调用InitGoogleTest 。PostFlagParsingInit();//关于对用户提供的设计internal::WriteToShardStatusFileIfNeeded();//对死亡测试线程安全方面的检测bool in_subprocess_for_death_test = false;#if GTEST_HAS_DEATH_TESTin_subprocess_for_death_test = (internal_run_death_test_flag_.get() != NULL);# if defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)if (in_subprocess_for_death_test) {GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_();}# endif// defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_)#endif// GTEST_HAS_DEATH_TEST//与环境相关的设置 , 暂时不关心const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex,in_subprocess_for_death_test);// 匹配测试用例 , 对所有的测试用例进行匹配 , 决定要执行哪一个测试用例const bool has_tests_to_run = FilterTests(should_shard? HONOR_SHARDING_PROTOCOL: IGNORE_SHARDING_PROTOCOL) > 0;//如果指定了gtestlisttest标志 , 则列出测试和出口 。if (GTEST_FLAG(list_tests)) {// This must be called *after* FilterTests() has been called.ListTestsMatchingFilter();return true;}//生成随机测试用例random_seed_ = GTEST_FLAG(shuffle) ?GetRandomSeedFromFlag(GTEST_FLAG(random_seed)) : 0;// True iff at least one test has failed.bool failed = false;//获取事件监听器 , 如果用户订阅了某事件 , 则在适当的时间点上报该事件 , 让用户进行额外的操作TestEventListener* repeater = listeners()->repeater();//上报事件OnTestProgramStartstart_timestamp_ = GetTimeInMillis();repeater->OnTestProgramStart(*parent_);const int repeat = in_subprocess_for_death_test ? 1 : GTEST_FLAG(repeat);const bool forever = repeat