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

Any good examples of ros unittests?

asked 2013-10-25 23:51:03 -0600

updated 2013-10-27 23:50:06 -0600

Hi all,

I have started to write some unittests in Python for my nodes and I would like to use existing tests as a model.

I have some experience with GTest (a little less with Python's unittest) but I had never tried to integrate my tests with ROS. Some questions for which I did not find answers in the documentation are: what kind of things should be tested in rostest level? parameters? subscriptions? the code itself? And how are these usually tested? For instance, are the parameters loaded in the rostest launch file or in your Python/C++ code? Even more, how Node-Level and Code-Level tests should be integrated?

I am looking for tests that could help me with the understanding of these concepts (and that maybe could be used as examples in the unittest's tutorials section).


To sum up my question, I am looking for examples of python's unittest that could be good candidates to put them in the tutorials section ( In essence these examples should illustrate proper ways of using unittest to test the correctness of your nodes. Eg: loading/setting parameters, initialization, publishers/subscribers, etc.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2013-10-28 00:17:24 -0600

updated 2013-10-28 01:08:28 -0600

I've found some examples in

for instance:

I suppose that these are enough to start. However, if anyone knows better examples and he/she is willing to share them, please feel free to post them!


I have also found this: which I believe that shows a bit better how unittesting is carried out in a regular project.

edit flag offensive delete link more

answered 2013-10-26 04:17:13 -0600

mirzashah gravatar image

updated 2013-10-26 04:20:47 -0600

ROS is agnostic to which unit testing framework you use, but most folks use gtest for C++ and unittest for Python. For Python, nosetest (an abstraction on top of unittest) is often used as well.

Integration tests are actually just an extension of unit testing in ROS and is handled through rostest. rostest is simply a wrapper around roslaunch. You can add <test> tags anywhere inside a standard ROS launch file to add additional nodes that are launched specifically for testing. roslaunch ignores these tags, but rostest does not. If you use rostest, it will bring up all the regular nodes (tag <node>) plus the test nodes (tag <test>) specified in the launch file. Usually launch files meant for testing have the .test extension.

Within your test nodes, you simply use a unit testing framework to report to rostest the results of the test. If you're in C++, it's easiest to use Python use unitest. rostest then intercepts the output of the test nodes when you exit and compiles it into a single integration test report in XML (I believe it uses the JUnit encoding for this).

This is where the examples are for Python. They're mostly correct but a little dated and I think there's a mistake at the end -- I don't have the chance to fix that now but will make a note to.

In general, for both Python unit and integration tests you want to do the following:

  1. Create a class that subclasses from unittest.TestCase
  2. Have a bunch of methods with the prefix "test_". Each represents a test case
  3. Implement your tests in these methods using the assert*() calls (e.g.self.assertTrue)
  4. Integration tests are the same except you create a ROS node handle within the test case and start publishing/subscribing. You perform checks in the exact same way as you do with unittest
  5. For integration tests, you also want to add a "add_rostest" to your CMakeLists.txt file.
  6. catkin_make run_tests in your catkin workspace

I can't find any good examples right now, maybe somebody can jump in and comment.

edit flag offensive delete link more



Thanks mirzashah! This is a really good point to start.

Victor Gonzalez-Pacheco gravatar image Victor Gonzalez-Pacheco  ( 2013-10-27 23:45:20 -0600 )edit

Question Tools



Asked: 2013-10-25 23:51:03 -0600

Seen: 13,294 times

Last updated: Oct 28 '13