Run Rostest in CI Using ROS1 Melodic and Docker


The Goal

I have a project with several ROS modules that I would like to test together using Rostest. The documentation for Rostest seems pretty straightforward and I was fairly quickly able to get a test node running that would:

  • Start my ROS node that I wanted to test.

  • Start my test node which sent some messages from other ROS nodes in my project to the Node under test

  • Verify the output messages of my node under test.

Now I wanted to run that as part of my continuous integration system in the cloud. I build and deploy this software using Docker, so doing the tests inside of the Docker container was another small piece of the puzzle.

The Struggle

I ran my tests locally using the rostest command which worked great.

Now I was like cool, I’ll just run this simple little test in CI easy peasy, no problem. The first challenge is that I wanted to have one command that would run all my tests. Ok cool, let me just google what the best way to do that is.

The first issue with the rostest command is that you have to explicitly declare each test file you would like to run.

For example:

rostest blep.test

I wanted to have one command that would run all the tests in my repository.

It seems that catkin has a facility for doing this once I add each test to the

catkin_make run_tests

Oh what’s this? Now my test node can’t seem to link against my custom messages properly. Hmmm wonder why that is…

2 hours later…

The Solution

I spent an inordinate amount of time trying to find a concise answer for how to make this work. The key ended up being buried in the comments of an older Github issue. See my solution below.

# Run the Rostests# catkin_make_isolated must be called in order to resolve an error in rostest.# See https://github.com/ros/catkin/issues/841#issuecomment-275467002sRUN /bin/bash -c '. /opt/ros/melodic/setup.bash; cd /usr/src/catkin_ws; catkin_make_isolated'RUN /bin/bash -c '. /opt/ros/melodic/setup.bash; cd /usr/src/catkin_ws; catkin_make_isolated --make-args run_tests'# catkin_test_results will return non-zero to fail the build if the tests don't passRUN /bin/bash -c '. /opt/ros/melodic/setup.bash; cd /usr/src/catkin_ws; catkin_test_results'

That’s it! Hopefully, that’s helpful to someone.

Keenan Johnson