Skip to content

Commit 7d1e8ce

Browse files
Add a test to check if trajectory message is sent
1 parent 1695484 commit 7d1e8ce

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

example_7/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ if(BUILD_TESTING)
108108
ament_add_pytest_test(example_7_urdf_xacro test/test_urdf_xacro.py)
109109
ament_add_pytest_test(view_example_7_launch test/test_view_robot_launch.py)
110110
ament_add_pytest_test(run_example_7_launch test/test_r6bot_controller_launch.py)
111+
ament_add_pytest_test(test_send_trajectory_launch test/test_send_trajectory_launch.py)
111112
endif()
112113

113114
## EXPORTS
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# Copyright (c) 2024 AIT - Austrian Institute of Technology GmbH
2+
#
3+
# Redistribution and use in source and binary forms, with or without
4+
# modification, are permitted provided that the following conditions are met:
5+
#
6+
# * Redistributions of source code must retain the above copyright
7+
# notice, this list of conditions and the following disclaimer.
8+
#
9+
# * Redistributions in binary form must reproduce the above copyright
10+
# notice, this list of conditions and the following disclaimer in the
11+
# documentation and/or other materials provided with the distribution.
12+
#
13+
# * Neither the name of the {copyright_holder} nor the names of its
14+
# contributors may be used to endorse or promote products derived from
15+
# this software without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27+
# POSSIBILITY OF SUCH DAMAGE.
28+
#
29+
# Author: Christoph Froehlich
30+
31+
import os
32+
import pytest
33+
import unittest
34+
35+
from ament_index_python.packages import get_package_share_directory
36+
from launch import LaunchDescription
37+
from launch.actions import IncludeLaunchDescription
38+
from launch.launch_description_sources import PythonLaunchDescriptionSource
39+
from launch_testing.actions import ReadyToTest
40+
from launch_testing_ros import WaitForTopics
41+
from trajectory_msgs.msg import JointTrajectory
42+
43+
import launch_testing.markers
44+
import rclpy
45+
46+
47+
# Executes the given launch file and checks if all nodes can be started
48+
@pytest.mark.rostest
49+
def generate_test_description():
50+
launch_include = IncludeLaunchDescription(
51+
PythonLaunchDescriptionSource(
52+
os.path.join(
53+
get_package_share_directory("ros2_control_demo_example_7"),
54+
"launch/send_trajectory.launch.py",
55+
)
56+
)
57+
)
58+
59+
return LaunchDescription([launch_include, ReadyToTest()])
60+
61+
62+
# This is our test fixture. Each method is a test case.
63+
# These run alongside the processes specified in generate_test_description()
64+
class TestFixture(unittest.TestCase):
65+
@classmethod
66+
def setUpClass(cls):
67+
rclpy.init()
68+
69+
@classmethod
70+
def tearDownClass(cls):
71+
rclpy.shutdown()
72+
73+
def setUp(self):
74+
self.node = rclpy.create_node("test_node")
75+
76+
def tearDown(self):
77+
self.node.destroy_node()
78+
79+
def test_check_if_msgs_published(self):
80+
topic = "/r6bot_controller/joint_trajectory"
81+
wait_for_topics = WaitForTopics([(topic, JointTrajectory)], timeout=20.0)
82+
assert wait_for_topics.wait(), f"Topic '{topic}' not found!"
83+
msgs = wait_for_topics.received_messages(topic)
84+
msg = msgs[0]
85+
assert len(msg.points) == 200, "Wrong number of joints in message"
86+
wait_for_topics.shutdown()
87+
88+
89+
@launch_testing.post_shutdown_test()
90+
# These tests are run after the processes in generate_test_description() have shutdown.
91+
class TestDescriptionCraneShutdown(unittest.TestCase):
92+
93+
def test_exit_codes(self, proc_info):
94+
"""Check if the processes exited normally."""
95+
launch_testing.asserts.assertExitCodes(proc_info)

0 commit comments

Comments
 (0)