ROS Resources: Documentation | Support | Discussion Forum | Service Status | Q&A answers.ros.org
Ask Your Question
0

How to test the private functions inside a ROS node?

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

jotator gravatar image

Hello,

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

Comments

1

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

gvdhoorn gravatar imagegvdhoorn ( 2019-01-23 05:10:40 -0500 )edit

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

jotator gravatar imagejotator ( 2019-01-23 05:19:13 -0500 )edit
1

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

gvdhoorn gravatar imagegvdhoorn ( 2019-01-23 05:24:12 -0500 )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 imagejotator ( 2019-01-23 05:40:51 -0500 )edit
1

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 imagegvdhoorn ( 2019-01-23 06:52:06 -0500 )edit
1

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

gvdhoorn gravatar imagegvdhoorn ( 2019-01-23 06:58:30 -0500 )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 imageThomas D ( 2019-01-23 09:08:04 -0500 )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 imagejotator ( 2019-01-23 09:11:47 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

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

Link gravatar image

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

A setup similar to the following may work for you.

test.h

#define STATIC_FN

my_header.h

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

my_source.c

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

Comments

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 imagejotator ( 2019-01-23 06:09:43 -0500 )edit

Fixed it. Sorry about that.

Link gravatar imageLink ( 2019-01-23 06:15:07 -0500 )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 imagejotator ( 2019-01-23 09:02:27 -0500 )edit

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

Link gravatar imageLink ( 2019-01-23 09:11:58 -0500 )edit

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

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

3 followers

Stats

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

Seen: 75 times

Last updated: Jan 23