使用FRIEND_TEST是正确的吗?

时间:2012-10-27 07:39:38

标签: c++ googletest

当我在https://github.com/google/googletest/blob/master/googletest/include/gtest/gtest_prod.h查看FRIEND_TEST的实施时,我看到以下内容:

#ifndef GTEST_INCLUDE_GTEST_GTEST_PROD_H_
#define GTEST_INCLUDE_GTEST_GTEST_PROD_H_
// When you need to test the private or protected members of a class,
// use the FRIEND_TEST macro to declare your tests as friends of the
// class.  For example:
//
// class MyClass {
//  private:
//   void MyMethod();
//   FRIEND_TEST(MyClassTest, MyMethod);
// };
//
// class MyClassTest : public testing::Test {
//   // ...
// };
//
// TEST_F(MyClassTest, MyMethod) {
//   // Can call MyClass::MyMethod() here.
// }
#define FRIEND_TEST(test_case_name, test_name)\
friend class test_case_name##_##test_name##_Test
#endif 

如果我的理解是正确的,那么测试类将无条件地成为生产类的子级。这将使生产类依赖于测试类。实际上,生产代码也将包含我的测试库。

我不确定这是否正确。

我在这里遗漏了什么,或者只是有条件地编译?

感谢。

1 个答案:

答案 0 :(得分:3)

我不是那样读的。如果需要,您可以将组成的不存在的类作为生产类的朋友。它是无害的,它肯定不会为您的生产代码引入依赖或添加测试代码。

class Production
{
   friend class WibbleWibble;
   ...
};

即使WibbleWibble不存在,此代码也完全正确。所以没有依赖。

相关问题