Go to the documentation of this file.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 = 'self_test'
00039 import roslib; roslib.load_manifest(PKG)
00040
00041 SRV_NAME = 'my_node/self_test'
00042
00043 import unittest
00044 import rospy, rostest
00045
00046 import sys
00047 from optparse import OptionParser
00048
00049 from diagnostic_msgs.srv import SelfTest, SelfTestRequest, SelfTestResponse
00050
00051 class TestSelfTest(unittest.TestCase):
00052 def __init__(self, *args):
00053 super(TestSelfTest, self).__init__(*args)
00054
00055 parser = OptionParser(usage="usage ./%prog [options]", prog="test_selftest.py")
00056 parser.add_option('--no-id', action="store_true",
00057 dest="no_id", default=False,
00058 help="No ID expected from self test")
00059 parser.add_option('--expect-fail', action="store_true",
00060 dest="expect_fail", default=False,
00061 help="Self test should fail")
00062 parser.add_option('--exception', action="store_true",
00063 dest="exception", default=False,
00064 help="Self test should throw exception and we should get error message")
00065
00066 parser.add_option('--gtest_output', action="store",
00067 dest="gtest")
00068
00069 options, args = parser.parse_args()
00070
00071 self.no_id = options.no_id
00072 self.expect_fail = options.expect_fail
00073 self.exception = options.exception
00074
00075
00076 def test_self_test(self):
00077 proxy = rospy.ServiceProxy(SRV_NAME, SelfTest)
00078
00079 try:
00080 rospy.wait_for_service(SRV_NAME, 15)
00081 except Exception, e:
00082 self.assert_(False, "Service %s did not respond. Unable to test self_test" % SRV_NAME)
00083
00084 try:
00085 res = proxy()
00086 except Exception, e:
00087 import traceback
00088 self.assert_(False, "Error calling self_test service. Exception: %s" % traceback.format_exc())
00089
00090 if self.no_id:
00091 self.assert_(res.id == '', "Result had node ID even though ID was not expected. ID: %s" % res.id)
00092 else:
00093 self.assert_(res.id != '', "Result had no node ID")
00094
00095 if self.expect_fail or self.exception:
00096 self.assert_(res.passed == 0, "Self test passed, but it shouldn't have. Result: %d" % res.passed)
00097
00098 max_val = 0
00099 for tst in res.status:
00100 max_val = max(max_val, tst.level)
00101
00102 self.assert_(max_val > 0, "Self test failed, but no sub tests reported a failure or warning")
00103 else:
00104 self.assert_(res.passed, "Self test failed, but we expected a pass")
00105
00106 for tst in res.status:
00107 self.assert_(tst.level == 0, "Self test subtest failed, but we marked it as a pass")
00108
00109
00110 if self.exception:
00111 found_ex = False
00112 for tst in res.status:
00113 if tst.message.find('exception') > -1:
00114 found_ex = True
00115
00116 self.assert_(found_ex, "Self test threw and exception, but we didn't catch it and report it")
00117
00118
00119 if __name__ == '__main__':
00120 rostest.run(PKG, sys.argv[0], TestSelfTest, sys.argv)