There are ways to connect to the rostest's roscore, which is restarted for each test in order to have a clean test fixture.
Indigo and later
By default rostest uses a random port so many rostests can be run in parallel.
Re-use an already-running master
This behavior can be overridden with the reuse-master flag, and then the standard ROS_MASTER_URI will be used for the test.
Start a roscore in another terminal, then:
rostest --reuse-master foo_package foo.test
See also this question thread and this pull request.
Connect to rostest's master
Bit hacky but it's possible. While rostest is running, run a command something like:
grep -r "Registering with master node" ~/.ros/log/latest
This will return the master's URL with the port number. Then update your ROS_MASTER_URI:
NOTE: Again, port number differs per every rostest run. When you're running rostest by integrated way (e.g. catkin_make run_tests), multiple rostests can be included and running at the same time, so that you might see multiple different port numbers in the grep result above. Choose the one you want to connect to carefully.
In hydro and earlier rostest uses port 22422 to not conflict with any running roscore instance. Thus, to connect to the rostest Master from another terminal you must first:
Tests without rostest
A different approach to connecting to the volatile rostest roscore is to run your test file using roslaunch. The <test...> portions of the test file will not run, so you then manually launch your test node to observe its behavior. For example if the foo.test consisted of the following:
<launch> <node name="foo_node" pkg="foo_package" type="foo_node" output="screen"> </node> <test test-name="foo_node-test" pkg="foo_package" type="foo_node-test" /> </launch>
Launch the node and the test separately:
roslaunch foo_package foo.test
Then in another terminal:
rosrun foo_package foo_node-test