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

How to use launch_testing for testing executables created by cpp ros2 nodes?

asked 2019-12-10 16:58:45 -0500

ros2user gravatar image

I am trying to explore ways to test ros2 nodes, and so far came across the following methods:

  1. https://github.com/ros2/launch/tree/m...
  2. https://github.com/ros2/launch_ros/tr...

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 (launch_testing & launch_testing_ros), but looking for better suggestions.

Also, where can find examples to test cpp ros2 nodes using these launch_testing or launch_testing_ros.

Environment: Ros2 Eloquent, Ubuntu 18.04

edit retag flag offensive close merge delete

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/bl...

swaroophs gravatar image swaroophs  ( 2020-05-20 03:50:40 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-03-21 09:49:58 -0500

jonas.ma gravatar image

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 <name>_launch_testing.py then gives your cpp node to the test system:

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/bl...

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_...

edit flag offensive delete link more

Question Tools

Stats

Asked: 2019-12-10 16:58:45 -0500

Seen: 979 times

Last updated: Dec 10 '19