[ROS2][foxy][launch testing] Integration testing with launch testing
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)