ros2 test_error ai_generated true

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

colcon test: Failed to find pytest framework for package 'my_package'

ID: ros2/colcon-test-no-pytest-framework

其他格式: JSON · Markdown 中文 · English
85%修复率
85%置信度
1证据数
2023-06-15首次发现

版本兼容性

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

根因分析

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

English

colcon test requires a test framework (pytest or unittest) to be configured in setup.cfg or package.xml; missing or misconfigured entry causes test discovery failure.

generic

官方文档

https://docs.ros.org/en/humble/Tutorials/Intermediate/Testing/Testing-Main.html

解决方案

  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

无效尝试

常见但无效的做法:

  1. Reinstalling pytest with pip without checking package configuration 70% 失败

    pytest is already installed; the issue is in the package's test configuration, not the framework itself.

  2. Adding a dummy test file without updating setup.cfg 85% 失败

    colcon uses setup.cfg or package.xml to discover tests; a test file alone is insufficient.

  3. Running pytest directly instead of colcon test 60% 失败

    pytest may work locally but colcon test uses a different discovery mechanism; this doesn't fix the colcon integration.