GTest源码剖析(三)——TEST_F宏

GTest源码剖析——TEST_F宏

  • GTest源码剖析TEST_F宏
    • TEST_F宏与TEST宏的区别
      • 1 TEST_F宏定义
      • 2 TEST宏定义
      • 3 对比分析
        • 31 拼接类对比
        • 32 调用MakeAndRegisterTestInfo传参对比
    • TEST_F宏作用
      • 1 TestCase级扩展
      • 2 Test级扩展
    • 参考

1 TEST_F宏与TEST宏的区别

TEST_F宏和TEST宏的实现非常接近,只是TEST_F宏的封装更加开放一些,所以对TEST宏的功能多了一些扩展。
详细见GTest源码剖析——TEST宏

1.1 TEST_F宏定义

#define TEST_F(test_fixture, test_name)
  GTEST_TEST_(test_fixture, test_name, test_fixture, 
              ::testing::internal::GetTypeId<test_fixture>())

1.2 TEST宏定义

//step1
#if !GTEST_DONT_DEFINE_TEST
# define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name)
#endif

//step2
#define GTEST_TEST(test_case_name, test_name)
  GTEST_TEST_(test_case_name, test_name,::testing::Test, ::testing::internal::GetTestTypeId())

1.3 对比分析

eg:

TEST(Call,makeCall);
TEST_F(Call,makeCall);

1.3.1 拼接类对比

区别在于:
TEST宏中的拼接类Call_makeCall_Test继承于Test类;
TEST_F宏中的拼接类Call_makeCall_Test继承于Call类;

即相对于TEST宏,TEST_F可以覆盖Test类中的虚函数SetUp()和TearDown()。

TEST宏:

class Call_makeCall_Test : public ::testing::Test    
{    
public:    
    Call_makeCall_Test();    

private:    
    virtual void TestBody();
    static ::testing::TestInfo* const test_info_ ; 

    Call_makeCall_Test(Call_makeCall_Test const &);
    void operator=(Call_makeCall_Test const &);
};  

TEST_F宏:

class Call_makeCall_Test : public ::testing::Call    
{    
public:    
    Call_makeCall_Test();    

private:    
    virtual void TestBody();
    static ::testing::TestInfo* const test_info_ ; 

    Call_makeCall_Test(Call_makeCall_Test const &);
    void operator=(Call_makeCall_Test const &);
};  

1.3.2 调用MakeAndRegisterTestInfo()传参对比

区别在于:
TEST宏: 传入MakeAndRegisterTestInfo()的参数为::testing::Test::SetUpTestCase和::testing::Test::TearDownTestCase。
TEST_F宏: 传入MakeAndRegisterTestInfo()的参数为Call::SetUpTestCase和Call::TearDownTestCase。

即相对于TEST宏,TEST_F可以定义自己的SetUpTestCase和TearDownTestCase。

TEST宏:

::testing::TestInfo* const Call_makeCall_Test::test_info_ =   
    ::testing::internal::MakeAndRegisterTestInfo(  
        "Call",   
        "makeCall",   
        NULL,   
        NULL,   
        ::testing::internal::CodeLocation(__FILE__, __LINE__), 
        ::testing::internal::GetTestTypeId(),
        ::testing::Test::SetUpTestCase,   
        ::testing::Test::TearDownTestCase,   
        new ::testing::internal::TestFactoryImpl<Call_makeCall_Test>);   

TEST_F宏:

::testing::TestInfo* const Call_makeCall_Test::test_info_ =   
    ::testing::internal::MakeAndRegisterTestInfo(  
        "Call",   
        "makeCall",   
        NULL,   
        NULL,   
        ::testing::internal::CodeLocation(__FILE__, __LINE__), 
        ::testing::internal::GetTestTypeId(),
        Call::SetUpTestCase,   
        Call::TearDownTestCase,   
        new ::testing::internal::TestFactoryImpl<Call_makeCall_Test>);   

2 TEST_F宏作用:

2.1 TestCase级扩展

  1. SetUpTestCase()方法在TestCase的第一个test之前执行.
  2. TearDownTestCase()方法在TestCase的最后一个test之后执行.
  3. 在TestCase为Call的测试用例中,可以共享_callServer,可以跨越多个test。
class Call : public testing::Test 
{
protected:
  static void SetUpTestCase() 
  {
    CallServer *_callServer = new CallServer;
  }

  static void TearDownTestCase() 
  {
    delete _callServer;
    _callServer = NULL;
  }

  static CallServer* _callServer;
};

2.2 Test级扩展

  1. SetUp()方法在每个Test之前执行
  2. TearDown()方法在每个Test之后执行
  3. 在TestCase为Call的测试用例中,可以共享_callInfo,SetUp会在下一个test重新调用,所以逐个对每一个test生效。
class Call : public testing::Test 
{
protected:
    virtual void SetUp()
    {
        CallInfo *_callInfo = new CallInfo;
    }
    virtual void TearDown()
    {
        delete _callInfo;
        _callInfo = NULL;
    }

    CallInfo* _callInfo;
};

3 参考

github: googletest


ZhaiPillar
2017-09-16

你可能感兴趣的:(GTest)