ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

How to test the private functions inside a ROS node?

asked 2019-01-23 04:38:24 -0600

jotator gravatar image


I have a package that contains a single executable which relies on the messages implemented in another three packages and I would like to test this executable. So far I think I understand how to test the node itself with the help of rostest, but first I need to have tests for a handful of functions I have inside the node. These functions are supposed to be private (delcared as static inside the node's source file), so they are not part of any API I could expose to the test driver.

How could I go about it? The tests would be extremely simple, the functions just take a ROS message object as input and provide a plain C structure with the contents of the message, so the test would simply input a ROS message and check that the output parameters correspond to those in the input message.

edit retag flag offensive close merge delete



Do you have any ideas how to approach this when it would be anything else (ie: not a ROS node)?

gvdhoorn gravatar image gvdhoorn  ( 2019-01-23 05:10:40 -0600 )edit

No. I checked the resources available in the ROS Wiki and these examples: , but they all seem to either assume a library with a public interface or a running node.

jotator gravatar image jotator  ( 2019-01-23 05:19:13 -0600 )edit

I wasn't asking about how to do it "within ROS" :)

gvdhoorn gravatar image gvdhoorn  ( 2019-01-23 05:24:12 -0600 )edit

Sorry, I was also talking about the gtest-only resources included in the wiki (the Level1 testing with no rostest). These haven't been very helpful so far, to be honest.

jotator gravatar image jotator  ( 2019-01-23 05:40:51 -0600 )edit

No need to apologise. What I wanted to get across is that ROS is not special here, and afaict does not change anything that you couldn't use when testing functions/methods local to a compilation unit without involving ROS.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-23 06:52:06 -0600 )edit

See this SO post and this thread on reddit for some common approaches. this is nice.

gvdhoorn gravatar image gvdhoorn  ( 2019-01-23 06:58:30 -0600 )edit

Can you get to your private functions via callbacks from services or subscribers? If so, then rostest provides a way to have a test node that can publish messages or call servers in a way that will invoke functions triggered by your callbacks. If that is useful I can put links.

Thomas D gravatar image Thomas D  ( 2019-01-23 09:08:04 -0600 )edit

Only one of the functions is passed as a callback for the reception of a message in a given topic, the rest are called periodically by main(), so I don't think I can benefit from it. Still, could you please post the links? Sounds like an interesting piece of information.

jotator gravatar image jotator  ( 2019-01-23 09:11:47 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2019-01-23 05:52:34 -0600

Link gravatar image

updated 2019-01-23 09:30:19 -0600

A setup similar to the following may work for you.


#define STATIC_FN


#ifdef TESTING
#  include "test.h"
#  define STATIC_FN static
STATIC_FN int doSomething();


STATIC_FN int doSomething() {
   // some code
   return something;
edit flag offensive delete link more


The functions I mention aren't methods inside a class, they are standard C functions declared as static. All examples I have seen assume that you have some header file with declarations to these functions that you can call in the test executable. What if you don't have them?

jotator gravatar image jotator  ( 2019-01-23 06:09:43 -0600 )edit

Fixed it. Sorry about that.

Link gravatar image Link  ( 2019-01-23 06:15:07 -0600 )edit

Thanks for the answer, but I saw elsewhere that this approach is very unsafe because it would completely break functions that use static local variables.

jotator gravatar image jotator  ( 2019-01-23 09:02:27 -0600 )edit

Yup. In that case I think I will delete this answer.

Link gravatar image Link  ( 2019-01-23 09:11:58 -0600 )edit

I made a completely new answer, using similar concepts. I hope it helps!

Link gravatar image Link  ( 2019-01-23 09:31:46 -0600 )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



Asked: 2019-01-23 04:38:24 -0600

Seen: 363 times

Last updated: Jan 23 '19