Skip to content

ROS2 Python Unit Test

base on

Simple very simple demo

Show how to use pytest in ros2 python package using ament_cmake

package

Using ament_cmake to create a python package

1
2
3
4
5
6
7
├── CMakeLists.txt
├── package.xml
├── ros_py
│   ├── __init__.py
│   └── simple_node.py
└── test
    └── test_simple.py

pytest test

simple_node.py
def test_math():
    assert 2 + 2 == 4

CMakeLists.txt

  • Add ament_add_pytest_test to the CMakeLists.txt file to run the test
1
2
3
4
if(BUILD_TESTING)
  find_package(ament_cmake_pytest REQUIRED)
  ament_add_pytest_test(test_math test/test_simple.py)
endif()

using colcon

1
2
3
4
5
# run package test
colcon test --packages-select ros_py  --event-handlers console_direct+

# run specific test
colcon test --packages-select ros_py --pytest-args -k test_math --event-handlers console_direct+

Reference