Tutorials for unit and integration testing of ROS scripts and packages

Those from programming background understand the importance of unit testing and integration testing, even for ROS programs. But quite many ROS users are beginners in programming/CS. Some have their first interaction with programming while exploring ROS and writing their first few node scripts.

A good set of tutorials with examples on how to use unittest and rostest would improve code quality of ROS packages.

9 Likes

A few samples from Nav2

System “smoke” testing: navigation2/nav2_system_tests at main · ros-planning/navigation2 · GitHub
Unit testing: navigation2/nav2_smac_planner/test at main · ros-planning/navigation2 · GitHub
Plugins testing: navigation2/nav2_behavior_tree/test at main · ros-planning/navigation2 · GitHub

1 Like

I agree with your suggestion and i also think that a tutorial on how to debug / how to setup an IDE to debug ROS would be also very helpful.

1 Like

I recommend creating an issue on the ROS 2 Documentation repository about the absence of a tutorial. Make sure to include all of the relevant resources you have found on the subject (i.e. do your research, check the wiki, Google a bunch, don’t just ask for a tutorial).

As to IDEs, well, I’ve never used one with ROS, unless you consider Emacs an IDE. I know Visual Code has a lot of docs but I find IDEs to be more trouble than their worth. What debugging tools do you need above and beyond PDB, GDB, Valgrind, etc? A few of these topics fall under the rubric of general programming knowledge more so than ROS specifically.

3 Likes

I think this is a great idea. Tutorials would be really helpful. Or at least a list or summary of what is available? A day ago a co-worker asked about tutorials for implementations of navigation IDEs for mobile robots using ROS.

What is a Navigation IDE? I have never seen such a thing.

Sure I can do that. What is the way to do this for ROS1? I see the equivalent repo for ROS1 isn’t active.

Since I am playing with the testing packages at the moment, I might write tutorials on my own to help out people, if that is allowed. (I have edited a few tutorial/wiki pages, but I’m not sure if I can create new ones.)

I have the same view. I use VS Code as my text editor and never required an IDE for all the work I do.
However, I’d love to hear from people who do, who can tell me what I’m missing out :slight_smile:

I think they meant navigation packages rather than IDE? In which case ROS has great wiki, a lot of Q&A, and there is also a lot of external documentation/tutorials for it

Hello I think it refers to the IDEs that can be used from the browser as there are several
but I don’t know if they can be used in Ros

this might help,

best of luck

1 Like

One trick for unit testing python nodes without bringing up a whole node is to mock out all the rclpy calls so that you only ever have to test your logic. We often break our nodes up into the “ROS interface” and “Main logic”, especially after migrating from ROS1 to ROS2 so these tests only ever hit our interface code.

The unit tests get a bit verbose this way, but places a lot less load on our CI system. We then have separate integration tests for actually bringing up the node against a real ROS instance

@patch('my_package.my_node.Node.__init__')
@patch('my_package.my_node.Node.declare_parameter')
@patch('my_package.my_node.Node.get_parameter')
@patch('my_package.my_node.Node.get_name')
@patch('my_package.my_node.Node.create_service')
@patch('my_package.my_node.Node.create_subscription')
@patch('my_package.my_node.Node.create_publisher')
@patch('my_package.my_node.Node.create_timer')
@patch('my_package.my_node.Node.create_client')
@patch('my_package.my_node.Node.get_logger')
@patch('my_package.my_node.Node.get_clock')
class TestMyNode(TestCase):
    def test__init_node(self,
                        clock_mock,
                        logger_mock,
                        service_client_mock,
                        create_timer_mock,
                        create_publisher_mock,
                        create_subscription_mock,
                        create_service_mock,
                        get_name_mock,
                        get_param_mock,
                        declare_param_mock,
                        node_init_mock):
        node = MyNode()

        self.assertIsNotNone(node)
2 Likes

David Wilson has a fantastic YouTube channel called System Crafters for all of you Emacs lovers out there:

https://www.youtube.com/c/SystemCrafters/playlists

We have some instructions for Autoware.Auto testing here Integration Testing covering unit and integration tests

Similar to what @matthews-jca mentioned for python, we also recommend splitting the ROS pub/sub logic from the algorithms Guidelines and Best Practices

and we recently added convenience functionality to
create smoke integration tests
to bring up the node in it’s own executable as well as component tests that you can build in C++ to bring up nodes without having multiple processes to deal with, thereby reducing the flakiness.