Skip to content
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

Open
wants to merge 5 commits into
base: noetic-devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
__pycache__/
/inorbit_republisher/test/sample_data/build/
Copy link
Member

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

/inorbit_republisher/test/sample_data/hardcodedRosbag/
/inorbit_republisher/test/sample_data/install/
/inorbit_republisher/test/sample_data/log/
.history/
10 changes: 10 additions & 0 deletions inorbit_republisher/config/example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ republishers:
topic: "/inorbit/custom_data/0"
key: "navsat"

# Añadido para los tópicos input y output
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid using language other than english

- topic: "/input_topic"
Copy link
Member

Choose a reason for hiding this comment

The 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. unittest_config.yaml under the test folder, and only include this section.

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"
Expand Down
31 changes: 31 additions & 0 deletions inorbit_republisher/test/UnitTests/README.md
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
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: fix indentation

```
39 changes: 39 additions & 0 deletions inorbit_republisher/test/UnitTests/test_republisher.py
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need a sleep here? Why?
Note that sleeps or wait times on unittests are typically a smell and are discouraged.


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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you tried rospy.wait_for_message instead of the sleep?

rospy.loginfo(f"Messages received: {self.received_messages}")
self.assertIn(expected_message, self.received_messages)

def test_no_message(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the value of this test. self.received_messages is initialized as an empty list on the setUp, so it will always have len == 0.

# 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)
9 changes: 9 additions & 0 deletions inorbit_republisher/test/UnitTests/test_republisher.test
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>