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

[ROS2][foxy][launch testing] Integration testing with launch testing

asked 2021-05-05 10:21:04 -0500

anastasiaPan gravatar image

updated 2021-05-05 10:22:51 -0500

I would like to do integration test using https://index.ros.org/p/launch_testin.... I have based my self on the examples (https://github.com/ros2/launch/tree/m...) and I can launch the node and run unittest.TestCase but I am not sure how to check the output. All I can find is the launch_testing.asserts funcitons but it looks like I can only test exit codes and strings? I would like to test:

  • a response from a service call
  • a result from a published topic

Example of my code - currently I am only trying to test the response from the service: I can see that the node is launched and the service is called but I am not able to test the response

    import os
    import sys
    import unittest
    import launch.actions
    import launch.substitutions
    import lifecycle_msgs.msg
    import ament_index_python
    import launch_ros.actions
    import launch_ros.events
    import launch_ros.events.lifecycle
    import launch_testing
    import launch_testing.actions
    import pytest

    from launch import LaunchDescription
    from launch import LaunchService
    from launch.actions import ExecuteProcess
    from launch_ros.actions import Node
    from launch_testing.legacy import LaunchTestService                                                                                                           
    from my_srv_pkg.srv import MySrv
    from launch_testing.asserts import assertSequentialStdout

@pytest.mark.launch_test
    def generate_test_description():
        TEST_PROC_PATH = os.path.join(
            ament_index_python.get_package_prefix('launch_testing'),
            'lib/launch_testing',
            'my-pkg'
        )

        # This is necessary to get unbuffered output from the process under test
        proc_env = os.environ.copy()
        proc_env['PYTHONUNBUFFERED'] = '1'

        launch_node = launch.actions.ExecuteProcess(
            cmd=['ros2 launch my-pkg my_launch.py'],
            shell=True,
            env=proc_env, output='screen'
        )

    return launch.LaunchDescription([
        launch_node,
        launch_testing.actions.ReadyToTest()
    ])

class TestServiceCall(unittest.TestCase):
    def test_if_service_is_called(self, proc_output):
        call_service_action = ExecuteProcess(
        cmd=['ros2 service call /my_service my-pkg/srv/MySrv "{<request data>}"'],
        shell=True,
            name='call_service_action',
            output='screen')

            response = MySrv.Response()
            <populate response with expected value>

            # here I want to check the response - if it is the same to what I expect  
            proc_output.assertWaitFor(response, timeout=5)
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-03-11 13:37:50 -0500

bojankoce gravatar image

Hello, @anastasiaPan.

Some illustrative examples about ROS2 unit and integration testing can be found in this document.

Did you find some more learning resources in the meantime? I am also interested in this topic.

Cheers, Bojan.

edit flag offensive delete link more
1

answered 2022-07-25 04:36:37 -0500

jonas.ma gravatar image

As i run into the same problem i created two example packages for testing a node that publishes a text and a pointcloud2 message. Maybe the packages can be of some help.

Testing a node that receives a string and republishes it:
https://github.com/GitRepJo/ros2_cpp_test_example

Testing a node that receives a Pointcloud2 message and republishes it:
https://github.com/GitRepJo/pcl_example

edit flag offensive delete link more

Question Tools

6 followers

Stats

Asked: 2021-05-05 10:21:04 -0500

Seen: 891 times

Last updated: Jul 25 '22