blob: 02e8f3a00b64a85a2f8090621705f0c214f68176 [file] [log] [blame]
Michael Foord2560e5c2010-03-27 12:34:21 +00001import unittest
2
3
4class 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
19class 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 Pitrouc9b3ef22013-03-20 20:16:47 +010044class _BaseLoggingResult(unittest.TestResult):
Michael Foord2560e5c2010-03-27 12:34:21 +000045 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 Pitrouc9b3ef22013-03-20 20:16:47 +010055 super().startTestRun()
Michael Foord2560e5c2010-03-27 12:34:21 +000056
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 Pitrouc9b3ef22013-03-20 20:16:47 +010063 super().stopTestRun()
Michael Foord2560e5c2010-03-27 12:34:21 +000064
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 Pitrouc9b3ef22013-03-20 20:16:47 +010071 super().addSuccess(*args)
Michael Foord2560e5c2010-03-27 12:34:21 +000072
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 Pitrouc9b3ef22013-03-20 20:16:47 +010079 super().addSkip(*args)
Michael Foord2560e5c2010-03-27 12:34:21 +000080
81 def addExpectedFailure(self, *args):
82 self._events.append('addExpectedFailure')
Antoine Pitrouc9b3ef22013-03-20 20:16:47 +010083 super().addExpectedFailure(*args)
Michael Foord2560e5c2010-03-27 12:34:21 +000084
85 def addUnexpectedSuccess(self, *args):
86 self._events.append('addUnexpectedSuccess')
Antoine Pitrouc9b3ef22013-03-20 20:16:47 +010087 super().addUnexpectedSuccess(*args)
88
89
90class 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
101class 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 Foord2560e5c2010-03-27 12:34:21 +0000112
113
114class 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