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

asked 2013-08-05 12:04:42 -0500

Toletum gravatar image

updated 2014-04-20 14:09:40 -0500

ngrennan gravatar image

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.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by tfoote
close date 2016-04-27 01:59:34.711350

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.

tfoote gravatar image tfoote  ( 2014-01-15 13:54:35 -0500 )edit

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

mch gravatar image mch  ( 2019-08-01 13:48:55 -0500 )edit

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

tyler-picknik gravatar image tyler-picknik  ( 2019-11-22 12:09:59 -0500 )edit

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)
mch gravatar image mch  ( 2019-11-22 17:49:40 -0500 )edit