test_selftest.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2010, Willow Garage, Inc.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of the Willow Garage nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 #
00034 
00035 ##\author Kevin Watts
00036 ##\brief Tests for valid self test calls
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         # Option comes with rostest, will fail w/o this line
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)


self_test
Author(s): Kevin Watts, Brice Rebsamen , Jeremy Leibs and Blaise Gassend
autogenerated on Fri Aug 28 2015 10:30:02