1
當我看着FRIEND_TEST在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
如果我的理解是正確的,測試類是由生產類的子類無條件。這將使生產類依賴於測試類。實際上,生產代碼也會包含我的測試庫。
我不知道這是否是正確的做法。
我在這裏錯過了什麼,或者應該只是有條件地編譯?
謝謝。
謝謝。不知道。 –
實際上,依賴是相反的。通過使測試類成爲生產類的朋友,您正在使測試類依賴於生產類的實現。這並不理想,但我想有時候在現實世界中需要它。 – john