繁体   English   中英

使用FRIEND_TEST是正确的吗?

[英]Is using FRIEND_TEST the right thing to do?

当我在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 

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

我不确定这是否是正确的做法。

我在这里遗漏了什么,还是应该只是有条件地编译?

谢谢。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM