Ask Your Question
9

Any good examples of ros unittests?

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

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

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).

Edit

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 (http://wiki.ros.org/unittest) 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
7

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

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

I've found some examples in https://github.com/ros/ros_tutorials/tree/hydro-devel/rospy_tutorials/test

for instance: https://github.com/ros/ros_tutorials/blob/hydro-devel/rospy_tutorials/test/test_add_two_ints.py

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!

Edit

I have also found this: https://github.com/jfstepha/test_knex_ros/blob/master/nodes/test_range_filter.py which I believe that shows a bit better how unittesting is carried out in a regular project.

edit flag offensive delete link more
21

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

mirzashah gravatar image

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

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 gtest...in 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. http://wiki.ros.org/unittest#ROS-Node-Level_Integration_Tests

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

Comments

1

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

Victor Gonzalez-Pacheco gravatar imageVictor Gonzalez-Pacheco ( 2013-10-27 23:45:20 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

4 followers

Stats

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

Seen: 8,364 times

Last updated: Oct 28 '13