-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Unit Test for Republisher Node on Noetic Branch #39
base: noetic-devel
Are you sure you want to change the base?
Changes from all commits
9a2f422
4941500
ae89291
1e91c64
4adba3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,6 @@ | ||
__pycache__/ | ||
/inorbit_republisher/test/sample_data/build/ | ||
/inorbit_republisher/test/sample_data/hardcodedRosbag/ | ||
/inorbit_republisher/test/sample_data/install/ | ||
/inorbit_republisher/test/sample_data/log/ | ||
.history/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,6 +54,16 @@ republishers: | |
topic: "/inorbit/custom_data/0" | ||
key: "navsat" | ||
|
||
# Añadido para los tópicos input y output | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid using language other than english |
||
- topic: "/input_topic" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wouldn't use this file for testing purposes, as it's the example used on the main README. Instead, I'd create a separate configuration file e.g. |
||
msg_type: "std_msgs/String" | ||
mappings: | ||
- field: "data" | ||
mapping_type: "single_field" | ||
out: | ||
topic: "/output_topic" | ||
key: "input_to_output" | ||
|
||
static_publishers: | ||
- value_from: | ||
package_version: "rospy" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Unit Test for ROS Republisher Node | ||
|
||
This repository contains a unit test for a ROS node that republishes messages from `/input_topic` to `/output_topic`. The test ensures the republisher node processes and republishes messages correctly. | ||
|
||
## Test Description | ||
|
||
The unit test is implemented in `test_republisher.py` and uses the `unittest` framework along with `rostest`. It includes the following test cases: | ||
|
||
- **`test_message_republishing`**: | ||
- Publishes a test message (`key=value`) to `/input_topic`. | ||
- Verifies that the republisher node republishes the message with the correct prefix (`input_to_output=`) to `/output_topic`. | ||
|
||
- **`test_no_message`**: | ||
- Ensures that no messages are received on `/output_topic` at the start of the test. | ||
|
||
The test subscribes to `/output_topic` and collects received messages for validation. | ||
|
||
## Running the Unit Test | ||
|
||
1. **Build the Workspace**: | ||
Ensure the workspace is built and the environment is sourced: | ||
```bash | ||
cd ~/catkin_ws | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add instructions for running tests on a docker container (see package README). This seems to run on a host machine |
||
rosdep install --from-paths ~/catkin_ws/src --ignore-src --rosditro=noetic | ||
catkin clean | ||
catkin_build inorbit_republisher --verbose | ||
2. **Source and Run the Workspace**: | ||
```bash | ||
. ~/catkin_ws/devel/setup.bash | ||
rostest inorbit_republisher test_republisher.test | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: fix indentation |
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/usr/bin/env python | ||
# Unit test for a ROS node that republishes messages from /input_topic to /output_topic. | ||
# It uses the unittest framework and rostest for testing. | ||
|
||
import unittest | ||
import rospy | ||
from std_msgs.msg import String | ||
|
||
class TestRepublisher(unittest.TestCase): | ||
def setUp(self): | ||
# Method to set up ROS nodes used in this test. | ||
rospy.init_node('test_republisher', anonymous=True) | ||
self.test_pub = rospy.Publisher('/input_topic', String, queue_size=10) | ||
self.received_messages = [] | ||
rospy.Subscriber('/output_topic', String, self.callback) | ||
rospy.sleep(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you need a sleep here? Why? |
||
|
||
def callback(self, msg): | ||
# Method to save messages received on /output_topic. | ||
self.received_messages.append(msg.data) | ||
|
||
def test_message_republishing(self): | ||
# Method to send a message (key=value) to /input_topic and | ||
# check if the republisher correctly adds the prefix (input_to_output=) and sends it to /output_topic. | ||
test_message = "key=value" | ||
expected_message = f"input_to_output={test_message}" | ||
rospy.loginfo(f"Publishing: {test_message} to /input_topic") | ||
self.test_pub.publish(String(data=test_message)) | ||
rospy.sleep(1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you tried |
||
rospy.loginfo(f"Messages received: {self.received_messages}") | ||
self.assertIn(expected_message, self.received_messages) | ||
|
||
def test_no_message(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see the value of this test. |
||
# Method to check that self.received_messages is empty at the start of the test. | ||
self.assertEqual(len(self.received_messages), 0) | ||
|
||
if __name__ == '__main__': | ||
import rostest | ||
rostest.rosrun('my_ros_package', 'test_republisher', TestRepublisher) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<launch> | ||
<!-- Launch republisher node --> | ||
<node name="inorbit_republisher" pkg="inorbit_republisher" type="republisher.py"> | ||
<param name="config" textfile="$(find inorbit_republisher)/config/example.yaml" /> | ||
</node> | ||
|
||
<!-- Launch unit test --> | ||
<test test-name="test_republisher" pkg="inorbit_republisher" type="test_republisher.py" /> | ||
</launch> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These ignores shouldn't be needed if running on a docker container