Robotics StackExchange | Archived questions

Pass parameters from launch file to python script in Unit Test(Rostest)

I am working on implementing a rostest whereby i can pass parameters from a roslaunch file to my python script for testing. However my test is failing, and normally it should pass. I will paste my code here and also will be glad to get some feedback on why this is happening.

final goal will be to have one generic python template that many launch files can be used to do integration tes

my script reading parameters from the roslaunch file.

class itn_rtest_group1template(unittest.TestCase):
success = False
global topic1
global datatype1
global topic2
global datatype2
topic1=rospy.get_param("/topic1")
datatype1=rospy.get_param('/datatype1')
topic2=rospy.get_param("/topic2")
datatype2=rospy.get_param("/datatype2")

print(topic1,datatype1,topic2,datatype2)
#def __init__(self, *args, **kwargs):
    #rospy.loginfo("topic1: %s, datatype1: %s, topic2: %s, datatype2: %s", topic1,  datatype1, topic2, datatype2)
def callback(self,topic1,datatype1,topic2,datatype2):
    # This is the callback function for Group 1 testcase
    #self.rospy.loginfo('topic1 = %s', topic1)
    #self.rospy.loginfo('topic1 = %s', topic2)
    rospy.loginfo(rospy.get_caller_id() + "topic1: %s, datatype1: %s, topic2: %s, datatype2: %s",topic1,datatype1,topic2,datatype2)
    self.success = True
def test_WI_group1template(self):
    #This is the python Unit test generic template implementation
    rospy.init_node('WI_group1template', anonymous=True)  
    #rospy.loginfo("topic1: %s, datatype1: %s, topic2: %s, datatype2: %s", topic1,  datatype1, topic2,datatype2 )
    #print("start integration testing node")
    rospy.Subscriber("topic_name", String,self.callback,(topic1, datatype1))
    rospy.Subscriber("topic_name", String, self.callback,(topic2, datatype2))
    timeout_t = time.time() + 5.0
    while (not rospy.is_shutdown() and time.time() < timeout_t and(not self.success)):
        time.sleep(0.1)
    self.assert_(self.success)

if name == 'main': import rosunit try: rostest.rosrun(PKG, 'WIgroup1template', itnrtest_group1template) except rospy.ROSInterruptException: pass

<launch>
 <param name="topic1"  value="chatter"/>
 <param name="datatype1" value="String" />
 <param name="topic2" value="gemoto"/>
 <param name="datatype2" value="String"/>
 <node name="WI_group1template" pkg="itn_rtest" type="int_testtemplate_group1.py" output="screen" 
  clear_params="true"/>   
<test test-name="WI_XXXX" pkg="itn_rtest" type="int_testtemplate_group1.py" >    </test>  </launch>

ERROR log "False is not true File "/usr/lib/python2.7/unittest/case.py", line 329, in run testMethod() File "/home/user/integrationtest/src/itnrtest/test/inttesttemplategroup1.py", line 65, in testWIgroup1template self.assert(self.success) File "/usr/lib/python2.7/unittest/case.py", line 422, in assertTrue raise self.failureException(msg) ------------------------------------------------------------------"

Asked by Buldoza on 2020-02-18 13:24:50 UTC

Comments

Answers