python rostest fails to import python module
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)
I believe it should work by replacing
from package_name.src.package_name.node1 import Node1Class
withfrom 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 thesrc
folder, only in the modules subfolders.Sadly that doesn't change anything. When I change the import to
from package_name.node1 import Node1Class
the error just changes toNo module named go_heading
. Thanks for the heads up on__init__.py
locations. That too didn't change anything though.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?I'm sorry. The error is
No module named node1
I was trying to use generic names for clarity but mistyped in my comment.