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

python rostest fails to import python module

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

vvdy gravatar image


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:

|— src
    |— package_name
|— test
    |— mytests.test
|— CMakeLists.txt
|— package.xml

The (I think) relevant code is the following:

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)


    <test test-name="node1_tests" pkg="package_name" type=""/>

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 "" 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/", 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/", line 329, in run
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rostest/", 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/", line 422, in assertTrue
    raise self.failureException(msg)
edit retag flag offensive close merge delete


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 in the src folder, only in the modules subfolders.

kartikmohta gravatar image kartikmohta  ( 2018-06-20 01:11:38 -0600 )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 locations. That too didn't change anything though.

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

Where is the go_heading module imported? In 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 -0600 )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 -0600 )edit

1 Answer

Sort by » oldest newest most voted

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

kartikmohta gravatar image

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


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

# fetch values from package.xml
setup_args = generate_distutils_setup(
    package_dir={'': 'src'},


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

See the pages and for more details.

edit flag offensive delete link more

Question Tools



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

Seen: 1,473 times

Last updated: Jun 21 '18