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