00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 PKG = 'rospy_tutorials'
00039 NAME = 'add_two_ints_test'
00040
00041 import roslib; roslib.load_manifest(PKG)
00042
00043 import sys
00044 import unittest
00045
00046 import rospy
00047 import rostest
00048 from rospy_tutorials.srv import *
00049
00050 class TestAddTwoInts(unittest.TestCase):
00051
00052 def test_add_two_ints(self):
00053 rospy.wait_for_service('add_two_ints')
00054 s = rospy.ServiceProxy('add_two_ints', AddTwoInts)
00055 tests = [(1, 2), (0, 0), (-1, -2), (12312, 980123), (sys.maxint, -sys.maxint), (sys.maxint, -1), (sys.maxint, 0)]
00056 for x, y in tests:
00057 print "Requesting %s+%s"%(x, y)
00058
00059 resp = s(x, y)
00060 resp2 = s.call(AddTwoIntsRequest(x, y))
00061 self.assertEquals(resp.sum,resp2.sum)
00062 print "%s+%s = %s"%(x, y, resp.sum)
00063 self.assertEquals(resp.sum,(x + y), "integration failure, returned sum was %s vs. %s"%(resp.sum, (x+y)))
00064
00065 def test_add_two_ints_bad_then_good(self):
00066 rospy.wait_for_service('add_two_ints')
00067 try:
00068 s = rospy.ServiceProxy('add_two_ints', BadTwoInts)
00069 resp = s(1, 2)
00070 self.fail("service call should have failed with exception but instead returned 1+2=%s"%resp.sum)
00071 except rospy.ServiceException, e:
00072 print "success -- ros exception was thrown: %s"%e
00073 s = rospy.ServiceProxy('add_two_ints', AddTwoInts)
00074 resp = s.call(AddTwoIntsRequest(1, 2))
00075 self.assertEquals(3,resp.sum)
00076
00077
00078 def test_add_two_ints_bad_type(self):
00079 rospy.wait_for_service('add_two_ints')
00080 s = rospy.ServiceProxy('add_two_ints', BadTwoInts)
00081 tests = [(1, 2), (0, 0), (-1, -2), (12312, 980123), (sys.maxint, -sys.maxint), (sys.maxint, -1), (sys.maxint, 0)]
00082 for x, y in tests:
00083 print "Requesting %s+%s"%(x, y)
00084
00085 try:
00086 resp = s(x, y)
00087 if resp.sum == x+y:
00088 self.fail("call 1 with bad type failed: the server appears to be incorrectly deserialing the packet as it returned: %s"%resp.sum)
00089 else:
00090 self.fail("call 1 with bad type failed to throw exception: %s"%resp.sum)
00091 except rospy.ServiceException, e:
00092 print "success -- ros exception was thrown: %s"%e
00093 try:
00094 resp = s.call(BadTwoIntsRequest(x, y))
00095 if resp.sum == x+y:
00096 self.fail("call 2 with bad type failed: the server appears to be incorrectly deserialing the packet as it returned: %s"%resp.sum)
00097 else:
00098 self.fail("call 2 with bad type failed to throw exception: %s"%resp.sum)
00099 except rospy.ServiceException, e:
00100 print "success -- ros exception was thrown: %s"%e
00101
00102 if __name__ == '__main__':
00103 rostest.rosrun(PKG, NAME, TestAddTwoInts, sys.argv)