1import unittest 2 3 4class TestHashing(object): 5 """Used as a mixin for TestCase""" 6 7 # Check for a valid __hash__ implementation 8 def test_hash(self): 9 for obj_1, obj_2 in self.eq_pairs: 10 try: 11 if not hash(obj_1) == hash(obj_2): 12 self.fail("%r and %r do not hash equal" % (obj_1, obj_2)) 13 except KeyboardInterrupt: 14 raise 15 except Exception, e: 16 self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e)) 17 18 for obj_1, obj_2 in self.ne_pairs: 19 try: 20 if hash(obj_1) == hash(obj_2): 21 self.fail("%s and %s hash equal, but shouldn't" % 22 (obj_1, obj_2)) 23 except KeyboardInterrupt: 24 raise 25 except Exception, e: 26 self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) 27 28 29class TestEquality(object): 30 """Used as a mixin for TestCase""" 31 32 # Check for a valid __eq__ implementation 33 def test_eq(self): 34 for obj_1, obj_2 in self.eq_pairs: 35 self.assertEqual(obj_1, obj_2) 36 self.assertEqual(obj_2, obj_1) 37 38 # Check for a valid __ne__ implementation 39 def test_ne(self): 40 for obj_1, obj_2 in self.ne_pairs: 41 self.assertNotEqual(obj_1, obj_2) 42 self.assertNotEqual(obj_2, obj_1) 43 44 45class LoggingResult(unittest.TestResult): 46 def __init__(self, log): 47 self._events = log 48 super(LoggingResult, self).__init__() 49 50 def startTest(self, test): 51 self._events.append('startTest') 52 super(LoggingResult, self).startTest(test) 53 54 def startTestRun(self): 55 self._events.append('startTestRun') 56 super(LoggingResult, self).startTestRun() 57 58 def stopTest(self, test): 59 self._events.append('stopTest') 60 super(LoggingResult, self).stopTest(test) 61 62 def stopTestRun(self): 63 self._events.append('stopTestRun') 64 super(LoggingResult, self).stopTestRun() 65 66 def addFailure(self, *args): 67 self._events.append('addFailure') 68 super(LoggingResult, self).addFailure(*args) 69 70 def addSuccess(self, *args): 71 self._events.append('addSuccess') 72 super(LoggingResult, self).addSuccess(*args) 73 74 def addError(self, *args): 75 self._events.append('addError') 76 super(LoggingResult, self).addError(*args) 77 78 def addSkip(self, *args): 79 self._events.append('addSkip') 80 super(LoggingResult, self).addSkip(*args) 81 82 def addExpectedFailure(self, *args): 83 self._events.append('addExpectedFailure') 84 super(LoggingResult, self).addExpectedFailure(*args) 85 86 def addUnexpectedSuccess(self, *args): 87 self._events.append('addUnexpectedSuccess') 88 super(LoggingResult, self).addUnexpectedSuccess(*args) 89 90 91class ResultWithNoStartTestRunStopTestRun(object): 92 """An object honouring TestResult before startTestRun/stopTestRun.""" 93 94 def __init__(self): 95 self.failures = [] 96 self.errors = [] 97 self.testsRun = 0 98 self.skipped = [] 99 self.expectedFailures = [] 100 self.unexpectedSuccesses = [] 101 self.shouldStop = False 102 103 def startTest(self, test): 104 pass 105 106 def stopTest(self, test): 107 pass 108 109 def addError(self, test): 110 pass 111 112 def addFailure(self, test): 113 pass 114 115 def addSuccess(self, test): 116 pass 117 118 def wasSuccessful(self): 119 return True 120