[ros2] Test for correct node_name and namespace
I am writing integration test cases for my ROS service. Please see the sample code snippet below:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from threading import Thread
import unittest
from example_interfaces.srv import AddTwoInts
import launch
import launch_ros
import launch_ros.actions
import launch_testing.actions
import pytest
import rclpy
@pytest.mark.rostest
def generate_test_description():
minimal_service_node = launch_ros.actions.Node(
package='examples_rclpy_minimal_service',
namespace='my_namespace',
executable='service_member_function',
name='minimal_service',
)
return (
launch.LaunchDescription(
[
minimal_service_node,
launch_testing.actions.ReadyToTest(),
]
),
{
'minimal_service': minimal_service_node,
},
)
class TestMinimalService(unittest.TestCase):
@classmethod
def setUpClass(cls):
rclpy.init()
@classmethod
def tearDownClass(cls):
rclpy.shutdown()
def setUp(self):
self.node = rclpy.create_node('test_minimal_service')
# the following line is a pseudo code
self.alll_node_names_and_namespaces = rclpy.get_node_names_and_namespaces()
self.spin_thread = Thread(target=rclpy.spin, args=(self.node,))
self.spin_thread.start()
def tearDown(self):
self.node.destroy_node()
def test_node_namespaces(self):
# test whether node is having with correct namespace
namespaces = [namespace for _, namespace in self.alll_node_names_and_namespaces]
self.assertIn('my_namespace', namespaces)
def test_node_name(self):
# test whether node is having with correct name
node_names = [node_name for node_name, _ in self.alll_node_names_and_namespaces]
self.assertIn('minimal_service', node_names)
Question
How to test whether my node is having with correct name and namespace?
Additional Info
Below is the workspace structure:
.
├── build
├── install
├── log
└── src
└── minimal_service
├── CHANGELOG.rst
├── examples_rclpy_minimal_service
│ ├── __init__.py
│ └── service_member_function.py
├── package.xml
├── README.md
├── resource
│ └── examples_rclpy_minimal_service
├── setup.cfg
├── setup.py
└── test
├── test_copyright.py
├── test_flake8.py
├── test_pep257.py
└── test_service_member_function.py <- This is the newly added file
The package minimal_service
has been taken from ros2/examples.
Other Info
- Operating System: Ubuntu 20.04.4 LTS
- Installation type: binaries
- Version or commit hash: ROS Foxy
- DDS implementation: Default
- Client library: rclpy