正在使用friend_test要做的正确的事情

Is using FRIEND_TEST the right thing to do?

本文关键字:正确的事 test friend      更新时间:2023-10-16

当我在https://github.com/google/googletest/googletest/blob/master/googletest/googletest/gtest/gtest/gtest/gtest_prod.h- https://github.com/googletest/googletest/googletest/googletest/googletest/googletest/googletest/googletest/googletest/googletest/grood.h https://github.com/googletest/googletest/googletest/grood.h中进行

#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,此代码也是完全正确的。因此没有依赖性。