Robotics StackExchange | Archived questions

When writing gtests with ROS, why doesn't the FRIEND_TEST macro work?

For unit testing, I need to write gtests that can access at least some private data. The FRIEND_TEST macro doesn't seem to be working however. In the example here, I have my so-called "targetClass" whose privateMap I wish to access. This has not been compiling for me.

Within targetClassTester, I have this:

TEST_F(tester_base, exp) 
{
    myMap = myTargetClass.privateMap;
}

Meanwhile, in the header file for targetClass, I have the following in the private section:

FRIEND_TEST(tester_base, exp);

Finally, the following shows what the relevant parts of CMakeLists.txt would look like: (As a special note, targetClass.cpp contains a main() method and is normally created as its own executable. It's only for testing that I created the "targetClassForTesting" library. For unit testing, the main() method in targetClass.cpp is ignored.)

add_library(targetClassForTesting src/targetClass.cpp include/au_uav_ros/targetClass.h)

add_executable(targetClassTester include/myPackage/test/targetClassTester.h src/test/targetClassTester.cpp include/au_uav_ros/targetClass.h)

target_link_libraries(targetClassTester ${catkin_LIBRARIES} ${GTEST_LIBRARIES} targetClassForTesting)

add_rostest(test/targetClassTester.test)

There don't seem to be any other questions on the internet about FRIEND_TEST malfunctioning, making me think the issue is particular to ROS.

Let me know if you need any more information. Thank you.

Asked by Toletum on 2013-08-05 12:04:42 UTC

Comments

There's no expected issues with integrating with googletest elements. What is your actual error? Without an actual error it's hard to help you.

Asked by tfoote on 2014-01-15 14:54:35 UTC

Same problem. Actual error: Compilation fails with msg error: ‘void TargetClass::privateMap()’ is private within this context

Asked by mch on 2019-08-01 13:48:55 UTC

I'm experiencing this same issue. Did you find a solution?

Asked by tyler-picknik on 2019-11-22 13:09:59 UTC

I've found workaround which satisfied my need.

#ifdef UNIT_TESTING_ON
 public:
#else
 private:
#endif

Then to enable testing in cmake file you can do

target_compile_definitions(${UNIT_TEST_NAME} PRIVATE UNIT_TESTING_ON)

Asked by mch on 2019-11-22 18:49:40 UTC

Answers