python unittest failure

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

prince gravatar image

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

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 https://drive.google.com/open?id=1KZOUQcTXiZIwy23KqWfK-bvs89i1xpuN.

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)
        self.param1='UNKNOWN'
        self.param2=0

    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):
        self.algo_param_pubs=None

    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):
        rospy.sleep(1)
        self.algo_param_pubs.publish(self.param_msg)
        self.assertEqual(self.algo_manager.get_algo_param2(), 100)
        self.assertEqual(self.algo_manager.get_algo_param1(), 'Paramtere1')

   def test_dummy_data(self):
        self.assertEqual(1,1)

if __name__=='__main__':
    import rostest
    rostest.rosrun('pkg_unittest_subs','Algo_param_unittest',Algo_param_unittest)

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/case.py", line 329, in run
    testMethod()
  File "/opt/ros/kinetic/lib/python2.7/dist-packages/rostest/runner.py", line 93, in fn
    self.fail(message)
  File "/usr/lib/python2.7/unittest/case.py", line 410, in fail
    raise self.failureException(msg)
--------------------------------------------------------------------------------

[ROSTEST]-----------------------------------------------------------------------

[testalgo_param_unittest_1][failed]

SUMMARY
 * RESULT: FAIL
 * TESTS: 0
 * ERRORS: 0
 * FAILURES: 1

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/loader.py", line 418, in loadTestsFromName
    addr.filename, addr.module)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 47, in importFromPath
    return self.importFromDir(dir_path, fqname)
  File "/usr/lib/python2.7/dist-packages/nose/importer.py", line 94, in importFromDir
    mod = load_module(part_fqname, fh, filename, desc)
  File "/home/karan/catkin_ws_m/src/pkg_unittest_subs/test/run_tests.py", 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 setup.py in the package as well!
  3. What is appropriate method to run the test cases? I am not sure ...
(more)
edit retag flag offensive close merge delete