8 from std_msgs.msg
import String
13 rospy.init_node(
'test_republisher', anonymous=
True)
14 self.
test_pub = rospy.Publisher(
'/input_topic', String, queue_size=10)
16 rospy.Subscriber(
'/output_topic', String, self.
callback)
19 timeout = rospy.get_time() + 5.0
21 while self.
test_pub.get_num_connections() == 0:
22 if rospy.get_time() > timeout:
23 self.fail(
"Test setup failed: Publisher connection timeout.")
33 test_message =
"key=value"
34 expected_message = f
"input_to_output={test_message}"
35 rospy.loginfo(f
"Publishing: {test_message} to /input_topic")
36 self.
test_pub.publish(String(data=test_message))
38 msg = rospy.wait_for_message(
'/output_topic', String, timeout=35)
39 print(f
"Received message: {msg.data}")
40 except rospy.ROSException:
41 print(
"Timeout exceeded while waiting for a message.")
42 rospy.loginfo(f
"Messages received: {self.received_messages}")
45 if __name__ ==
'__main__':
46 rostest.rosrun(
'my_ros_package',
'test_republisher', TestRepublisher)