首页 文章

使用FRIEND_TEST是正确的吗?

提问于
浏览
1

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

  • 3

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

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

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

相关问题