1import unittest 2 3# If we're using Python 2.6, add in more modern unittest convenience methods 4if not hasattr(unittest.TestCase, 'assertIn'): 5 def assertIn(self, member, container, msg=None): 6 return self.assertTrue(member in container, 7 msg or '%s not found in %s' % (member, container)) 8 def assertNotIn(self, member, container, msg=None): 9 return self.assertTrue(member not in container, 10 msg or '%s unexpectedly found in %s' 11 % (member, container)) 12 def assertIs(self, a, b, msg=None): 13 return self.assertTrue(a is b, msg or '%s is not %s' % (a, b)) 14 def assertIsInstance(self, obj, cls, msg=None): 15 return self.assertTrue(isinstance(obj, cls), 16 msg or '%s is not an instance of %s' % (obj, cls)) 17 def assertLess(self, a, b, msg=None): 18 return self.assertTrue(a < b, msg or '%s not less than %s' % (a, b)) 19 def assertGreater(self, a, b, msg=None): 20 return self.assertTrue(a > b, msg or '%s not greater than %s' % (a, b)) 21 def assertLessEqual(self, a, b, msg=None): 22 return self.assertTrue(a <= b, 23 msg or '%s not less than or equal to %s' % (a, b)) 24 def assertGreaterEqual(self, a, b, msg=None): 25 return self.assertTrue(a >= b, 26 msg or '%s not greater than or equal to %s' % (a, b)) 27 def assertIsNone(self, obj, msg=None): 28 return self.assertTrue(obj is None, msg or '%s is not None' % obj) 29 def assertIsNotNone(self, obj, msg=None): 30 return self.assertTrue(obj is not None, msg or 'unexpectedly None') 31 def assertAlmostEqual(self, first, second, places=None, msg=None, 32 delta=None): 33 if first == second: 34 return 35 if delta is not None and places is not None: 36 raise TypeError("specify delta or places not both") 37 diff = abs(first - second) 38 if delta is not None: 39 if diff <= delta: 40 return 41 standard_msg = ("%s != %s within %s delta (%s difference)" 42 % (first, second, delta, diff)) 43 else: 44 if places is None: 45 places = 7 46 if round(diff, places) == 0: 47 return 48 standard_msg = ("%s != %s within %r places (%s difference)" 49 % (first, second, places, diff)) 50 raise self.failureException(msg or standard_msg) 51 unittest.TestCase.assertIn = assertIn 52 unittest.TestCase.assertNotIn = assertNotIn 53 unittest.TestCase.assertIs = assertIs 54 unittest.TestCase.assertIsInstance = assertIsInstance 55 unittest.TestCase.assertLess = assertLess 56 unittest.TestCase.assertGreater = assertGreater 57 unittest.TestCase.assertLessEqual = assertLessEqual 58 unittest.TestCase.assertGreaterEqual = assertGreaterEqual 59 unittest.TestCase.assertIsNone = assertIsNone 60 unittest.TestCase.assertIsNotNone = assertIsNotNone 61 unittest.TestCase.assertAlmostEqual = assertAlmostEqual 62