Contents
Standard Test Nodes
hztest
hztest allows you test the publishing rate of a node. It reads in parameters that specify the topic name, rate, and error bounds of the test, and will succeed if the topic matches that specification. It can also be used to test for no publication, i.e. 0Hz. By default, hztest only tests the overall, average publication rate. This can be changed by setting the ~check_intervals parameter.
Here is a sample configuration:
<launch> <node name="talker" pkg="test_ros" type="talker.py" /> <param name="hztest1/topic" value="chatter" /> <param name="hztest1/hz" value="10.0" /> <param name="hztest1/hzerror" value="0.5" /> <param name="hztest1/test_duration" value="5.0" /> <test test-name="hztest_test" pkg="rostest" type="hztest" name="hztest1" /> </launch>
selftest_test
selftest_test, in the self_test package, runs a self-test and checks that it passes. Please see the self_test package for more documentation.
paramtest
New in Kinetic (? Addition to prior distros is discussed)
Similar to hztest, paramtest allows you test if certain parameters are registered at the Parameter Server. It reads in parameters that specify the parameter name, and optionally its value, and then will succeed if the parameter matches that specification.
Here 3 different configurations samples; :
<!-- Test if a specific parameter is not empty. --> <test pkg="rostest" type="paramtest" name="paramtest_nonempty" test-name="paramtest_nonempty"> <param name="param_name_target" value="param_nonempty" /> <param name="test_duration" value="5.0" /> <param name="wait_time" value="20.0" /> </test> <!-- Test if a specific parameter is empty. --> <test pkg="rostest" type="paramtest" name="paramtest_empty" test-name="paramtest_empty"> <param name="param_name_target" value="param_empty" /> <param name="test_duration" value="5.0" /> <param name="wait_time" value="30.0" /> </test> <!-- Test if a specific parameter carries a specific value. --> <test pkg="rostest" type="paramtest" name="paramtest_value_specific_correct" test-name="paramtest_value_specific_correct"> <param name="param_name_target" value="param_value_specific" /> <param name="param_value_expected" value="Opensource Robotics is forever." /> <param name="test_duration" value="5.0" /> <param name="wait_time" value="30.0" /> </test>
publishtest
New in Kinetic
publishtest is a node to test if specified topics are published at least once. This can be used for testing topics that are latched or those of which publish rate is uneven, and also supports testing multiple topics with a single testcase.
Here is a sample of testing some nodes:
<launch> <node name="talker_0" pkg="rospy" type="talker.py"> <remap from="chatter" to="~output" /> </node> <node name="talker_1" pkg="rostest" type="just_advertise"> <rosparam> msg_name: std_msgs/String </rosparam> </node> <test name="publishtest" test-name="publishtest" pkg="rostest" type="publishtest"> <rosparam> topics: - name: talker_0/output timeout: 10 negative: False - name: talker_1/output timeout: 3 negative: True # means it is not published until the timeout </rosparam> </test> </launch>
Note:
"topics" element in <rosparam> is required, as well as its child elements (name, timeout, negative).
You can still substitute values in <rosparam> by using arg tag but along with "subst_value" attribute (as required in <rosparam> tag).
advertisetest
New in Melodic
advertisetest is a node that checks if the specified topics or services are advertised, the below parameters must be set. This node supports testing multiple topics and services with a single testcase.
Here is a sample of testing some nodes:
<launch> <node pkg="rostest" type="talker.py" name="freq_topic_pub"/> <node pkg="rostest" type="publish_once.py" name="once_topic_pub"/> <node pkg="rostest" type="service_server.py" name="service_server"/> <test test-name="advertisetest_test" pkg="rostest" type="advertisetest" time-limit="7.0" retry="3"> <rosparam> topics: - name: /chatter timeout: 2. - name: /once_topic type: std_msgs/Bool timeout: 2. - name: /advertised_topic timeout: 2. negative: true services: - name: /empty timeout: 2. - name: /set_bool type: std_srvs/SetBool timeout: 2. - name: /unadvertised_service timeout: 2. negative: true </rosparam> </test> </launch>
Note:
This example came from the unit_test of this node, check the test link (as advertisetest testcase)
Execution of rostest tests
As first step to get familiar with writing rostest tests it is helpful to execute and investigate the tests of rostest's own tests.
Preconditions to run the tests
To being able to run the tests of rostest the following steps are required.
Clone ros_comm into the catkin workspace:
cd <catkin-workspace>/src git clone https://github.com/ros/ros_comm.git
Build ros_comm:
cd <catkin-workspace> catkin_make --pkg ros_comm
Get a list of all make targets of rostest (they will be used later):
catkin_make run_tests_rostest_ (and invoke tab-completion) run_tests_rostest_gtest run_tests_rostest_gtest_test_permuter run_tests_rostest_rostest run_tests_rostest_rostest_test_clean_master.test run_tests_rostest_rostest_test_distro_version.test run_tests_rostest_rostest_test_hztest0.test run_tests_rostest_rostest_test_hztest.test run_tests_rostest_rostest_test_param.test run_tests_rostest_rostest_test_publishtest.test
Execute hztest tests
The hztest node implementation is located in <catkin-workspace>/src/ros_comm/tools/rostest/nodes/hztest. The tests of the hztest node are implemented in <catkin-workspace>/src/ros_comm/tools/rostest/test/hztest0.test and in <catkin-workspace>/src/ros_comm/tools/rostest/test/hztest.test. W.r.t. to usual taxonomy in software testing hztest acts as a "mock topic subscriber node" and verifies if talker.py, the "node under test" (topic publisher) publishes the topics as expected. The implementation of talker.py is located in <catkin-workspace-root-directory>/src/ros_comm/clients/rospy/test_nodes/talker.py.
Running the hztest tests gives you a first idea about how the node has to be used in own tests. You can run the hztest tests in rostest as follows:
cd <catkin-workspace-root-directory> catkin_make run_tests_rostest_rostest_test_hztest.test catkin_make run_tests_rostest_rostest_test_hztest0.test
Implementation of hztest in the ros_comm package (lunar)
Execute paramtest tests
The paramtest node implementation is located in <catkin-workspace>/src/ros_comm/tools/rostest/nodes/paramtest. The test of the paramtest node is implemented in <catkin-workspace>/src/ros_comm/tools/rostest/test/param.test. Running the paramtest test gives you a first idea about how the node has to be used in own tests. You can run the paramtest test in rostest as follows:
cd <catkin-workspace> catkin_make run_tests_rostest_rostest_test_param.test
Implementation of paramtest in the ros_comm package (lunar)
ROS node integration testing
The GitHub repository "Ros-Test-Example" (branch: master) contains an example for rostest, gtest and rviz based ROS node integration testing. The hztest test node is used to verify the publishing of car related data and to verify the publishing of simulated car related data. The example is described in PDF slides. The same repository shows how to use gmock based mocks for the car and the road in "Ros-Test-Example" (branch: mocking).