# colcon test：在'build/my_package/test_results'中未找到测试输出文件

- **ID:** `ros2/colcon-test-missing-test-output`
- **领域:** ros2
- **类别:** test_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

测试运行器（如pytest、gtest）未在预期目录中生成输出文件，因为测试未能运行、被跳过，或CMake/ament测试配置未正确设置输出生成。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ROS2 Humble | active | — | — |
| ROS2 Iron | active | — | — |
| ROS2 Rolling | active | — | — |

## 解决方案

1. ```
   检查测试可执行文件是否存在并可运行：'colcon test --packages-select my_package'，并在控制台输出中查找错误消息。如果是C++ gtest，确保在CMakeLists.txt中使用了'add_rostest'或'ament_add_gtest'。
   ```
2. ```
   使用'--cmake-args -DBUILD_TESTING=ON'重新构建包，然后重新运行测试。这确保测试目标被构建。
   ```
3. ```
   对于Python包，确保'setup.py'或'setup.cfg'中有指向正确测试目录的'test'部分，并从工作区根目录运行'colcon test'。
   ```

## 无效尝试

- **Running 'colcon test' with '--event-handlers console_direct+' to see all output, but still no test results** — This only increases verbosity; it does not fix the underlying issue of tests not producing output files. (80% 失败率)
- **Manually creating the test_results directory to satisfy colcon** — The directory must contain actual test output files; an empty directory does not resolve the error. (95% 失败率)
