diff --git a/autoware_utils_testing/CMakeLists.txt b/autoware_utils_testing/CMakeLists.txt index 1cc6667..fd10f95 100644 --- a/autoware_utils_testing/CMakeLists.txt +++ b/autoware_utils_testing/CMakeLists.txt @@ -10,7 +10,9 @@ ament_auto_add_library(${PROJECT_NAME} SHARED if(BUILD_TESTING) file(GLOB_RECURSE test_files test/*.cpp) - ament_auto_add_gtest(test_${PROJECT_NAME} ${test_files}) + ament_add_ros_isolated_gtest(test_${PROJECT_NAME} ${test_files}) + ament_target_dependencies(test_${PROJECT_NAME} std_msgs) + target_link_libraries(test_${PROJECT_NAME} ${PROJECT_NAME}) endif() ament_auto_package() diff --git a/autoware_utils_testing/README.md b/autoware_utils_testing/README.md index a38880a..e75f030 100644 --- a/autoware_utils_testing/README.md +++ b/autoware_utils_testing/README.md @@ -1,11 +1,19 @@ # autoware_utils_testing +## Notice + +**This package is under construction. All interfaces currently provided are unstable.** + ## Overview The **autoware_utils** library is a comprehensive toolkit designed to facilitate the development of autonomous driving applications. -This package provides essential utilities for visualization. -It is extensively used in the Autoware project to handle common tasks such as creating markers for RViz. +This package provides essential utilities for testing. +It is extensively used in the Autoware project to handle common tasks such as handling executor and mock node. -## Design +## Usage Example -- **`marker_helper.hpp`**: Helper functions for creating and manipulating visualization markers. +- See [test/main.cpp](./test/main.cpp) + - `gtest/ros_env.hpp` +- See [test/cases/mock_node.cpp](./test/cases/mock_node.cpp) + - `mock_node.hpp` + - `spin_thread.hpp` diff --git a/autoware_utils_testing/package.xml b/autoware_utils_testing/package.xml index df1f17e..b32dc41 100644 --- a/autoware_utils_testing/package.xml +++ b/autoware_utils_testing/package.xml @@ -13,8 +13,10 @@ gtest_vendor rclcpp + ament_cmake_ros ament_lint_auto autoware_lint_common + std_msgs ament_cmake diff --git a/autoware_utils_testing/test/cases/mock_node.cpp b/autoware_utils_testing/test/cases/mock_node.cpp new file mode 100644 index 0000000..1d48cf3 --- /dev/null +++ b/autoware_utils_testing/test/cases/mock_node.cpp @@ -0,0 +1,56 @@ +// Copyright 2025 The Autoware Contributors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include +#include + +#include +#include + +#include + +#include +#include + +TEST(TestDebugPublisher, Main) +{ + const auto mock = std::make_shared("mock_node"); + const auto node = std::make_shared("test_node"); + + auto pub1 = node->create_publisher("/test/foo", rclcpp::QoS(1)); + auto pub2 = node->create_publisher("/test/bar", rclcpp::QoS(1)); + auto sub1 = mock->sub("/test/foo", rclcpp::QoS(1)); + auto sub2 = mock->sub("/test/bar", rclcpp::QoS(1)); + + autoware_utils_testing::SpinThread thread; + thread.add_node(mock); + thread.add_node(node); + thread.start(); + pub1->publish(std_msgs::build().data(123)); + pub2->publish(std_msgs::build().data("hello")); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + pub1->publish(std_msgs::build().data(456)); + pub2->publish(std_msgs::build().data("world")); + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + thread.stop(); + + EXPECT_EQ(sub1->data.size(), 2); + EXPECT_EQ(sub1->data[0].data, 123); + EXPECT_EQ(sub1->data[1].data, 456); + + EXPECT_EQ(sub2->data.size(), 2); + EXPECT_EQ(sub2->data[0].data, "hello"); + EXPECT_EQ(sub2->data[1].data, "world"); +} diff --git a/autoware_utils_testing/test/main.cpp b/autoware_utils_testing/test/main.cpp index 7c283b2..eb8f344 100644 --- a/autoware_utils_testing/test/main.cpp +++ b/autoware_utils_testing/test/main.cpp @@ -12,10 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include +#include int main(int argc, char ** argv) { testing::InitGoogleTest(&argc, argv); + testing::AddGlobalTestEnvironment(autoware_utils_testing::gtest::ros_env(argc, argv)); return RUN_ALL_TESTS(); }