1 | n/a | """TestSuite""" |
---|
2 | n/a | |
---|
3 | n/a | import sys |
---|
4 | n/a | |
---|
5 | n/a | from . import case |
---|
6 | n/a | from . import util |
---|
7 | n/a | |
---|
8 | n/a | __unittest = True |
---|
9 | n/a | |
---|
10 | n/a | |
---|
11 | n/a | def _call_if_exists(parent, attr): |
---|
12 | n/a | func = getattr(parent, attr, lambda: None) |
---|
13 | n/a | func() |
---|
14 | n/a | |
---|
15 | n/a | |
---|
16 | n/a | class BaseTestSuite(object): |
---|
17 | n/a | """A simple test suite that doesn't provide class or module shared fixtures. |
---|
18 | n/a | """ |
---|
19 | n/a | _cleanup = True |
---|
20 | n/a | |
---|
21 | n/a | def __init__(self, tests=()): |
---|
22 | n/a | self._tests = [] |
---|
23 | n/a | self._removed_tests = 0 |
---|
24 | n/a | self.addTests(tests) |
---|
25 | n/a | |
---|
26 | n/a | def __repr__(self): |
---|
27 | n/a | return "<%s tests=%s>" % (util.strclass(self.__class__), list(self)) |
---|
28 | n/a | |
---|
29 | n/a | def __eq__(self, other): |
---|
30 | n/a | if not isinstance(other, self.__class__): |
---|
31 | n/a | return NotImplemented |
---|
32 | n/a | return list(self) == list(other) |
---|
33 | n/a | |
---|
34 | n/a | def __iter__(self): |
---|
35 | n/a | return iter(self._tests) |
---|
36 | n/a | |
---|
37 | n/a | def countTestCases(self): |
---|
38 | n/a | cases = self._removed_tests |
---|
39 | n/a | for test in self: |
---|
40 | n/a | if test: |
---|
41 | n/a | cases += test.countTestCases() |
---|
42 | n/a | return cases |
---|
43 | n/a | |
---|
44 | n/a | def addTest(self, test): |
---|
45 | n/a | # sanity checks |
---|
46 | n/a | if not callable(test): |
---|
47 | n/a | raise TypeError("{} is not callable".format(repr(test))) |
---|
48 | n/a | if isinstance(test, type) and issubclass(test, |
---|
49 | n/a | (case.TestCase, TestSuite)): |
---|
50 | n/a | raise TypeError("TestCases and TestSuites must be instantiated " |
---|
51 | n/a | "before passing them to addTest()") |
---|
52 | n/a | self._tests.append(test) |
---|
53 | n/a | |
---|
54 | n/a | def addTests(self, tests): |
---|
55 | n/a | if isinstance(tests, str): |
---|
56 | n/a | raise TypeError("tests must be an iterable of tests, not a string") |
---|
57 | n/a | for test in tests: |
---|
58 | n/a | self.addTest(test) |
---|
59 | n/a | |
---|
60 | n/a | def run(self, result): |
---|
61 | n/a | for index, test in enumerate(self): |
---|
62 | n/a | if result.shouldStop: |
---|
63 | n/a | break |
---|
64 | n/a | test(result) |
---|
65 | n/a | if self._cleanup: |
---|
66 | n/a | self._removeTestAtIndex(index) |
---|
67 | n/a | return result |
---|
68 | n/a | |
---|
69 | n/a | def _removeTestAtIndex(self, index): |
---|
70 | n/a | """Stop holding a reference to the TestCase at index.""" |
---|
71 | n/a | try: |
---|
72 | n/a | test = self._tests[index] |
---|
73 | n/a | except TypeError: |
---|
74 | n/a | # support for suite implementations that have overridden self._tests |
---|
75 | n/a | pass |
---|
76 | n/a | else: |
---|
77 | n/a | # Some unittest tests add non TestCase/TestSuite objects to |
---|
78 | n/a | # the suite. |
---|
79 | n/a | if hasattr(test, 'countTestCases'): |
---|
80 | n/a | self._removed_tests += test.countTestCases() |
---|
81 | n/a | self._tests[index] = None |
---|
82 | n/a | |
---|
83 | n/a | def __call__(self, *args, **kwds): |
---|
84 | n/a | return self.run(*args, **kwds) |
---|
85 | n/a | |
---|
86 | n/a | def debug(self): |
---|
87 | n/a | """Run the tests without collecting errors in a TestResult""" |
---|
88 | n/a | for test in self: |
---|
89 | n/a | test.debug() |
---|
90 | n/a | |
---|
91 | n/a | |
---|
92 | n/a | class TestSuite(BaseTestSuite): |
---|
93 | n/a | """A test suite is a composite test consisting of a number of TestCases. |
---|
94 | n/a | |
---|
95 | n/a | For use, create an instance of TestSuite, then add test case instances. |
---|
96 | n/a | When all tests have been added, the suite can be passed to a test |
---|
97 | n/a | runner, such as TextTestRunner. It will run the individual test cases |
---|
98 | n/a | in the order in which they were added, aggregating the results. When |
---|
99 | n/a | subclassing, do not forget to call the base class constructor. |
---|
100 | n/a | """ |
---|
101 | n/a | |
---|
102 | n/a | def run(self, result, debug=False): |
---|
103 | n/a | topLevel = False |
---|
104 | n/a | if getattr(result, '_testRunEntered', False) is False: |
---|
105 | n/a | result._testRunEntered = topLevel = True |
---|
106 | n/a | |
---|
107 | n/a | for index, test in enumerate(self): |
---|
108 | n/a | if result.shouldStop: |
---|
109 | n/a | break |
---|
110 | n/a | |
---|
111 | n/a | if _isnotsuite(test): |
---|
112 | n/a | self._tearDownPreviousClass(test, result) |
---|
113 | n/a | self._handleModuleFixture(test, result) |
---|
114 | n/a | self._handleClassSetUp(test, result) |
---|
115 | n/a | result._previousTestClass = test.__class__ |
---|
116 | n/a | |
---|
117 | n/a | if (getattr(test.__class__, '_classSetupFailed', False) or |
---|
118 | n/a | getattr(result, '_moduleSetUpFailed', False)): |
---|
119 | n/a | continue |
---|
120 | n/a | |
---|
121 | n/a | if not debug: |
---|
122 | n/a | test(result) |
---|
123 | n/a | else: |
---|
124 | n/a | test.debug() |
---|
125 | n/a | |
---|
126 | n/a | if self._cleanup: |
---|
127 | n/a | self._removeTestAtIndex(index) |
---|
128 | n/a | |
---|
129 | n/a | if topLevel: |
---|
130 | n/a | self._tearDownPreviousClass(None, result) |
---|
131 | n/a | self._handleModuleTearDown(result) |
---|
132 | n/a | result._testRunEntered = False |
---|
133 | n/a | return result |
---|
134 | n/a | |
---|
135 | n/a | def debug(self): |
---|
136 | n/a | """Run the tests without collecting errors in a TestResult""" |
---|
137 | n/a | debug = _DebugResult() |
---|
138 | n/a | self.run(debug, True) |
---|
139 | n/a | |
---|
140 | n/a | ################################ |
---|
141 | n/a | |
---|
142 | n/a | def _handleClassSetUp(self, test, result): |
---|
143 | n/a | previousClass = getattr(result, '_previousTestClass', None) |
---|
144 | n/a | currentClass = test.__class__ |
---|
145 | n/a | if currentClass == previousClass: |
---|
146 | n/a | return |
---|
147 | n/a | if result._moduleSetUpFailed: |
---|
148 | n/a | return |
---|
149 | n/a | if getattr(currentClass, "__unittest_skip__", False): |
---|
150 | n/a | return |
---|
151 | n/a | |
---|
152 | n/a | try: |
---|
153 | n/a | currentClass._classSetupFailed = False |
---|
154 | n/a | except TypeError: |
---|
155 | n/a | # test may actually be a function |
---|
156 | n/a | # so its class will be a builtin-type |
---|
157 | n/a | pass |
---|
158 | n/a | |
---|
159 | n/a | setUpClass = getattr(currentClass, 'setUpClass', None) |
---|
160 | n/a | if setUpClass is not None: |
---|
161 | n/a | _call_if_exists(result, '_setupStdout') |
---|
162 | n/a | try: |
---|
163 | n/a | setUpClass() |
---|
164 | n/a | except Exception as e: |
---|
165 | n/a | if isinstance(result, _DebugResult): |
---|
166 | n/a | raise |
---|
167 | n/a | currentClass._classSetupFailed = True |
---|
168 | n/a | className = util.strclass(currentClass) |
---|
169 | n/a | errorName = 'setUpClass (%s)' % className |
---|
170 | n/a | self._addClassOrModuleLevelException(result, e, errorName) |
---|
171 | n/a | finally: |
---|
172 | n/a | _call_if_exists(result, '_restoreStdout') |
---|
173 | n/a | |
---|
174 | n/a | def _get_previous_module(self, result): |
---|
175 | n/a | previousModule = None |
---|
176 | n/a | previousClass = getattr(result, '_previousTestClass', None) |
---|
177 | n/a | if previousClass is not None: |
---|
178 | n/a | previousModule = previousClass.__module__ |
---|
179 | n/a | return previousModule |
---|
180 | n/a | |
---|
181 | n/a | |
---|
182 | n/a | def _handleModuleFixture(self, test, result): |
---|
183 | n/a | previousModule = self._get_previous_module(result) |
---|
184 | n/a | currentModule = test.__class__.__module__ |
---|
185 | n/a | if currentModule == previousModule: |
---|
186 | n/a | return |
---|
187 | n/a | |
---|
188 | n/a | self._handleModuleTearDown(result) |
---|
189 | n/a | |
---|
190 | n/a | |
---|
191 | n/a | result._moduleSetUpFailed = False |
---|
192 | n/a | try: |
---|
193 | n/a | module = sys.modules[currentModule] |
---|
194 | n/a | except KeyError: |
---|
195 | n/a | return |
---|
196 | n/a | setUpModule = getattr(module, 'setUpModule', None) |
---|
197 | n/a | if setUpModule is not None: |
---|
198 | n/a | _call_if_exists(result, '_setupStdout') |
---|
199 | n/a | try: |
---|
200 | n/a | setUpModule() |
---|
201 | n/a | except Exception as e: |
---|
202 | n/a | if isinstance(result, _DebugResult): |
---|
203 | n/a | raise |
---|
204 | n/a | result._moduleSetUpFailed = True |
---|
205 | n/a | errorName = 'setUpModule (%s)' % currentModule |
---|
206 | n/a | self._addClassOrModuleLevelException(result, e, errorName) |
---|
207 | n/a | finally: |
---|
208 | n/a | _call_if_exists(result, '_restoreStdout') |
---|
209 | n/a | |
---|
210 | n/a | def _addClassOrModuleLevelException(self, result, exception, errorName): |
---|
211 | n/a | error = _ErrorHolder(errorName) |
---|
212 | n/a | addSkip = getattr(result, 'addSkip', None) |
---|
213 | n/a | if addSkip is not None and isinstance(exception, case.SkipTest): |
---|
214 | n/a | addSkip(error, str(exception)) |
---|
215 | n/a | else: |
---|
216 | n/a | result.addError(error, sys.exc_info()) |
---|
217 | n/a | |
---|
218 | n/a | def _handleModuleTearDown(self, result): |
---|
219 | n/a | previousModule = self._get_previous_module(result) |
---|
220 | n/a | if previousModule is None: |
---|
221 | n/a | return |
---|
222 | n/a | if result._moduleSetUpFailed: |
---|
223 | n/a | return |
---|
224 | n/a | |
---|
225 | n/a | try: |
---|
226 | n/a | module = sys.modules[previousModule] |
---|
227 | n/a | except KeyError: |
---|
228 | n/a | return |
---|
229 | n/a | |
---|
230 | n/a | tearDownModule = getattr(module, 'tearDownModule', None) |
---|
231 | n/a | if tearDownModule is not None: |
---|
232 | n/a | _call_if_exists(result, '_setupStdout') |
---|
233 | n/a | try: |
---|
234 | n/a | tearDownModule() |
---|
235 | n/a | except Exception as e: |
---|
236 | n/a | if isinstance(result, _DebugResult): |
---|
237 | n/a | raise |
---|
238 | n/a | errorName = 'tearDownModule (%s)' % previousModule |
---|
239 | n/a | self._addClassOrModuleLevelException(result, e, errorName) |
---|
240 | n/a | finally: |
---|
241 | n/a | _call_if_exists(result, '_restoreStdout') |
---|
242 | n/a | |
---|
243 | n/a | def _tearDownPreviousClass(self, test, result): |
---|
244 | n/a | previousClass = getattr(result, '_previousTestClass', None) |
---|
245 | n/a | currentClass = test.__class__ |
---|
246 | n/a | if currentClass == previousClass: |
---|
247 | n/a | return |
---|
248 | n/a | if getattr(previousClass, '_classSetupFailed', False): |
---|
249 | n/a | return |
---|
250 | n/a | if getattr(result, '_moduleSetUpFailed', False): |
---|
251 | n/a | return |
---|
252 | n/a | if getattr(previousClass, "__unittest_skip__", False): |
---|
253 | n/a | return |
---|
254 | n/a | |
---|
255 | n/a | tearDownClass = getattr(previousClass, 'tearDownClass', None) |
---|
256 | n/a | if tearDownClass is not None: |
---|
257 | n/a | _call_if_exists(result, '_setupStdout') |
---|
258 | n/a | try: |
---|
259 | n/a | tearDownClass() |
---|
260 | n/a | except Exception as e: |
---|
261 | n/a | if isinstance(result, _DebugResult): |
---|
262 | n/a | raise |
---|
263 | n/a | className = util.strclass(previousClass) |
---|
264 | n/a | errorName = 'tearDownClass (%s)' % className |
---|
265 | n/a | self._addClassOrModuleLevelException(result, e, errorName) |
---|
266 | n/a | finally: |
---|
267 | n/a | _call_if_exists(result, '_restoreStdout') |
---|
268 | n/a | |
---|
269 | n/a | |
---|
270 | n/a | class _ErrorHolder(object): |
---|
271 | n/a | """ |
---|
272 | n/a | Placeholder for a TestCase inside a result. As far as a TestResult |
---|
273 | n/a | is concerned, this looks exactly like a unit test. Used to insert |
---|
274 | n/a | arbitrary errors into a test suite run. |
---|
275 | n/a | """ |
---|
276 | n/a | # Inspired by the ErrorHolder from Twisted: |
---|
277 | n/a | # http://twistedmatrix.com/trac/browser/trunk/twisted/trial/runner.py |
---|
278 | n/a | |
---|
279 | n/a | # attribute used by TestResult._exc_info_to_string |
---|
280 | n/a | failureException = None |
---|
281 | n/a | |
---|
282 | n/a | def __init__(self, description): |
---|
283 | n/a | self.description = description |
---|
284 | n/a | |
---|
285 | n/a | def id(self): |
---|
286 | n/a | return self.description |
---|
287 | n/a | |
---|
288 | n/a | def shortDescription(self): |
---|
289 | n/a | return None |
---|
290 | n/a | |
---|
291 | n/a | def __repr__(self): |
---|
292 | n/a | return "<ErrorHolder description=%r>" % (self.description,) |
---|
293 | n/a | |
---|
294 | n/a | def __str__(self): |
---|
295 | n/a | return self.id() |
---|
296 | n/a | |
---|
297 | n/a | def run(self, result): |
---|
298 | n/a | # could call result.addError(...) - but this test-like object |
---|
299 | n/a | # shouldn't be run anyway |
---|
300 | n/a | pass |
---|
301 | n/a | |
---|
302 | n/a | def __call__(self, result): |
---|
303 | n/a | return self.run(result) |
---|
304 | n/a | |
---|
305 | n/a | def countTestCases(self): |
---|
306 | n/a | return 0 |
---|
307 | n/a | |
---|
308 | n/a | def _isnotsuite(test): |
---|
309 | n/a | "A crude way to tell apart testcases and suites with duck-typing" |
---|
310 | n/a | try: |
---|
311 | n/a | iter(test) |
---|
312 | n/a | except TypeError: |
---|
313 | n/a | return True |
---|
314 | n/a | return False |
---|
315 | n/a | |
---|
316 | n/a | |
---|
317 | n/a | class _DebugResult(object): |
---|
318 | n/a | "Used by the TestSuite to hold previous class when running in debug." |
---|
319 | n/a | _previousTestClass = None |
---|
320 | n/a | _moduleSetUpFailed = False |
---|
321 | n/a | shouldStop = False |
---|