How to use launch_testing for testing executables created by cpp ros2 nodes?
I am trying to explore ways to test ros2 nodes, and so far came across the following methods:
- https://github.com/ros2/launch/tree/master/launch_testing
- https://github.com/ros2/launch_ros/tree/master/launch_testing_ros
Objective to accomplish: - Is to launch "system under test" node - publish some data on "SUT" node inputs - subscribe to the outputs of "SUT" node - assert and verify "SUT" outputs. - assert if the "SUT" node launches, shuts down properly
I guess, this objective can be accomplished without the above-mentioned packages (launchtesting & launchtesting_ros), but looking for better suggestions.
Also, where can find examples to test cpp ros2 nodes using these launchtesting or launchtesting_ros.
Environment: Ros2 Eloquent, Ubuntu 18.04
Asked by ros2user on 2019-12-10 17:58:45 UTC
Answers
You can use launch_ros for both cpp nodes and python nodes i guess for. For python you have to use the setup.py to make necessary files available to the test and for cpp the CMakeList.txt.
Below is the code you have to add to your CMakeList if testing a cpp package and the launch description for the node that is to be tested:
In your CMakeList.txt include
install(FILES test/<name>_launch_testing.py DESTINATION lib/${PROJECT_NAME}) if(BUILD_TESTING) find_package(launch_testing_ament_cmake) add_launch_test(test/<name>_launch_testing.py) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() endif()
Your generate_test_description(): inside
dut = Node( package='ros2_cpp_test_example', executable='<name>', name='<name>', ) context = {'dut': dut} return (launch.LaunchDescription([ dut, launch_testing.actions.ReadyToTest()] ), context )
Below the launch description you can add your testcases using the class unittest.TestCase. You can access the output of the launched nodes as described in the links you supplied.
Inside the testcases you can also start a testnode that receives/sends messages of/to the node you want to test as shown in https://github.com/ros2/launch_ros/blob/master/launch_testing_ros/test/examples/talker_listener_launch_test.py
Here you can find an example package in addition to the official documentation with Ubuntu 20.04 and Foxy: https://github.com/GitRepJo/ros2_cpp_test_example
Asked by jonas.ma on 2022-03-21 09:49:58 UTC
Comments
Hello! I have this exact same question. Did you by chance figure out what to use for cpp ros2 nodes? I am just leaning towards option 2 since it has a simple "talker" "listener" example that would work for me. https://github.com/ros2/launch_ros/blob/master/launch_testing_ros/test/examples/talker_listener_launch_test.py
Asked by swaroophs on 2020-05-20 03:50:40 UTC