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