# Automatically specify the port for rostest

Hello ROS community.

I am writing unit tests for a functionality that monitors the status of the roscore and performs certain steps in case the roscore stops working (properly). Now, one of the tests I have tests the behavior of complete roscore crash. To do that I do the following:

1. add_custom_target with a command that spawns a roscore and make it a dependency to the test,
2. In the test I use --reuse-master flag to attach to the spawned roscore,
3. I kill the attached roscore within the test to test if the functionality works properly.

All of that looks something like this:

  add_custom_target(custom_command_${PROJECT_NAME} COMMAND nohup roscore > /dev/null 2>&1 & sleep 1 ) add_executable(${PROJECT_NAME}-rostest)  #  ... link libraries etc. Here the roscore is being killed by grep-ing for it's pid

ARGS --reuse-master --clear
DEPENDENCIES custom_command_${PROJECT_NAME}${PROJECT_NAME}-rostest
)


Now, I would like to have control of on which port to spawn the roscore, since I don't want to use the default port. Adapting the command to do so is straightforward - simply adding a -p and a port number, but then --reuse-master does not connect to the desired port. Setting ROS_MASTER_URI with the same port before running

  catkin run_tests


solves the problem, but I would like to avoid setting the ROS_MASTER_URI every time I run the test and instead simply hard code the port I want to use. So I would like to know if there is a way of explicitly setting the port for --reuse-master to connect to, or setting the ROS_MASTER_URI for the test case from within the CMakeLists?

edit retag close merge delete