玩转Google单元测试框架gtest系列之七:深入解析gtest

发表于:2010-4-12 14:54

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:CoderZh    来源:www.cnblogs.com/coderzh

分享:

  因此,我们关注一下: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();
}

《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号