ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

ROS2 gmock for external libraries

asked 2022-06-07 07:31:59 -0500

Bernat Gaston gravatar image

Hi all,

I am trying to implement unit testing in my ROS2 project. I used to work with python and ROS1, where I had experience with pytest and nose. Now I am using ROS2-foxy, C++ and I've chosen gtest, since it seemed to me the best option (not sure though). Now, I am trying to test a function that has calls to rclcpp Node, since its class inherits from it. I am able to gtest it, but when I bring in gmock, I get tons of linking problems with gtest. Navigating arround, it seems that these linking problems come because gmock brings its own gtest implementation, which may conflict with previous one. However, due to the dependencies of ros-foxy-gtest and ros-foxy-gmock packages with the rest of the ros-foxy packages, I can not remove one of them or even both and execute my own version. I am trying to use known ros2 packages as examples (like rclcpp or even nav2) but I am not able to unlock how they use gmock or how they actually mock these functionalities. Any place where I can find easy examples? Tutorials? any clue? Many thanks

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-09-06 09:00:53 -0500

Bernat Gaston gravatar image

The problem is gmock already comes with a gtest implementation, so adding both in the CMAKE creates a conflict. Hence, if you just use gtest you should have at the end of your CMake

if(BUILD_TESTING)
find_package(ament_cmake_gtest REQUIRED)
ament_add_gtest(test_node test/src/test_node.cpp)
ament_target_dependencies(test_node
<dependencies>
)
target_link_libraries(test_node ${PROJECT_NAME}_lib)
endif()

But if you use gmock then

if(BUILD_TESTING)
find_package(ament_cmake_gmock REQUIRED)
ament_add_gmock(test_node test/src/test_node.cpp)
ament_target_dependencies(test_node
<dependencies>
)
target_link_libraries(test_node ${PROJECT_NAME}_lib)
endif()
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2022-06-07 07:31:59 -0500

Seen: 431 times

Last updated: Sep 06 '22