# colcon test：未找到包 'my_package' 的 pytest 框架

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

## 根因

colcon test 需要在 setup.cfg 或 package.xml 中配置测试框架（pytest 或 unittest）；缺少或配置错误会导致测试发现失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| ros2:humble | active | — | — |
| ros2:iron | active | — | — |
| colcon-core:0.14.0 | active | — | — |
| pytest:7.4.0 | active | — | — |

## 解决方案

1. ```
   确保包根目录下的 setup.cfg 包含 [tool:pytest] 部分并列出测试路径。示例：
[tool:pytest]
testpaths = test

然后运行：colcon test --packages-select my_package
   ```
2. ```
   在 package.xml 中添加 pytest 的 test_depend 条目，并确保 test 目录中存在 conftest.py 或 __init__.py：
<test_depend>python3-pytest</test_depend>

然后重新构建：colcon build --packages-select my_package && colcon test
   ```

## 无效尝试

- **Reinstalling pytest with pip without checking package configuration** — pytest is already installed; the issue is in the package's test configuration, not the framework itself. (70% 失败率)
- **Adding a dummy test file without updating setup.cfg** — colcon uses setup.cfg or package.xml to discover tests; a test file alone is insufficient. (85% 失败率)
- **Running pytest directly instead of colcon test** — pytest may work locally but colcon test uses a different discovery mechanism; this doesn't fix the colcon integration. (60% 失败率)
