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

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

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

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

  我们又看到了熟悉的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());
    }
}

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

关注51Testing

联系我们

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

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

沪ICP备05003035号

沪公网安备 31010102002173号