Execute Unit Tests with Python 3

asked 2020-04-20 03:26:21 -0500

Felix Widmaier gravatar image

updated 2020-04-20 03:55:20 -0500

I use catkin as build system for a package using Python 3. So far this has not been a problem as our code does not really use ROS at all (except from catkin).

Now I added Python unit tests using the following structure:

#!/usr/bin/env python3
import unittest

class TestFoobar(unittest.TestCase):

    def test_foo(self):

if __name__ == "__main__":
    import rosunit
    rosunit.unitrun("my_package", 'test_foobar', TestFoobar)

When I execute the test directly (e.g. via rosrun my_package test_foobar.py), the test is running with Python 3 and all is good. However, when running catkin run_tests, the shebang seems to be ignored and the test is executed with Python 2 (which fails due to incompatibilities).

Unfortunately, I'm still stuck with ROS Kinetic on Ubuntu 16.04.

So the actual question: Is there a way with Kinetic to make the tests being executed with Python 3?

I already tried adding -DPYTHON_EXECUTABLE=/usr/bin/python3 which works for building but does not seem to have an effect on the test.

edit retag flag offensive close merge delete


Does wiki/UsingPython3 discuss this at all?

If not: would be good to add.

gvdhoorn gravatar image gvdhoorn  ( 2020-04-20 06:00:12 -0500 )edit

UsingPython3/SourceCodeChanges mentions "In general, tests and CMake logic should invoke scripts with python2 or python3 rather than relying on the shebang." at the bottom but without mentioning on how I can change which version it uses. I also found out about ROS_PYTHON_VERSION there and tried setting it to 3 without success (not sure if this variable is even considered at all by Kinetic?).

Felix Widmaier gravatar image Felix Widmaier  ( 2020-04-20 06:26:30 -0500 )edit

Hi Felix, Did you find a solution for this? I'm getting the same problem. Thanks.

Dwil gravatar image Dwil  ( 2020-11-09 08:57:19 -0500 )edit

Unfortunately not. I'm simply running the tests manually for now. I'll anyway move to ROS2 soon, which hopefully should fix this problem.

Felix Widmaier gravatar image Felix Widmaier  ( 2020-11-09 09:58:12 -0500 )edit