ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Writing tests in ROS 2

asked 2023-03-09 14:00:42 -0600

jbarry gravatar image

updated 2023-03-10 12:50:01 -0600

kscottz gravatar image


I have a large number of tests in ROS 2 in Python I would like to write that are essentially 1. Publish a message 2. Subscribe and do something 3. Check what happens during the "do something"

I can do this by creating the publisher and subscriber in the same piece of code. However, it's closer to what happens "for real" if I publish in one process and subscribe in another. But that doesn't fit in well with pytest. Is there a ROS 2 "way" to do this - something like the equivalent of ROS1's rostest?


edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2023-03-10 19:49:03 -0600

stevebillings gravatar image

I’m thinking that the important thing to test is the “do something” code, and not ROS’s publish and subscribe mechanisms (or the little bit of probably-not-going-to-change-very-often client code that calls them). To test the “do something” code, I’d be inclined to write a set of tests where each test constructs a different message (the data structure), passes it to your “do something” code as input, and verifies whatever the “do something” code produces in response. Would that serve your objectives?

edit flag offensive delete link more


I do test the "do something" code separately of course. But if I leave the wrappers untested then the only way I find bugs in them is by starting up a whole pipeline. This is especially bad with ROS2's emphasis on asynchronicity where multi-process tests can be very helpful to track down race conditions and deadlocks without having to start a whole simulator or run on hardware.

jbarry gravatar image jbarry  ( 2023-03-13 07:22:48 -0600 )edit

Question Tools



Asked: 2023-03-09 14:00:42 -0600

Seen: 315 times

Last updated: Mar 10 '23