ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

python rostest fails to import python module

asked 2018-06-19 14:31:24 -0500

vvdy gravatar image

Hello,

I'm using ROS kinetic on an ubuntu 16.04 machine and have been trying to use rostest to run unit tests (and hopefully integration tests in the future). I keep running into problems when trying to import the python classes I'm testing.

Following instructions in the rostest wiki, my file structure is currently as follows:

package_name
|— __init__.py
|— src
    |— __init__.py
    |— package_name
        |—__init__.py
        |— node1.py
|— test
    |— __init__.py
    |— test_node1.py
    |— mytests.test
|— CMakeLists.txt
|— package.xml

The (I think) relevant code is the following:

test_node1.py:

import rospy
import unittest
import io
from package_name.src.package_name.node1 import Node1Class

PKG = 'package_name'


class TestNode1Class(unittest.TestCase):
    """Test Node1Class."""


if __name__ == '__main__':
    import rostest
    rostest.rosrun(PKG, 'node1_tests', TestNode1Class)

mytests.test:

<launch>
    <test test-name="node1_tests" pkg="package_name" type="test_node1.py"/>
</launch>

When I run

rostest package_name mytests.test

It fails with the error

ImportError: No module named src.package_name.node1

It does not mention the top level ROS package name, which makes me think that it does find that put then for some reason is not able to go down the package tree?

However, when I move "test_node1.py" up one level, so that it sits in the root of the ROS package everything does run perfectly (at that point it sits at the same level as the "src" package.

I am pretty new to ROS and the rostest documentation is not very extensive. I feel like there is some path setting I may be missing or did not include?

The full traceback for the error is as follows:

... logging to /home/vagrant/.ros/log/rostest-ubuntu-xenial-29567.log
[ROSUNIT] Outputting test results to /home/vagrant/.ros/test_results/package_name/rostest-test_package_name.xml
Traceback (most recent call last):
  File "/home/vagrant/fs_dev/dev_ws/src/package_name/test/test_node1.py", line 11, in <module>
    from package_name.src.package_name.node1 import Node1Class
ImportError: No module named src.package_name.node1
[Testcase: testnode1_tests] ... FAILURE!
FAILURE: test [node1_tests] did not generate test results
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rostest/runner.py", line 164, in fn
    self.assert_(os.path.isfile(test_file), "test [%s] did not generate test results"%test_name)
  File "/usr/lib/python2.7/unittest/case.py", line 422, in assertTrue
    raise self.failureException(msg)
edit retag flag offensive close merge delete

Comments

I believe it should work by replacing from package_name.src.package_name.node1 import Node1Class with from package_name.node1 import Node1Class.

Just as a note, the current convention for python packages in ROS is to not have a __init__.py in the src folder, only in the modules subfolders.

kartikmohta gravatar image kartikmohta  ( 2018-06-20 01:11:38 -0500 )edit

Sadly that doesn't change anything. When I change the import to from package_name.node1 import Node1Class the error just changes to No module named go_heading. Thanks for the heads up on __init__.py locations. That too didn't change anything though.

vvdy gravatar image vvdy  ( 2018-06-20 16:57:06 -0500 )edit

Where is the go_heading module imported? In node1.py? Is it present in some other ros package? That seems to be a different issue, right?

kartikmohta gravatar image kartikmohta  ( 2018-06-20 17:41:13 -0500 )edit

I'm sorry. The error is No module named node1 I was trying to use generic names for clarity but mistyped in my comment.

vvdy gravatar image vvdy  ( 2018-06-20 19:12:21 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2018-06-21 01:31:36 -0500

kartikmohta gravatar image

Just noticed that the package is missing a setup.py file, you need to create the setup.py in your package root folder containing:

## ! DO NOT MANUALLY INVOKE THIS setup.py, USE CATKIN INSTEAD

from distutils.core import setup
from catkin_pkg.python_setup import generate_distutils_setup

# fetch values from package.xml
setup_args = generate_distutils_setup(
    packages=['package_name'],
    package_dir={'': 'src'},
)

setup(**setup_args)

and then you need to add a call to catkin_python_setup() in your CMakeLists.txt.

See the pages http://wiki.ros.org/rospy_tutorials/T... and http://docs.ros.org/api/catkin/html/u... for more details.

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2018-06-19 14:31:24 -0500

Seen: 1,566 times

Last updated: Jun 21 '18