0
$\begingroup$

Rosanswers logo

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


Originally posted by Bernat Gaston on ROS Answers with karma: 119 on 2022-06-07

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

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()

Originally posted by Bernat Gaston with karma: 119 on 2022-09-06

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.