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 | |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 44 | class _BaseLoggingResult(unittest.TestResult): |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 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') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 55 | super().startTestRun() |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 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') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 63 | super().stopTestRun() |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 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') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 71 | super().addSuccess(*args) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 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') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 79 | super().addSkip(*args) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 80 | |
| 81 | def addExpectedFailure(self, *args): |
| 82 | self._events.append('addExpectedFailure') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 83 | super().addExpectedFailure(*args) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 84 | |
| 85 | def addUnexpectedSuccess(self, *args): |
| 86 | self._events.append('addUnexpectedSuccess') |
Antoine Pitrou | c9b3ef2 | 2013-03-20 20:16:47 +0100 | [diff] [blame] | 87 | super().addUnexpectedSuccess(*args) |
| 88 | |
| 89 | |
| 90 | class LegacyLoggingResult(_BaseLoggingResult): |
| 91 | """ |
| 92 | A legacy TestResult implementation, without an addSubTest method, |
| 93 | which records its method calls. |
| 94 | """ |
| 95 | |
| 96 | @property |
| 97 | def addSubTest(self): |
| 98 | raise AttributeError |
| 99 | |
| 100 | |
| 101 | class LoggingResult(_BaseLoggingResult): |
| 102 | """ |
| 103 | A TestResult implementation which records its method calls. |
| 104 | """ |
| 105 | |
| 106 | def addSubTest(self, test, subtest, err): |
| 107 | if err is None: |
| 108 | self._events.append('addSubTestSuccess') |
| 109 | else: |
| 110 | self._events.append('addSubTestFailure') |
| 111 | super().addSubTest(test, subtest, err) |
Michael Foord | 2560e5c | 2010-03-27 12:34:21 +0000 | [diff] [blame] | 112 | |
| 113 | |
| 114 | class ResultWithNoStartTestRunStopTestRun(object): |
| 115 | """An object honouring TestResult before startTestRun/stopTestRun.""" |
| 116 | |
| 117 | def __init__(self): |
| 118 | self.failures = [] |
| 119 | self.errors = [] |
| 120 | self.testsRun = 0 |
| 121 | self.skipped = [] |
| 122 | self.expectedFailures = [] |
| 123 | self.unexpectedSuccesses = [] |
| 124 | self.shouldStop = False |
| 125 | |
| 126 | def startTest(self, test): |
| 127 | pass |
| 128 | |
| 129 | def stopTest(self, test): |
| 130 | pass |
| 131 | |
| 132 | def addError(self, test): |
| 133 | pass |
| 134 | |
| 135 | def addFailure(self, test): |
| 136 | pass |
| 137 | |
| 138 | def addSuccess(self, test): |
| 139 | pass |
| 140 | |
| 141 | def wasSuccessful(self): |
| 142 | return True |