警告
您正在閱讀已達到生命週期結束(end-of-life,EOL)版本的 ROS 2 文件,該版本不再正式受支援。如果您想獲取最新資訊,請參見 Kilted。
Writing Basic Tests with C++ with GTest
Starting point: we’ll assume you have a basic ament_cmake package set up already and you want to add some tests to it.
In this tutorial, we’ll be using gtest.
Package Setup
來源碼
We’ll start off with our code in a file called test/tutorial_test.cpp
#include <gtest/gtest.h>
TEST(package_name, a_first_test)
{
ASSERT_EQ(4, 2 + 2);
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
CMakeLists.txt
if(BUILD_TESTING)
find_package(ament_cmake_gtest REQUIRED)
ament_add_gtest(${PROJECT_NAME}_tutorial_test test/tutorial_test.cpp)
target_include_directories(${PROJECT_NAME}_tutorial_test PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)
ament_target_dependencies(${PROJECT_NAME}_tutorial_test
std_msgs
)
target_link_libraries(${PROJECT_NAME}_tutorial_test name_of_local_library)
endif()
The testing code is wrapped in the if/endif
block to avoid building tests where possible. ament_add_gtest
functions much like add_executable
so you’ll need to call target_include_directories
, ament_target_dependencies
and target_link_libraries
as you normally would.
執行測試
See the tutorial on how to run tests from the command line for more information on running the tests and inspecting the test results.