What is the recommended pytest workflow for an ament_cmake_python package?


I am seeking a recommendation for the following situation. I have a ROS2 package (humble) that only contains some python code, see it here.

I would like to add tests to it.

Build type: ament_cmake

Build Tool Dependencies:

Yes, I know the ament_cmake_python docs say to not to do this, however it’s an upsteam package, that’s just the convention of the package. There is no information the ament_cmake_python docs on how to add tests for python code.

According to the colcon docs, I need to put my tests folder matching the format of tests/test_FOO.py, so I did that.

Next, I inferred I need to declare a dependency on pytest like so:

When I run “colcon test --packages-select my_package”, the python tests are not recognized or run.

Is it required in the build situation to also utilize the ament_cmake_pytest package?

If so, this seems like a big disadvantage, and the tutorial about automatically finding the python tests don’t work in this build type.

Can I get a recommendation on how I should proceed?

This should probably be asked on answers.ros.org instead, but take a look at this package: ros2_tracing/test_tracetools at rolling · ros2/ros2_tracing · GitHub. In short, add your test_*.py files under a test/ directory and use ament_cmake_pytest’s ament_add_pytest_test() function, e.g.: ros2_tracing/CMakeLists.txt at 0d79e0210eb976ed94dd30c640432a1b4bd76766 · ros2/ros2_tracing · GitHub

The ament_cmake_python user documentation page should be updated to cover this. I’ve opened an issue for it: Mention how to deal with Python tests in ament_cmake package · Issue #3215 · ros2/ros2_documentation · GitHub

1 Like

Thank you, that worked. I’ll follow up with the Github issue.