ros2 test_error ai_generated true

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

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

Also available as: JSON · Markdown · 中文
85%Fix Rate
85%Confidence
1Evidence
2023-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
ros2:humble active
ros2:iron active
colcon-core:0.14.0 active
pytest:7.4.0 active

Root Cause

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

中文

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

Official Documentation

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

Workarounds

  1. 85% success Ensure setup.cfg in the package root contains a [tool:pytest] section and test paths are listed. Example: [tool:pytest] testpaths = test Then run: colcon test --packages-select my_package
    Ensure setup.cfg in the package root contains a [tool:pytest] section and test paths are listed. Example:
    [tool:pytest]
    testpaths = test
    
    Then run: colcon test --packages-select my_package
  2. 80% success Add a test_depend entry for pytest in package.xml and ensure a conftest.py or __init__.py exists in the test directory: <test_depend>python3-pytest</test_depend> Then rebuild: colcon build --packages-select my_package && colcon test
    Add a test_depend entry for pytest in package.xml and ensure a conftest.py or __init__.py exists in the test directory:
    <test_depend>python3-pytest</test_depend>
    
    Then rebuild: colcon build --packages-select my_package && colcon test

中文步骤

  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

Dead Ends

Common approaches that don't work:

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

    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% fail

    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% fail

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