This repository provides a suite of tools and utilities tailored for testing and debugging ROS 2 (Robot Operating System) applications. It aims to simplify the development and testing workflows for ROS 2-based projects, particularly in scenarios involving unit and integration testing.
The tools in this repository address challenges posed by ROS 2's inter-process communication, which can lead to inconsistent test results. By focusing on integration testing without revalidating the underlying RMW (ROS Middleware) implementations, this repository ensures a more streamlined and reliable testing process.
This framework enables writing reliable, fully repeatable unit tests (and more) for C++ ROS 2 implementations, eliminating the issue of so-called "flaky tests".
This repository and tooling was initally developed as a collaboration between BEAM and Spyrosoft; and is maintained as a collaboration.
- Verifying whether the tested Node has created the necessary entities such as publishers, subscribers, timers, services clients, services providers, action servers or action clients.
- Setting expectations on mocked entities, enabling verification of events such as publishing expected messages on a selected topic, sending a request by a client or validating a service response, sending action goals or verifying action feedback and results.
- Direct message passing to subscribers, or direct request injection to services and responding to clients.
- Direct timer callbacks firing and simulated time control, resulting in immediate and precise time-dependent implementations testing.
- Single-threaded, controllable test execution.
LIMITATION: The
rtestframework has a significant limitation, in order to test the implementation against ROS 2, access to the source code of those components is required. They need to be directly included in the test build process. For example: testing interactions with components coming from external dependencies, such as those installed from system repositories, is not possible. In such cases, the source code of those packages must be downloaded and included in the workspace source tree.
Complete documentation: Rtest Documentation
- rclcpp
- GoogleTest
- ament_cmake_ros
- Clone the repository:
$ git clone https://github.com/yourusername/rtest.git
- Build and run the test examples:
$ colcon build --cmake-args="-DRTEST_BUILD_EXAMPLES=On" $ colcon test --packages-select rtest_examples --event-handlers console_cohesion+
Add a dependency to rtest in your package.xml file:
<package format="3">
...
<test_depend>rtest</test_depend>
</package>Create a sub-folder test and add a CMakeLists.txt file there.
WARNING: The Rtest uses C++ template code substitution at the source level. You must build the unit under test directly from sources. Linking with a static or dynamic library will not work.
Example CMakeLists.txt:
find_package(ament_cmake_gmock REQUIRED)
find_package(rtest REQUIRED)
ament_add_gmock(${PROJECT_NAME}-test
main.cpp
unit_tests.cpp
${CMAKE_SOURCE_DIR}/src/ros2_node.cpp
)
target_link_libraries(${PROJECT_NAME}-test
rtest::publisher_mock
rtest::subscription_mock
rtest::timer_mock
rtest::service_mock
rtest::service_client_mock
rtest::action_server_mock
rtest::action_client_mock
)
target_link_libraries(${PROJECT_NAME}-test
rclcpp::rclcpp
)The testing library uses Google Mocking framework and requires initialization:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <rclcpp/rclcpp.hpp>
int main(int argc, char **argv) {
testing::InitGoogleMock(&argc, argv);
rclcpp::init(argc, argv);
// Run all tests
int result = RUN_ALL_TESTS();
rclcpp::shutdown();
return result;
}The framework allows mocking of the following ROS 2 components:
rclcpp::TimerBaserclcpp::Publisherrclcpp::Subscriptionrclcpp::Servicerclcpp::Clientrclcpp_action::Server(experimental)rclcpp_action::Client(experimental)
Testing ROS 2 components follows a two-step approach:
Use the appropriate find* function to locate the component created by your node:
findTimersfor timersfindPublisherfor publishersfindSubscriptionfor subscriptionsfindServicefor service providersfindClientfor service clientsexperimental::findActionClientfor action clientsexperimental::findActionServerfor action servers
Once the interesting mocked entities are found, user can set up call expectaions or interact with them directly.
Examples are located in the examples folder.
This project is licensed under the APACHE 2.0 License. See the LICENSE file for details.