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