【CMake系列】10-cmake测试 ctest

cmake作为一个强大的构建系统指导工具,同时也提供了测试功能,可用于项目的单元测试等,也可以与其他测试框架协作,如googletest,共同完成项目开发中的测试工作,本节我们就来学习 如何借助cmake 完成测试

本专栏的实践代码全部放在 github 上,欢迎 star !!!

如有问题,欢迎留言、或加群【392784757】交流

接下来给出我们本节的CMakeLists.txt 文件

cmake_minimum_required(VERSION 3.20)

project(test_ctest)

file(WRITE test_ctest.cpp [=[

#include <iostream>
using namespace std;

int main(int argc,char* argv[])
{
  if(argc <=1 ) // 没有传递参数
    return -1;
  cout<<argv[1]<<endl;
  return 0;
}
]=])

add_executable(test_ctest test_ctest.cpp)

 
# 测试用例
add_test(NAME test_return_-1 #
COMMAND test_ctest 
)

add_test(NAME test_return_0 #
COMMAND test_ctest test_return_0
)

##################
add_test(NAME test_success # 测试控制台输出
COMMAND test_ctest success

)

set_tests_properties(test_success
PROPERTIES
PASS_REGULAR_EXPRESSION success #成功字符串匹配正则

)

add_test(NAME test_failed # 测试控制台输出
COMMAND test_ctest failed

) 
set_tests_properties(test_failed
PROPERTIES
FAIL_REGULAR_EXPRESSION failed #成功字符串匹配正则

)

enable_testing()

接下来,我们将逐行进行解释,

首先,我们来明确一下测试,将我们的程序的输出与期待输出进行判断
程序的输出 在这里分为两种情况
程序运行 控制台输出
程序运行 返回值 (最常见的也就是 main 函数 返回 0 还是 -1)

也就是对程序的执行过程进行验证,是否符合我们的预期

cmake 测试

涉及命令

  • add_test()
add_test(NAME <name> 
COMMAND <command> [<arg>...] # 具体执行的命令 也就是 运行程序
[CONFIGURATIONS <config>...]
[WORKING_DIRECTORY <dir>]
[COMMAND_EXPAND_LISTS])
  • set_tests_properties()
    设定测试属性
    PASS_REGULAR_EXPRESSION 匹配失败的控制台输出
    FAIL_REGULAR_EXPRESSION 匹配成功的控制台输出

如下面的事例,
程序输出 success ,则程序运行符合逻辑,即测试通过
程序输出 failed,则程序运行不符合逻辑,测试未通过

add_test(NAME test_success # 测试控制台输出
COMMAND test_ctest success
)

set_tests_properties(test_success
PROPERTIES
PASS_REGULAR_EXPRESSION success #成功字符串 匹配正则
)

add_test(NAME test_failed # 测试控制台输出
COMMAND test_ctest failed

) 
set_tests_properties(test_failed
PROPERTIES
FAIL_REGULAR_EXPRESSION failed #成功字符串匹配正则
  • enable_testing()
    启用测试

构建过程

cmake -S . -B build

cmake --build build

cd build 
ctest -C Debug

输出测试结果
在这里插入图片描述

[main] 正在配置项目: lizhuo [proc] 正在执行命令: /usr/bin/cmake -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_C_COMPILER:FILEPATH=/usr/bin/gcc -DCMAKE_CXX_COMPILER:FILEPATH=/usr/bin/g++ --no-warn-unused-cli -S /home/lizhuo/ros2_ws/image_subscriber_cpp -B /home/lizhuo/build -G "Unix Makefiles" [cmake] Not searching for unused variables given on the command line. [cmake] -- Found ament_cmake: 1.3.12 (/opt/ros/humble/share/ament_cmake/cmake) [cmake] -- Found rclcpp: 16.0.14 (/opt/ros/humble/share/rclcpp/cmake) [cmake] -- Found rosidl_generator_c: 3.1.7 (/opt/ros/humble/share/rosidl_generator_c/cmake) [cmake] -- Found rosidl_adapter: 3.1.7 (/opt/ros/humble/share/rosidl_adapter/cmake) [cmake] -- Found rosidl_generator_cpp: 3.1.7 (/opt/ros/humble/share/rosidl_generator_cpp/cmake) [cmake] -- Using all available rosidl_typesupport_c: rosidl_typesupport_fastrtps_c;rosidl_typesupport_introspection_c [cmake] -- Using all available rosidl_typesupport_cpp: rosidl_typesupport_fastrtps_cpp;rosidl_typesupport_introspection_cpp [cmake] -- Found rmw_implementation_cmake: 6.1.2 (/opt/ros/humble/share/rmw_implementation_cmake/cmake) [cmake] -- Found rmw_fastrtps_cpp: 6.2.8 (/opt/ros/humble/share/rmw_fastrtps_cpp/cmake) [cmake] -- Using RMW implementation 'rmw_fastrtps_cpp' as default [cmake] -- Found sensor_msgs: 4.9.0 (/opt/ros/humble/share/sensor_msgs/cmake) [cmake] -- Found cv_bridge: 3.2.1 (/opt/ros/humble/share/cv_bridge/cmake) [cmake] -- Found ament_lint_auto: 0.12.14 (/opt/ros/humble/share/ament_lint_auto/cmake) [cmake] -- Added test 'cppcheck' to perform static code analysis on C / C++ code [cmake] -- Configured cppcheck include dirs: [cmake] -- Configured cppcheck exclude dirs and/or files: [cmake] -- Added test 'lint_cmake' to check CMake code style [cmake] -- Added test 'uncrustify' to check C / C++ code style [cmake] -- Configured uncrustify additional arguments: [cmake] -- Added test 'xmllint' to check XML markup files [cmake] -- Configuring done [cmake] -- Generating done [cmake] -- Build files have been written to: /home/lizhuo/build [ctest] 在测试资源管理器中找不到文件夹: /home/lizhuo/ros2_ws/image_subscriber_cpp
08-05
<think>我们正在处理一个ROS2 Humble项目配置问题:CMake配置成功,但CTest找不到测试文件夹。根据引用[1]和[3]中提到的测试框架(特别是CTest),我们将重点解决CTest的配置问题。 ## 问题分析 在ROS2项目中,使用CTest进行测试时,常见的“找不到测试文件夹”错误通常由以下原因引起: 1. **测试目录未正确添加到`CMakeLists.txt`** 2. **测试文件未被`CMakeLists.txt`包含** 3. **测试可执行文件未通过`add_test()`注册** 4. **构建目录结构问题导致测试路径错误** ## 解决方案 ### 步骤1:确保测试文件存在 在项目根目录下创建测试文件夹,例如`test`,并放置测试文件(如`test_example.cpp`): ``` your_package/ ├── CMakeLists.txt ├── package.xml ├── src/ └── test/ └── test_example.cpp ``` ### 步骤2:配置`CMakeLists.txt`启用测试 修改`CMakeLists.txt`,添加以下关键部分(参考引用[1]中CTest的使用): ```cmake # 启用测试 if(BUILD_TESTING) find_package(ament_cmake_test REQUIRED) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() # 添加测试可执行文件 add_executable(test_example test/test_example.cpp) target_link_libraries(test_example ${PROJECT_NAME}) ament_target_dependencies(test_example rclcpp std_msgs) # 注册测试 add_test(NAME test_example COMMAND test_example) target_include_directories(test_example PUBLIC include ${Boost_INCLUDE_DIRS}) endif() ``` ### 步骤3:正确设置测试工作目录 使用`set_property`指定测试运行的工作目录(避免因路径错误导致文件找不到): ```cmake set_property(TEST test_example PROPERTY WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test) ``` ### 步骤4:完整构建并运行测试 ```bash colcon build --packages-select your_package colcon test --packages-select your_package ``` ### 步骤5:检查测试结果 ```bash colcon test-result --verbose ``` ## 常见错误处理 - **错误:`No tests found!`** 检查`CMakeLists.txt`中是否遗漏`BUILD_TESTING`条件判断(ROS2中测试默认开启)。 - **错误:`test not run`** 确保测试可执行文件已正确编译(查看`build/your_package`目录下是否存在`test_example`)。 - **路径错误** 使用绝对路径替代相对路径:`WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/test`。 ## 示例测试文件 (`test/test_example.cpp`) ```cpp #include <gtest/gtest.h> TEST(TestSuite, testCase1) { EXPECT_EQ(1, 1); } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } ``` > **关键点**:ROS2 Humble使用`ament_cmake_test`作为测试基础框架,CTest是其底层驱动[^1]。通过`colcon test`命令触发CTest执行。 §§相关问题§§ 1. ROS2中如何配置GoogleTest单元测试? 2. 如何为ROS2节点编写集成测试? 3. CTest与ROS2的`ament_cmake_test`有何关系? 4. 如何调试ROS2测试中的路径错误?</think>### ROS 2 Humble C++项目CTest测试配置指南 在ROS 2 Humble中配置CTest时遇到"找不到测试文件夹"的问题,通常是由于CMake配置不完整或路径设置错误导致的。以下是系统化的解决方案: --- #### 1. **确认测试文件结构正确** ```bash your_package/ ├── CMakeLists.txt ├── package.xml ├── src/ └── test/ # 测试目录必须命名为test/ └── test_example.cpp # 测试文件 ``` 确保测试目录**严格命名为`test`**(小写),这是CTest的默认搜索目录[^1]。 --- #### 2. **更新`CMakeLists.txt`配置** 添加以下关键指令: ```cmake # 启用测试功能 if(BUILD_TESTING) # 包含测试目录 include_directories(test) # 添加测试可执行文件 add_executable(test_example test/test_example.cpp) target_link_libraries(test_example ${PROJECT_NAME} gtest gtest_main) # 注册CTest测试 add_test(NAME your_test_name COMMAND test_example) ament_add_gtest(test_example test/test_example.cpp) endif() ``` --- #### 3. **解决路径问题的关键配置** 在`CMakeLists.txt`中添加: ```cmake # 显式设置测试工作目录 set(TEST_WORKING_DIR ${CMAKE_CURRENT_SOURCE_DIR}/test) set_property(TEST test_example PROPERTY WORKING_DIRECTORY ${TEST_WORKING_DIR}) # 确保包含测试目录 file(GLOB TEST_SOURCES "test/*.cpp") ``` --- #### 4. **构建和测试命令** ```bash # 清理并重新构建 colcon build --packages-select your_package --cmake-clean-cache # 运行测试(两种方式) ctest -V # 详细模式 colcon test --packages-select your_package ``` --- #### 5. **常见错误排查** - **错误:`No tests found`** ```bash # 检查测试文件是否被编译 ls build/your_package/test_example ``` 解决方案:确保`add_executable`包含测试文件 - **错误:`Test directory not found`** 解决方案:在`package.xml`中添加测试依赖: ```xml <test_depend>ament_cmake_gtest</test_depend> ``` - **环境问题**:确保重新source环境 ```bash source install/setup.bash ``` --- #### 6. **测试文件示例 (`test/test_example.cpp`)** ```cpp #include <gtest/gtest.h> #include "your_package/your_header.hpp" // 被测头文件 TEST(TestSuite, testCase1) { EXPECT_EQ(1, 1); // 简单测试用例 } int main(int argc, char **argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } ``` > **关键点**:ROS 2使用`ament_cmake`作为构建系统,CTest是其底层测试驱动[^1]。当配置正确时,`colcon test`会自动调用CTest执行测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值