python unittest failure

asked 2020-01-02 03:27:51 -0600

prince gravatar image

updated 2020-01-02 03:31:57 -0600

Greetings, I am unable to execute the unittest in python nodes. System: Ubuntu 16.04, ROS Kinetic

The wiki pages do not provides a sample test code.Sample packages to reproduce the error is available at

For a simple subscriber of a message:

#!/usr/bin/env python
import rospy
from test_msg.msg import algo_param

class Algo_param_manager:
    def __init__(self):
        self.msg_subs = rospy.Subscriber('/algo_param_topic', algo_param, self.algo_param_cb)

    def algo_param_cb(self, algo_param_msg):
        self.param1 = algo_param_msg.param1
        self.param2 = algo_param_msg.param2 

    def get_algo_param1(self):
        return self.param1

    def get_algo_param2(self):
        return self.param2

I have written a simple unittest:

#!/usr/bin/env python
import rospy
import unittest
from test_msg.msg import algo_param
from pkg_unittest_subs.scripts.msg_subscribe import Algo_param_manager

class Algo_param_unittest(unittest.TestCase):
    def __init__(self):

    def setUp(self):
        rospy.init_node('algo_param_message_test_node', anonymous=True)
        self.param_msg = algo_param()
        self.param_msg.param1 = 'Parameter1'
        self.param_msg.param2 = 100

        self.algo_param_pubs = rospy.Publisher('/algo_param_topic',algo_param)
        self.algo_manager = Algo_param_manager()

    def test_input_data_consistency(self):
        self.assertEqual(self.algo_manager.get_algo_param2(), 100)
        self.assertEqual(self.algo_manager.get_algo_param1(), 'Paramtere1')

   def test_dummy_data(self):

if __name__=='__main__':
    import rostest

I am facing the following errors

$rostest pkg_unittest_subs algo_unittest.test 
... logging to /home/cair/.ros/log/rostest-cair-CELSIUS-R940-29561.log
[ROSUNIT] Outputting test results to /home/cair/.ros/test_results/pkg_unittest_subs/rostest-test_algo_unittest.xml
[Testcase: testalgo_param_unittest_1] ... FAILURE!
FAILURE: Test node [pkg_unittest_subs/algo_param_unittest] does not exist or is not executable
  File "/usr/lib/python2.7/unittest/", line 329, in run
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rostest/", line 93, in fn
  File "/usr/lib/python2.7/unittest/", line 410, in fail
    raise self.failureException(msg)



 * TESTS: 0
 * ERRORS: 0

ERROR: The following tests failed to run:
 * testalgo_param_unittest_1

Second instruction from Wiki:

 $catkin build pkg_unittest_subs --catkin-make-args run_tests

ERROR: Failure: ImportError (cannot import name Algo_param_manager)
Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/nose/", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/karan/catkin_ws_m/src/pkg_unittest_subs/test/", line 5, in <module>
    from pkg_unittest_subs import Algo_param_manager
ImportError: cannot import name Algo_param_manager

Ran 1 test in 0.037s

I request you to share the following:

  1. Correct way to formulate the unittest for python code based nodes? I have googled substantially to get a standard correct answer.
  2. How can I correct the attached code? I have tried compiling (catkin build), hoping that python files will get appropriately installed, hence available in the PYTHONPATH. I have tried keeping in the package as well!
  3. What is appropriate method to run the test cases? I am not sure ...
edit retag flag offensive close merge delete