TEST_F宏和TEST宏的实现非常接近,只是TEST_F宏的封装更加开放一些,所以对TEST宏的功能多了一些扩展。
详细见GTest源码剖析——TEST宏
#define TEST_F(test_fixture, test_name)
GTEST_TEST_(test_fixture, test_name, test_fixture,
::testing::internal::GetTypeId<test_fixture>())
//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())
eg:
TEST(Call,makeCall);
TEST_F(Call,makeCall);
区别在于:
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 &);
};
区别在于:
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>);
class Call : public testing::Test
{
protected:
static void SetUpTestCase()
{
CallServer *_callServer = new CallServer;
}
static void TearDownTestCase()
{
delete _callServer;
_callServer = NULL;
}
static CallServer* _callServer;
};
class Call : public testing::Test
{
protected:
virtual void SetUp()
{
CallInfo *_callInfo = new CallInfo;
}
virtual void TearDown()
{
delete _callInfo;
_callInfo = NULL;
}
CallInfo* _callInfo;
};
github: googletest
ZhaiPillar
2017-09-16