2
$\begingroup$

I have a large number of tests in ROS2 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 ROS2 "way" to do this - something like the equivalent of ROS1's rostest?

Thanks!

$\endgroup$

2 Answers 2

3
$\begingroup$

In addition to the aforementioned launch_testing of the launch project whose goal is to provide "tools for launching multiple processes and for writing tests involving multiple processes" there is the launch_testing_ros of the launch_ros project. This is about "tools for launching ROS nodes and for writing tests involving ROS nodes".

So as long as you don't combine multiple nodes into a single process launch_testing_ros is probably the best solution for testing node interactions based on topics.

$\endgroup$
2
$\begingroup$

Generally speaking, you should try to make it so that your software isn't heavily reliant on ROS, to the point where you can test the core, nontrivial logic of your code without such an invasive dependency. This often means writing a library that does not rely on ROS at all and then writing a thin ROS wrapper around its runtime execution. The library's behaviour can be tested independently. This might be one way to get around your problem.

If that isn't possible, you should look into pytest's mock testing extension pytest-mock. Mock testing involves being able to make "mock" objects that isolate your dependencies; you can assign expected behaviour like method calls on them and match whether it works how you expect.

You can also use the launch testing package to run ROS 1-like rostests

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.