| 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 Exception as e: |
| 29 |
self.fail("Problem hashing %r and %r: %s" % (obj_1, obj_2, e)) |
| 30 |
|
| 31 |
for obj_1, obj_2 in self.ne_pairs: |
| 32 |
try: |
| 33 |
if hash(obj_1) == hash(obj_2): |
| 34 |
self.fail("%s and %s hash equal, but shouldn't" % |
| 35 |
(obj_1, obj_2)) |
| 36 |
except Exception as e: |
| 37 |
self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) |
| 38 |
|
| 39 |
|
| 40 |
class _BaseLoggingResult(unittest.TestResult): |
| 41 |
def __init__(self, log): |
| 42 |
self._events = log |
| 43 |
super().__init__() |
| 44 |
|
| 45 |
def startTest(self, test): |
| 46 |
self._events.append('startTest') |
| 47 |
super().startTest(test) |
| 48 |
|
| 49 |
def startTestRun(self): |
| 50 |
self._events.append('startTestRun') |
| 51 |
super().startTestRun() |
| 52 |
|
| 53 |
def stopTest(self, test): |
| 54 |
self._events.append('stopTest') |
| 55 |
super().stopTest(test) |
| 56 |
|
| 57 |
def stopTestRun(self): |
| 58 |
self._events.append('stopTestRun') |
| 59 |
super().stopTestRun() |
| 60 |
|
| 61 |
def addFailure(self, *args): |
| 62 |
self._events.append('addFailure') |
| 63 |
super().addFailure(*args) |
| 64 |
|
| 65 |
def addSuccess(self, *args): |
| 66 |
self._events.append('addSuccess') |
| 67 |
super().addSuccess(*args) |
| 68 |
|
| 69 |
def addError(self, *args): |
| 70 |
self._events.append('addError') |
| 71 |
super().addError(*args) |
| 72 |
|
| 73 |
def addSkip(self, *args): |
| 74 |
self._events.append('addSkip') |
| 75 |
super().addSkip(*args) |
| 76 |
|
| 77 |
def addExpectedFailure(self, *args): |
| 78 |
self._events.append('addExpectedFailure') |
| 79 |
super().addExpectedFailure(*args) |
| 80 |
|
| 81 |
def addUnexpectedSuccess(self, *args): |
| 82 |
self._events.append('addUnexpectedSuccess') |
| 83 |
super().addUnexpectedSuccess(*args) |
| 84 |
|
| 85 |
|
| 86 |
class LegacyLoggingResult(_BaseLoggingResult): |
| 87 |
""" |
| 88 |
A legacy TestResult implementation, without an addSubTest method, |
| 89 |
which records its method calls. |
| 90 |
""" |
| 91 |
|
| 92 |
@property |
| 93 |
def addSubTest(self): |
| 94 |
raise AttributeError |
| 95 |
|
| 96 |
|
| 97 |
class LoggingResult(_BaseLoggingResult): |
| 98 |
""" |
| 99 |
A TestResult implementation which records its method calls. |
| 100 |
""" |
| 101 |
|
| 102 |
def addSubTest(self, test, subtest, err): |
| 103 |
if err is None: |
| 104 |
self._events.append('addSubTestSuccess') |
| 105 |
else: |
| 106 |
self._events.append('addSubTestFailure') |
| 107 |
super().addSubTest(test, subtest, err) |
| 108 |
|
| 109 |
|
| 110 |
class ResultWithNoStartTestRunStopTestRun(object): |
| 111 |
"""An object honouring TestResult before startTestRun/stopTestRun.""" |
| 112 |
|
| 113 |
def __init__(self): |
| 114 |
self.failures = [] |
| 115 |
self.errors = [] |
| 116 |
self.testsRun = 0 |
| 117 |
self.skipped = [] |
| 118 |
self.expectedFailures = [] |
| 119 |
self.unexpectedSuccesses = [] |
| 120 |
self.shouldStop = False |
| 121 |
|
| 122 |
def startTest(self, test): |
| 123 |
pass |
| 124 |
|
| 125 |
def stopTest(self, test): |
| 126 |
pass |
| 127 |
|
| 128 |
def addError(self, test): |
| 129 |
pass |
| 130 |
|
| 131 |
def addFailure(self, test): |
| 132 |
pass |
| 133 |
|
| 134 |
def addSuccess(self, test): |
| 135 |
pass |
| 136 |
|
| 137 |
def wasSuccessful(self): |
| 138 |
return True |
| 139 |
|
| 140 |
|
| 141 |
class BufferedWriter: |
| 142 |
def __init__(self): |
| 143 |
self.result = '' |
| 144 |
self.buffer = '' |
| 145 |
|
| 146 |
def write(self, arg): |
| 147 |
self.buffer += arg |
| 148 |
|
| 149 |
def flush(self): |
| 150 |
self.result += self.buffer |
| 151 |
self.buffer = '' |
| 152 |
|
| 153 |
def getvalue(self): |
| 154 |
return self.result |