Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 1 | """Test script for unittest. |
| 2 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 3 | By Collin Winter <collinw at gmail.com> |
| 4 | |
| 5 | Still need testing: |
| 6 | TestCase.{assert,fail}* methods (some are tested implicitly) |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 9 | from test import support |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 10 | import unittest |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 11 | from unittest import TestCase |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 12 | import types |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 14 | ### Support code |
| 15 | ################################################################ |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 17 | class LoggingResult(unittest.TestResult): |
| 18 | def __init__(self, log): |
| 19 | self._events = log |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 20 | super().__init__() |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 21 | |
| 22 | def startTest(self, test): |
| 23 | self._events.append('startTest') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 24 | super().startTest(test) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 25 | |
| 26 | def stopTest(self, test): |
| 27 | self._events.append('stopTest') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 28 | super().stopTest(test) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 29 | |
| 30 | def addFailure(self, *args): |
| 31 | self._events.append('addFailure') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | super().addFailure(*args) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 33 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 34 | def addSuccess(self, *args): |
| 35 | self._events.append('addSuccess') |
| 36 | super(LoggingResult, self).addSuccess(*args) |
| 37 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 38 | def addError(self, *args): |
| 39 | self._events.append('addError') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 40 | super().addError(*args) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 41 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 42 | def addSkip(self, *args): |
| 43 | self._events.append('addSkip') |
| 44 | super(LoggingResult, self).addSkip(*args) |
| 45 | |
| 46 | def addExpectedFailure(self, *args): |
| 47 | self._events.append('addExpectedFailure') |
| 48 | super(LoggingResult, self).addExpectedFailure(*args) |
| 49 | |
| 50 | def addUnexpectedSuccess(self, *args): |
| 51 | self._events.append('addUnexpectedSuccess') |
| 52 | super(LoggingResult, self).addUnexpectedSuccess(*args) |
| 53 | |
| 54 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 55 | class TestEquality(object): |
| 56 | # Check for a valid __eq__ implementation |
| 57 | def test_eq(self): |
| 58 | for obj_1, obj_2 in self.eq_pairs: |
| 59 | self.assertEqual(obj_1, obj_2) |
| 60 | self.assertEqual(obj_2, obj_1) |
| 61 | |
| 62 | # Check for a valid __ne__ implementation |
| 63 | def test_ne(self): |
| 64 | for obj_1, obj_2 in self.ne_pairs: |
| 65 | self.failIfEqual(obj_1, obj_2) |
| 66 | self.failIfEqual(obj_2, obj_1) |
| 67 | |
| 68 | class TestHashing(object): |
| 69 | # Check for a valid __hash__ implementation |
| 70 | def test_hash(self): |
| 71 | for obj_1, obj_2 in self.eq_pairs: |
| 72 | try: |
| 73 | assert hash(obj_1) == hash(obj_2) |
| 74 | except KeyboardInterrupt: |
| 75 | raise |
| 76 | except AssertionError: |
| 77 | self.fail("%s and %s do not hash equal" % (obj_1, obj_2)) |
| 78 | except Exception as e: |
| 79 | self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) |
| 80 | |
| 81 | for obj_1, obj_2 in self.ne_pairs: |
| 82 | try: |
| 83 | assert hash(obj_1) != hash(obj_2) |
| 84 | except KeyboardInterrupt: |
| 85 | raise |
| 86 | except AssertionError: |
| 87 | self.fail("%s and %s hash equal, but shouldn't" % (obj_1, obj_2)) |
| 88 | except Exception as e: |
| 89 | self.fail("Problem hashing %s and %s: %s" % (obj_1, obj_2, e)) |
| 90 | |
| 91 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 92 | # List subclass we can add attributes to. |
| 93 | class MyClassSuite(list): |
| 94 | |
| 95 | def __init__(self, tests, klass): |
| 96 | super(MyClassSuite, self).__init__(tests) |
| 97 | |
| 98 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 99 | ################################################################ |
| 100 | ### /Support code |
| 101 | |
| 102 | class Test_TestLoader(TestCase): |
| 103 | |
| 104 | ### Tests for TestLoader.loadTestsFromTestCase |
| 105 | ################################################################ |
| 106 | |
| 107 | # "Return a suite of all tests cases contained in the TestCase-derived |
| 108 | # class testCaseClass" |
| 109 | def test_loadTestsFromTestCase(self): |
| 110 | class Foo(unittest.TestCase): |
| 111 | def test_1(self): pass |
| 112 | def test_2(self): pass |
| 113 | def foo_bar(self): pass |
| 114 | |
| 115 | tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 116 | |
| 117 | loader = unittest.TestLoader() |
| 118 | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 119 | |
| 120 | # "Return a suite of all tests cases contained in the TestCase-derived |
| 121 | # class testCaseClass" |
| 122 | # |
| 123 | # Make sure it does the right thing even if no tests were found |
| 124 | def test_loadTestsFromTestCase__no_matches(self): |
| 125 | class Foo(unittest.TestCase): |
| 126 | def foo_bar(self): pass |
| 127 | |
| 128 | empty_suite = unittest.TestSuite() |
| 129 | |
| 130 | loader = unittest.TestLoader() |
| 131 | self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite) |
| 132 | |
| 133 | # "Return a suite of all tests cases contained in the TestCase-derived |
| 134 | # class testCaseClass" |
| 135 | # |
| 136 | # What happens if loadTestsFromTestCase() is given an object |
| 137 | # that isn't a subclass of TestCase? Specifically, what happens |
| 138 | # if testCaseClass is a subclass of TestSuite? |
| 139 | # |
| 140 | # This is checked for specifically in the code, so we better add a |
| 141 | # test for it. |
| 142 | def test_loadTestsFromTestCase__TestSuite_subclass(self): |
| 143 | class NotATestCase(unittest.TestSuite): |
| 144 | pass |
| 145 | |
| 146 | loader = unittest.TestLoader() |
| 147 | try: |
| 148 | loader.loadTestsFromTestCase(NotATestCase) |
| 149 | except TypeError: |
| 150 | pass |
| 151 | else: |
| 152 | self.fail('Should raise TypeError') |
| 153 | |
| 154 | # "Return a suite of all tests cases contained in the TestCase-derived |
| 155 | # class testCaseClass" |
| 156 | # |
| 157 | # Make sure loadTestsFromTestCase() picks up the default test method |
| 158 | # name (as specified by TestCase), even though the method name does |
| 159 | # not match the default TestLoader.testMethodPrefix string |
| 160 | def test_loadTestsFromTestCase__default_method_name(self): |
| 161 | class Foo(unittest.TestCase): |
| 162 | def runTest(self): |
| 163 | pass |
| 164 | |
| 165 | loader = unittest.TestLoader() |
| 166 | # This has to be false for the test to succeed |
| 167 | self.failIf('runTest'.startswith(loader.testMethodPrefix)) |
| 168 | |
| 169 | suite = loader.loadTestsFromTestCase(Foo) |
| 170 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 171 | self.assertEqual(list(suite), [Foo('runTest')]) |
| 172 | |
| 173 | ################################################################ |
| 174 | ### /Tests for TestLoader.loadTestsFromTestCase |
| 175 | |
| 176 | ### Tests for TestLoader.loadTestsFromModule |
| 177 | ################################################################ |
| 178 | |
| 179 | # "This method searches `module` for classes derived from TestCase" |
| 180 | def test_loadTestsFromModule__TestCase_subclass(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 181 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 182 | class MyTestCase(unittest.TestCase): |
| 183 | def test(self): |
| 184 | pass |
| 185 | m.testcase_1 = MyTestCase |
| 186 | |
| 187 | loader = unittest.TestLoader() |
| 188 | suite = loader.loadTestsFromModule(m) |
| 189 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 190 | |
| 191 | expected = [loader.suiteClass([MyTestCase('test')])] |
| 192 | self.assertEqual(list(suite), expected) |
| 193 | |
| 194 | # "This method searches `module` for classes derived from TestCase" |
| 195 | # |
| 196 | # What happens if no tests are found (no TestCase instances)? |
| 197 | def test_loadTestsFromModule__no_TestCase_instances(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 198 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 199 | |
| 200 | loader = unittest.TestLoader() |
| 201 | suite = loader.loadTestsFromModule(m) |
| 202 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 203 | self.assertEqual(list(suite), []) |
| 204 | |
| 205 | # "This method searches `module` for classes derived from TestCase" |
| 206 | # |
| 207 | # What happens if no tests are found (TestCases instances, but no tests)? |
| 208 | def test_loadTestsFromModule__no_TestCase_tests(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 209 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 210 | class MyTestCase(unittest.TestCase): |
| 211 | pass |
| 212 | m.testcase_1 = MyTestCase |
| 213 | |
| 214 | loader = unittest.TestLoader() |
| 215 | suite = loader.loadTestsFromModule(m) |
| 216 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 217 | |
| 218 | self.assertEqual(list(suite), [loader.suiteClass()]) |
| 219 | |
| 220 | # "This method searches `module` for classes derived from TestCase"s |
| 221 | # |
| 222 | # What happens if loadTestsFromModule() is given something other |
| 223 | # than a module? |
| 224 | # |
| 225 | # XXX Currently, it succeeds anyway. This flexibility |
| 226 | # should either be documented or loadTestsFromModule() should |
| 227 | # raise a TypeError |
| 228 | # |
| 229 | # XXX Certain people are using this behaviour. We'll add a test for it |
| 230 | def test_loadTestsFromModule__not_a_module(self): |
| 231 | class MyTestCase(unittest.TestCase): |
| 232 | def test(self): |
| 233 | pass |
| 234 | |
| 235 | class NotAModule(object): |
| 236 | test_2 = MyTestCase |
| 237 | |
| 238 | loader = unittest.TestLoader() |
| 239 | suite = loader.loadTestsFromModule(NotAModule) |
| 240 | |
| 241 | reference = [unittest.TestSuite([MyTestCase('test')])] |
| 242 | self.assertEqual(list(suite), reference) |
| 243 | |
| 244 | ################################################################ |
| 245 | ### /Tests for TestLoader.loadTestsFromModule() |
| 246 | |
| 247 | ### Tests for TestLoader.loadTestsFromName() |
| 248 | ################################################################ |
| 249 | |
| 250 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 251 | # a module, a test case class, a TestSuite instance, a test method |
| 252 | # within a test case class, or a callable object which returns a |
| 253 | # TestCase or TestSuite instance." |
| 254 | # |
| 255 | # Is ValueError raised in response to an empty name? |
| 256 | def test_loadTestsFromName__empty_name(self): |
| 257 | loader = unittest.TestLoader() |
| 258 | |
| 259 | try: |
| 260 | loader.loadTestsFromName('') |
| 261 | except ValueError as e: |
| 262 | self.assertEqual(str(e), "Empty module name") |
| 263 | else: |
| 264 | self.fail("TestLoader.loadTestsFromName failed to raise ValueError") |
| 265 | |
| 266 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 267 | # a module, a test case class, a TestSuite instance, a test method |
| 268 | # within a test case class, or a callable object which returns a |
| 269 | # TestCase or TestSuite instance." |
| 270 | # |
| 271 | # What happens when the name contains invalid characters? |
| 272 | def test_loadTestsFromName__malformed_name(self): |
| 273 | loader = unittest.TestLoader() |
| 274 | |
| 275 | # XXX Should this raise ValueError or ImportError? |
| 276 | try: |
| 277 | loader.loadTestsFromName('abc () //') |
| 278 | except ValueError: |
| 279 | pass |
| 280 | except ImportError: |
| 281 | pass |
| 282 | else: |
| 283 | self.fail("TestLoader.loadTestsFromName failed to raise ValueError") |
| 284 | |
| 285 | # "The specifier name is a ``dotted name'' that may resolve ... to a |
| 286 | # module" |
| 287 | # |
| 288 | # What happens when a module by that name can't be found? |
| 289 | def test_loadTestsFromName__unknown_module_name(self): |
| 290 | loader = unittest.TestLoader() |
| 291 | |
| 292 | try: |
| 293 | loader.loadTestsFromName('sdasfasfasdf') |
| 294 | except ImportError as e: |
| 295 | self.assertEqual(str(e), "No module named sdasfasfasdf") |
| 296 | else: |
| 297 | self.fail("TestLoader.loadTestsFromName failed to raise ImportError") |
| 298 | |
| 299 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 300 | # a module, a test case class, a TestSuite instance, a test method |
| 301 | # within a test case class, or a callable object which returns a |
| 302 | # TestCase or TestSuite instance." |
| 303 | # |
| 304 | # What happens when the module is found, but the attribute can't? |
| 305 | def test_loadTestsFromName__unknown_attr_name(self): |
| 306 | loader = unittest.TestLoader() |
| 307 | |
| 308 | try: |
| 309 | loader.loadTestsFromName('unittest.sdasfasfasdf') |
| 310 | except AttributeError as e: |
| 311 | self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") |
| 312 | else: |
| 313 | self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") |
| 314 | |
| 315 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 316 | # a module, a test case class, a TestSuite instance, a test method |
| 317 | # within a test case class, or a callable object which returns a |
| 318 | # TestCase or TestSuite instance." |
| 319 | # |
| 320 | # What happens when we provide the module, but the attribute can't be |
| 321 | # found? |
| 322 | def test_loadTestsFromName__relative_unknown_name(self): |
| 323 | loader = unittest.TestLoader() |
| 324 | |
| 325 | try: |
| 326 | loader.loadTestsFromName('sdasfasfasdf', unittest) |
| 327 | except AttributeError as e: |
| 328 | self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") |
| 329 | else: |
| 330 | self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") |
| 331 | |
| 332 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 333 | # a module, a test case class, a TestSuite instance, a test method |
| 334 | # within a test case class, or a callable object which returns a |
| 335 | # TestCase or TestSuite instance." |
| 336 | # ... |
| 337 | # "The method optionally resolves name relative to the given module" |
| 338 | # |
| 339 | # Does loadTestsFromName raise ValueError when passed an empty |
| 340 | # name relative to a provided module? |
| 341 | # |
| 342 | # XXX Should probably raise a ValueError instead of an AttributeError |
| 343 | def test_loadTestsFromName__relative_empty_name(self): |
| 344 | loader = unittest.TestLoader() |
| 345 | |
| 346 | try: |
| 347 | loader.loadTestsFromName('', unittest) |
| 348 | except AttributeError as e: |
| 349 | pass |
| 350 | else: |
| 351 | self.fail("Failed to raise AttributeError") |
| 352 | |
| 353 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 354 | # a module, a test case class, a TestSuite instance, a test method |
| 355 | # within a test case class, or a callable object which returns a |
| 356 | # TestCase or TestSuite instance." |
| 357 | # ... |
| 358 | # "The method optionally resolves name relative to the given module" |
| 359 | # |
| 360 | # What happens when an impossible name is given, relative to the provided |
| 361 | # `module`? |
| 362 | def test_loadTestsFromName__relative_malformed_name(self): |
| 363 | loader = unittest.TestLoader() |
| 364 | |
| 365 | # XXX Should this raise AttributeError or ValueError? |
| 366 | try: |
| 367 | loader.loadTestsFromName('abc () //', unittest) |
| 368 | except ValueError: |
| 369 | pass |
| 370 | except AttributeError: |
| 371 | pass |
| 372 | else: |
| 373 | self.fail("TestLoader.loadTestsFromName failed to raise ValueError") |
| 374 | |
| 375 | # "The method optionally resolves name relative to the given module" |
| 376 | # |
| 377 | # Does loadTestsFromName raise TypeError when the `module` argument |
| 378 | # isn't a module object? |
| 379 | # |
| 380 | # XXX Accepts the not-a-module object, ignorning the object's type |
| 381 | # This should raise an exception or the method name should be changed |
| 382 | # |
| 383 | # XXX Some people are relying on this, so keep it for now |
| 384 | def test_loadTestsFromName__relative_not_a_module(self): |
| 385 | class MyTestCase(unittest.TestCase): |
| 386 | def test(self): |
| 387 | pass |
| 388 | |
| 389 | class NotAModule(object): |
| 390 | test_2 = MyTestCase |
| 391 | |
| 392 | loader = unittest.TestLoader() |
| 393 | suite = loader.loadTestsFromName('test_2', NotAModule) |
| 394 | |
| 395 | reference = [MyTestCase('test')] |
| 396 | self.assertEqual(list(suite), reference) |
| 397 | |
| 398 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 399 | # a module, a test case class, a TestSuite instance, a test method |
| 400 | # within a test case class, or a callable object which returns a |
| 401 | # TestCase or TestSuite instance." |
| 402 | # |
| 403 | # Does it raise an exception if the name resolves to an invalid |
| 404 | # object? |
| 405 | def test_loadTestsFromName__relative_bad_object(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 406 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 407 | m.testcase_1 = object() |
| 408 | |
| 409 | loader = unittest.TestLoader() |
| 410 | try: |
| 411 | loader.loadTestsFromName('testcase_1', m) |
| 412 | except TypeError: |
| 413 | pass |
| 414 | else: |
| 415 | self.fail("Should have raised TypeError") |
| 416 | |
| 417 | # "The specifier name is a ``dotted name'' that may |
| 418 | # resolve either to ... a test case class" |
| 419 | def test_loadTestsFromName__relative_TestCase_subclass(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 420 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 421 | class MyTestCase(unittest.TestCase): |
| 422 | def test(self): |
| 423 | pass |
| 424 | m.testcase_1 = MyTestCase |
| 425 | |
| 426 | loader = unittest.TestLoader() |
| 427 | suite = loader.loadTestsFromName('testcase_1', m) |
| 428 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 429 | self.assertEqual(list(suite), [MyTestCase('test')]) |
| 430 | |
| 431 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 432 | # a module, a test case class, a TestSuite instance, a test method |
| 433 | # within a test case class, or a callable object which returns a |
| 434 | # TestCase or TestSuite instance." |
| 435 | def test_loadTestsFromName__relative_TestSuite(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 436 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 437 | class MyTestCase(unittest.TestCase): |
| 438 | def test(self): |
| 439 | pass |
| 440 | m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
| 441 | |
| 442 | loader = unittest.TestLoader() |
| 443 | suite = loader.loadTestsFromName('testsuite', m) |
| 444 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 445 | |
| 446 | self.assertEqual(list(suite), [MyTestCase('test')]) |
| 447 | |
| 448 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 449 | # ... a test method within a test case class" |
| 450 | def test_loadTestsFromName__relative_testmethod(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 451 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 452 | class MyTestCase(unittest.TestCase): |
| 453 | def test(self): |
| 454 | pass |
| 455 | m.testcase_1 = MyTestCase |
| 456 | |
| 457 | loader = unittest.TestLoader() |
| 458 | suite = loader.loadTestsFromName('testcase_1.test', m) |
| 459 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 460 | |
| 461 | self.assertEqual(list(suite), [MyTestCase('test')]) |
| 462 | |
| 463 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 464 | # a module, a test case class, a TestSuite instance, a test method |
| 465 | # within a test case class, or a callable object which returns a |
| 466 | # TestCase or TestSuite instance." |
| 467 | # |
| 468 | # Does loadTestsFromName() raise the proper exception when trying to |
| 469 | # resolve "a test method within a test case class" that doesn't exist |
| 470 | # for the given name (relative to a provided module)? |
| 471 | def test_loadTestsFromName__relative_invalid_testmethod(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 472 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 473 | class MyTestCase(unittest.TestCase): |
| 474 | def test(self): |
| 475 | pass |
| 476 | m.testcase_1 = MyTestCase |
| 477 | |
| 478 | loader = unittest.TestLoader() |
| 479 | try: |
| 480 | loader.loadTestsFromName('testcase_1.testfoo', m) |
| 481 | except AttributeError as e: |
| 482 | self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'") |
| 483 | else: |
| 484 | self.fail("Failed to raise AttributeError") |
| 485 | |
| 486 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 487 | # ... a callable object which returns a ... TestSuite instance" |
| 488 | def test_loadTestsFromName__callable__TestSuite(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 489 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 490 | testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 491 | testcase_2 = unittest.FunctionTestCase(lambda: None) |
| 492 | def return_TestSuite(): |
| 493 | return unittest.TestSuite([testcase_1, testcase_2]) |
| 494 | m.return_TestSuite = return_TestSuite |
| 495 | |
| 496 | loader = unittest.TestLoader() |
| 497 | suite = loader.loadTestsFromName('return_TestSuite', m) |
| 498 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 499 | self.assertEqual(list(suite), [testcase_1, testcase_2]) |
| 500 | |
| 501 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 502 | # ... a callable object which returns a TestCase ... instance" |
| 503 | def test_loadTestsFromName__callable__TestCase_instance(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 504 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 505 | testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 506 | def return_TestCase(): |
| 507 | return testcase_1 |
| 508 | m.return_TestCase = return_TestCase |
| 509 | |
| 510 | loader = unittest.TestLoader() |
| 511 | suite = loader.loadTestsFromName('return_TestCase', m) |
| 512 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 513 | self.assertEqual(list(suite), [testcase_1]) |
| 514 | |
| 515 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 516 | # ... a callable object which returns a TestCase or TestSuite instance" |
| 517 | # |
| 518 | # What happens if the callable returns something else? |
| 519 | def test_loadTestsFromName__callable__wrong_type(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 520 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 521 | def return_wrong(): |
| 522 | return 6 |
| 523 | m.return_wrong = return_wrong |
| 524 | |
| 525 | loader = unittest.TestLoader() |
| 526 | try: |
| 527 | suite = loader.loadTestsFromName('return_wrong', m) |
| 528 | except TypeError: |
| 529 | pass |
| 530 | else: |
| 531 | self.fail("TestLoader.loadTestsFromName failed to raise TypeError") |
| 532 | |
| 533 | # "The specifier can refer to modules and packages which have not been |
| 534 | # imported; they will be imported as a side-effect" |
| 535 | def test_loadTestsFromName__module_not_loaded(self): |
| 536 | # We're going to try to load this module as a side-effect, so it |
| 537 | # better not be loaded before we try. |
| 538 | # |
| 539 | # Why pick audioop? Google shows it isn't used very often, so there's |
| 540 | # a good chance that it won't be imported when this test is run |
| 541 | module_name = 'audioop' |
| 542 | |
| 543 | import sys |
| 544 | if module_name in sys.modules: |
| 545 | del sys.modules[module_name] |
| 546 | |
| 547 | loader = unittest.TestLoader() |
| 548 | try: |
| 549 | suite = loader.loadTestsFromName(module_name) |
| 550 | |
| 551 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 552 | self.assertEqual(list(suite), []) |
| 553 | |
| 554 | # audioop should now be loaded, thanks to loadTestsFromName() |
| 555 | self.failUnless(module_name in sys.modules) |
| 556 | finally: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 557 | if module_name in sys.modules: |
| 558 | del sys.modules[module_name] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 559 | |
| 560 | ################################################################ |
| 561 | ### Tests for TestLoader.loadTestsFromName() |
| 562 | |
| 563 | ### Tests for TestLoader.loadTestsFromNames() |
| 564 | ################################################################ |
| 565 | |
| 566 | # "Similar to loadTestsFromName(), but takes a sequence of names rather |
| 567 | # than a single name." |
| 568 | # |
| 569 | # What happens if that sequence of names is empty? |
| 570 | def test_loadTestsFromNames__empty_name_list(self): |
| 571 | loader = unittest.TestLoader() |
| 572 | |
| 573 | suite = loader.loadTestsFromNames([]) |
| 574 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 575 | self.assertEqual(list(suite), []) |
| 576 | |
| 577 | # "Similar to loadTestsFromName(), but takes a sequence of names rather |
| 578 | # than a single name." |
| 579 | # ... |
| 580 | # "The method optionally resolves name relative to the given module" |
| 581 | # |
| 582 | # What happens if that sequence of names is empty? |
| 583 | # |
| 584 | # XXX Should this raise a ValueError or just return an empty TestSuite? |
| 585 | def test_loadTestsFromNames__relative_empty_name_list(self): |
| 586 | loader = unittest.TestLoader() |
| 587 | |
| 588 | suite = loader.loadTestsFromNames([], unittest) |
| 589 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 590 | self.assertEqual(list(suite), []) |
| 591 | |
| 592 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 593 | # a module, a test case class, a TestSuite instance, a test method |
| 594 | # within a test case class, or a callable object which returns a |
| 595 | # TestCase or TestSuite instance." |
| 596 | # |
| 597 | # Is ValueError raised in response to an empty name? |
| 598 | def test_loadTestsFromNames__empty_name(self): |
| 599 | loader = unittest.TestLoader() |
| 600 | |
| 601 | try: |
| 602 | loader.loadTestsFromNames(['']) |
| 603 | except ValueError as e: |
| 604 | self.assertEqual(str(e), "Empty module name") |
| 605 | else: |
| 606 | self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") |
| 607 | |
| 608 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 609 | # a module, a test case class, a TestSuite instance, a test method |
| 610 | # within a test case class, or a callable object which returns a |
| 611 | # TestCase or TestSuite instance." |
| 612 | # |
| 613 | # What happens when presented with an impossible module name? |
| 614 | def test_loadTestsFromNames__malformed_name(self): |
| 615 | loader = unittest.TestLoader() |
| 616 | |
| 617 | # XXX Should this raise ValueError or ImportError? |
| 618 | try: |
| 619 | loader.loadTestsFromNames(['abc () //']) |
| 620 | except ValueError: |
| 621 | pass |
| 622 | except ImportError: |
| 623 | pass |
| 624 | else: |
| 625 | self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") |
| 626 | |
| 627 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 628 | # a module, a test case class, a TestSuite instance, a test method |
| 629 | # within a test case class, or a callable object which returns a |
| 630 | # TestCase or TestSuite instance." |
| 631 | # |
| 632 | # What happens when no module can be found for the given name? |
| 633 | def test_loadTestsFromNames__unknown_module_name(self): |
| 634 | loader = unittest.TestLoader() |
| 635 | |
| 636 | try: |
| 637 | loader.loadTestsFromNames(['sdasfasfasdf']) |
| 638 | except ImportError as e: |
| 639 | self.assertEqual(str(e), "No module named sdasfasfasdf") |
| 640 | else: |
| 641 | self.fail("TestLoader.loadTestsFromNames failed to raise ImportError") |
| 642 | |
| 643 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 644 | # a module, a test case class, a TestSuite instance, a test method |
| 645 | # within a test case class, or a callable object which returns a |
| 646 | # TestCase or TestSuite instance." |
| 647 | # |
| 648 | # What happens when the module can be found, but not the attribute? |
| 649 | def test_loadTestsFromNames__unknown_attr_name(self): |
| 650 | loader = unittest.TestLoader() |
| 651 | |
| 652 | try: |
| 653 | loader.loadTestsFromNames(['unittest.sdasfasfasdf', 'unittest']) |
| 654 | except AttributeError as e: |
| 655 | self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") |
| 656 | else: |
| 657 | self.fail("TestLoader.loadTestsFromNames failed to raise AttributeError") |
| 658 | |
| 659 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 660 | # a module, a test case class, a TestSuite instance, a test method |
| 661 | # within a test case class, or a callable object which returns a |
| 662 | # TestCase or TestSuite instance." |
| 663 | # ... |
| 664 | # "The method optionally resolves name relative to the given module" |
| 665 | # |
| 666 | # What happens when given an unknown attribute on a specified `module` |
| 667 | # argument? |
| 668 | def test_loadTestsFromNames__unknown_name_relative_1(self): |
| 669 | loader = unittest.TestLoader() |
| 670 | |
| 671 | try: |
| 672 | loader.loadTestsFromNames(['sdasfasfasdf'], unittest) |
| 673 | except AttributeError as e: |
| 674 | self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") |
| 675 | else: |
| 676 | self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") |
| 677 | |
| 678 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 679 | # a module, a test case class, a TestSuite instance, a test method |
| 680 | # within a test case class, or a callable object which returns a |
| 681 | # TestCase or TestSuite instance." |
| 682 | # ... |
| 683 | # "The method optionally resolves name relative to the given module" |
| 684 | # |
| 685 | # Do unknown attributes (relative to a provided module) still raise an |
| 686 | # exception even in the presence of valid attribute names? |
| 687 | def test_loadTestsFromNames__unknown_name_relative_2(self): |
| 688 | loader = unittest.TestLoader() |
| 689 | |
| 690 | try: |
| 691 | loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest) |
| 692 | except AttributeError as e: |
| 693 | self.assertEqual(str(e), "'module' object has no attribute 'sdasfasfasdf'") |
| 694 | else: |
| 695 | self.fail("TestLoader.loadTestsFromName failed to raise AttributeError") |
| 696 | |
| 697 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 698 | # a module, a test case class, a TestSuite instance, a test method |
| 699 | # within a test case class, or a callable object which returns a |
| 700 | # TestCase or TestSuite instance." |
| 701 | # ... |
| 702 | # "The method optionally resolves name relative to the given module" |
| 703 | # |
| 704 | # What happens when faced with the empty string? |
| 705 | # |
| 706 | # XXX This currently raises AttributeError, though ValueError is probably |
| 707 | # more appropriate |
| 708 | def test_loadTestsFromNames__relative_empty_name(self): |
| 709 | loader = unittest.TestLoader() |
| 710 | |
| 711 | try: |
| 712 | loader.loadTestsFromNames([''], unittest) |
| 713 | except AttributeError: |
| 714 | pass |
| 715 | else: |
| 716 | self.fail("Failed to raise ValueError") |
| 717 | |
| 718 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 719 | # a module, a test case class, a TestSuite instance, a test method |
| 720 | # within a test case class, or a callable object which returns a |
| 721 | # TestCase or TestSuite instance." |
| 722 | # ... |
| 723 | # "The method optionally resolves name relative to the given module" |
| 724 | # |
| 725 | # What happens when presented with an impossible attribute name? |
| 726 | def test_loadTestsFromNames__relative_malformed_name(self): |
| 727 | loader = unittest.TestLoader() |
| 728 | |
| 729 | # XXX Should this raise AttributeError or ValueError? |
| 730 | try: |
| 731 | loader.loadTestsFromNames(['abc () //'], unittest) |
| 732 | except AttributeError: |
| 733 | pass |
| 734 | except ValueError: |
| 735 | pass |
| 736 | else: |
| 737 | self.fail("TestLoader.loadTestsFromNames failed to raise ValueError") |
| 738 | |
| 739 | # "The method optionally resolves name relative to the given module" |
| 740 | # |
| 741 | # Does loadTestsFromNames() make sure the provided `module` is in fact |
| 742 | # a module? |
| 743 | # |
| 744 | # XXX This validation is currently not done. This flexibility should |
| 745 | # either be documented or a TypeError should be raised. |
| 746 | def test_loadTestsFromNames__relative_not_a_module(self): |
| 747 | class MyTestCase(unittest.TestCase): |
| 748 | def test(self): |
| 749 | pass |
| 750 | |
| 751 | class NotAModule(object): |
| 752 | test_2 = MyTestCase |
| 753 | |
| 754 | loader = unittest.TestLoader() |
| 755 | suite = loader.loadTestsFromNames(['test_2'], NotAModule) |
| 756 | |
| 757 | reference = [unittest.TestSuite([MyTestCase('test')])] |
| 758 | self.assertEqual(list(suite), reference) |
| 759 | |
| 760 | # "The specifier name is a ``dotted name'' that may resolve either to |
| 761 | # a module, a test case class, a TestSuite instance, a test method |
| 762 | # within a test case class, or a callable object which returns a |
| 763 | # TestCase or TestSuite instance." |
| 764 | # |
| 765 | # Does it raise an exception if the name resolves to an invalid |
| 766 | # object? |
| 767 | def test_loadTestsFromNames__relative_bad_object(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 768 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 769 | m.testcase_1 = object() |
| 770 | |
| 771 | loader = unittest.TestLoader() |
| 772 | try: |
| 773 | loader.loadTestsFromNames(['testcase_1'], m) |
| 774 | except TypeError: |
| 775 | pass |
| 776 | else: |
| 777 | self.fail("Should have raised TypeError") |
| 778 | |
| 779 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 780 | # ... a test case class" |
| 781 | def test_loadTestsFromNames__relative_TestCase_subclass(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 782 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 783 | class MyTestCase(unittest.TestCase): |
| 784 | def test(self): |
| 785 | pass |
| 786 | m.testcase_1 = MyTestCase |
| 787 | |
| 788 | loader = unittest.TestLoader() |
| 789 | suite = loader.loadTestsFromNames(['testcase_1'], m) |
| 790 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 791 | |
| 792 | expected = loader.suiteClass([MyTestCase('test')]) |
| 793 | self.assertEqual(list(suite), [expected]) |
| 794 | |
| 795 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 796 | # ... a TestSuite instance" |
| 797 | def test_loadTestsFromNames__relative_TestSuite(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 798 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 799 | class MyTestCase(unittest.TestCase): |
| 800 | def test(self): |
| 801 | pass |
| 802 | m.testsuite = unittest.TestSuite([MyTestCase('test')]) |
| 803 | |
| 804 | loader = unittest.TestLoader() |
| 805 | suite = loader.loadTestsFromNames(['testsuite'], m) |
| 806 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 807 | |
| 808 | self.assertEqual(list(suite), [m.testsuite]) |
| 809 | |
| 810 | # "The specifier name is a ``dotted name'' that may resolve ... to ... a |
| 811 | # test method within a test case class" |
| 812 | def test_loadTestsFromNames__relative_testmethod(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 813 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 814 | class MyTestCase(unittest.TestCase): |
| 815 | def test(self): |
| 816 | pass |
| 817 | m.testcase_1 = MyTestCase |
| 818 | |
| 819 | loader = unittest.TestLoader() |
| 820 | suite = loader.loadTestsFromNames(['testcase_1.test'], m) |
| 821 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 822 | |
| 823 | ref_suite = unittest.TestSuite([MyTestCase('test')]) |
| 824 | self.assertEqual(list(suite), [ref_suite]) |
| 825 | |
| 826 | # "The specifier name is a ``dotted name'' that may resolve ... to ... a |
| 827 | # test method within a test case class" |
| 828 | # |
| 829 | # Does the method gracefully handle names that initially look like they |
| 830 | # resolve to "a test method within a test case class" but don't? |
| 831 | def test_loadTestsFromNames__relative_invalid_testmethod(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 832 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 833 | class MyTestCase(unittest.TestCase): |
| 834 | def test(self): |
| 835 | pass |
| 836 | m.testcase_1 = MyTestCase |
| 837 | |
| 838 | loader = unittest.TestLoader() |
| 839 | try: |
| 840 | loader.loadTestsFromNames(['testcase_1.testfoo'], m) |
| 841 | except AttributeError as e: |
| 842 | self.assertEqual(str(e), "type object 'MyTestCase' has no attribute 'testfoo'") |
| 843 | else: |
| 844 | self.fail("Failed to raise AttributeError") |
| 845 | |
| 846 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 847 | # ... a callable object which returns a ... TestSuite instance" |
| 848 | def test_loadTestsFromNames__callable__TestSuite(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 849 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 850 | testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 851 | testcase_2 = unittest.FunctionTestCase(lambda: None) |
| 852 | def return_TestSuite(): |
| 853 | return unittest.TestSuite([testcase_1, testcase_2]) |
| 854 | m.return_TestSuite = return_TestSuite |
| 855 | |
| 856 | loader = unittest.TestLoader() |
| 857 | suite = loader.loadTestsFromNames(['return_TestSuite'], m) |
| 858 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 859 | |
| 860 | expected = unittest.TestSuite([testcase_1, testcase_2]) |
| 861 | self.assertEqual(list(suite), [expected]) |
| 862 | |
| 863 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 864 | # ... a callable object which returns a TestCase ... instance" |
| 865 | def test_loadTestsFromNames__callable__TestCase_instance(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 866 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 867 | testcase_1 = unittest.FunctionTestCase(lambda: None) |
| 868 | def return_TestCase(): |
| 869 | return testcase_1 |
| 870 | m.return_TestCase = return_TestCase |
| 871 | |
| 872 | loader = unittest.TestLoader() |
| 873 | suite = loader.loadTestsFromNames(['return_TestCase'], m) |
| 874 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 875 | |
| 876 | ref_suite = unittest.TestSuite([testcase_1]) |
| 877 | self.assertEqual(list(suite), [ref_suite]) |
| 878 | |
| 879 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 880 | # ... a callable object which returns a TestCase or TestSuite instance" |
| 881 | # |
| 882 | # Are staticmethods handled correctly? |
| 883 | def test_loadTestsFromNames__callable__call_staticmethod(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 884 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 885 | class Test1(unittest.TestCase): |
| 886 | def test(self): |
| 887 | pass |
| 888 | |
| 889 | testcase_1 = Test1('test') |
| 890 | class Foo(unittest.TestCase): |
| 891 | @staticmethod |
| 892 | def foo(): |
| 893 | return testcase_1 |
| 894 | m.Foo = Foo |
| 895 | |
| 896 | loader = unittest.TestLoader() |
| 897 | suite = loader.loadTestsFromNames(['Foo.foo'], m) |
| 898 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 899 | |
| 900 | ref_suite = unittest.TestSuite([testcase_1]) |
| 901 | self.assertEqual(list(suite), [ref_suite]) |
| 902 | |
| 903 | # "The specifier name is a ``dotted name'' that may resolve ... to |
| 904 | # ... a callable object which returns a TestCase or TestSuite instance" |
| 905 | # |
| 906 | # What happens when the callable returns something else? |
| 907 | def test_loadTestsFromNames__callable__wrong_type(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 908 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 909 | def return_wrong(): |
| 910 | return 6 |
| 911 | m.return_wrong = return_wrong |
| 912 | |
| 913 | loader = unittest.TestLoader() |
| 914 | try: |
| 915 | suite = loader.loadTestsFromNames(['return_wrong'], m) |
| 916 | except TypeError: |
| 917 | pass |
| 918 | else: |
| 919 | self.fail("TestLoader.loadTestsFromNames failed to raise TypeError") |
| 920 | |
| 921 | # "The specifier can refer to modules and packages which have not been |
| 922 | # imported; they will be imported as a side-effect" |
| 923 | def test_loadTestsFromNames__module_not_loaded(self): |
| 924 | # We're going to try to load this module as a side-effect, so it |
| 925 | # better not be loaded before we try. |
| 926 | # |
| 927 | # Why pick audioop? Google shows it isn't used very often, so there's |
| 928 | # a good chance that it won't be imported when this test is run |
| 929 | module_name = 'audioop' |
| 930 | |
| 931 | import sys |
| 932 | if module_name in sys.modules: |
| 933 | del sys.modules[module_name] |
| 934 | |
| 935 | loader = unittest.TestLoader() |
| 936 | try: |
| 937 | suite = loader.loadTestsFromNames([module_name]) |
| 938 | |
| 939 | self.failUnless(isinstance(suite, loader.suiteClass)) |
| 940 | self.assertEqual(list(suite), [unittest.TestSuite()]) |
| 941 | |
| 942 | # audioop should now be loaded, thanks to loadTestsFromName() |
| 943 | self.failUnless(module_name in sys.modules) |
| 944 | finally: |
Guido van Rossum | 360e4b8 | 2007-05-14 22:51:27 +0000 | [diff] [blame] | 945 | if module_name in sys.modules: |
| 946 | del sys.modules[module_name] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 947 | |
| 948 | ################################################################ |
| 949 | ### /Tests for TestLoader.loadTestsFromNames() |
| 950 | |
| 951 | ### Tests for TestLoader.getTestCaseNames() |
| 952 | ################################################################ |
| 953 | |
| 954 | # "Return a sorted sequence of method names found within testCaseClass" |
| 955 | # |
| 956 | # Test.foobar is defined to make sure getTestCaseNames() respects |
| 957 | # loader.testMethodPrefix |
| 958 | def test_getTestCaseNames(self): |
| 959 | class Test(unittest.TestCase): |
| 960 | def test_1(self): pass |
| 961 | def test_2(self): pass |
| 962 | def foobar(self): pass |
| 963 | |
| 964 | loader = unittest.TestLoader() |
| 965 | |
| 966 | self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2']) |
| 967 | |
| 968 | # "Return a sorted sequence of method names found within testCaseClass" |
| 969 | # |
| 970 | # Does getTestCaseNames() behave appropriately if no tests are found? |
| 971 | def test_getTestCaseNames__no_tests(self): |
| 972 | class Test(unittest.TestCase): |
| 973 | def foobar(self): pass |
| 974 | |
| 975 | loader = unittest.TestLoader() |
| 976 | |
| 977 | self.assertEqual(loader.getTestCaseNames(Test), []) |
| 978 | |
| 979 | # "Return a sorted sequence of method names found within testCaseClass" |
| 980 | # |
| 981 | # Are not-TestCases handled gracefully? |
| 982 | # |
| 983 | # XXX This should raise a TypeError, not return a list |
| 984 | # |
| 985 | # XXX It's too late in the 2.5 release cycle to fix this, but it should |
| 986 | # probably be revisited for 2.6 |
| 987 | def test_getTestCaseNames__not_a_TestCase(self): |
| 988 | class BadCase(int): |
| 989 | def test_foo(self): |
| 990 | pass |
| 991 | |
| 992 | loader = unittest.TestLoader() |
| 993 | names = loader.getTestCaseNames(BadCase) |
| 994 | |
| 995 | self.assertEqual(names, ['test_foo']) |
| 996 | |
| 997 | # "Return a sorted sequence of method names found within testCaseClass" |
| 998 | # |
| 999 | # Make sure inherited names are handled. |
| 1000 | # |
| 1001 | # TestP.foobar is defined to make sure getTestCaseNames() respects |
| 1002 | # loader.testMethodPrefix |
| 1003 | def test_getTestCaseNames__inheritance(self): |
| 1004 | class TestP(unittest.TestCase): |
| 1005 | def test_1(self): pass |
| 1006 | def test_2(self): pass |
| 1007 | def foobar(self): pass |
| 1008 | |
| 1009 | class TestC(TestP): |
| 1010 | def test_1(self): pass |
| 1011 | def test_3(self): pass |
| 1012 | |
| 1013 | loader = unittest.TestLoader() |
| 1014 | |
| 1015 | names = ['test_1', 'test_2', 'test_3'] |
| 1016 | self.assertEqual(loader.getTestCaseNames(TestC), names) |
| 1017 | |
| 1018 | ################################################################ |
| 1019 | ### /Tests for TestLoader.getTestCaseNames() |
| 1020 | |
| 1021 | ### Tests for TestLoader.testMethodPrefix |
| 1022 | ################################################################ |
| 1023 | |
| 1024 | # "String giving the prefix of method names which will be interpreted as |
| 1025 | # test methods" |
| 1026 | # |
| 1027 | # Implicit in the documentation is that testMethodPrefix is respected by |
| 1028 | # all loadTestsFrom* methods. |
| 1029 | def test_testMethodPrefix__loadTestsFromTestCase(self): |
| 1030 | class Foo(unittest.TestCase): |
| 1031 | def test_1(self): pass |
| 1032 | def test_2(self): pass |
| 1033 | def foo_bar(self): pass |
| 1034 | |
| 1035 | tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
| 1036 | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1037 | |
| 1038 | loader = unittest.TestLoader() |
| 1039 | loader.testMethodPrefix = 'foo' |
| 1040 | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1) |
| 1041 | |
| 1042 | loader.testMethodPrefix = 'test' |
| 1043 | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2) |
| 1044 | |
| 1045 | # "String giving the prefix of method names which will be interpreted as |
| 1046 | # test methods" |
| 1047 | # |
| 1048 | # Implicit in the documentation is that testMethodPrefix is respected by |
| 1049 | # all loadTestsFrom* methods. |
| 1050 | def test_testMethodPrefix__loadTestsFromModule(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1051 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1052 | class Foo(unittest.TestCase): |
| 1053 | def test_1(self): pass |
| 1054 | def test_2(self): pass |
| 1055 | def foo_bar(self): pass |
| 1056 | m.Foo = Foo |
| 1057 | |
| 1058 | tests_1 = [unittest.TestSuite([Foo('foo_bar')])] |
| 1059 | tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])] |
| 1060 | |
| 1061 | loader = unittest.TestLoader() |
| 1062 | loader.testMethodPrefix = 'foo' |
| 1063 | self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1) |
| 1064 | |
| 1065 | loader.testMethodPrefix = 'test' |
| 1066 | self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2) |
| 1067 | |
| 1068 | # "String giving the prefix of method names which will be interpreted as |
| 1069 | # test methods" |
| 1070 | # |
| 1071 | # Implicit in the documentation is that testMethodPrefix is respected by |
| 1072 | # all loadTestsFrom* methods. |
| 1073 | def test_testMethodPrefix__loadTestsFromName(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1074 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1075 | class Foo(unittest.TestCase): |
| 1076 | def test_1(self): pass |
| 1077 | def test_2(self): pass |
| 1078 | def foo_bar(self): pass |
| 1079 | m.Foo = Foo |
| 1080 | |
| 1081 | tests_1 = unittest.TestSuite([Foo('foo_bar')]) |
| 1082 | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1083 | |
| 1084 | loader = unittest.TestLoader() |
| 1085 | loader.testMethodPrefix = 'foo' |
| 1086 | self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1) |
| 1087 | |
| 1088 | loader.testMethodPrefix = 'test' |
| 1089 | self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2) |
| 1090 | |
| 1091 | # "String giving the prefix of method names which will be interpreted as |
| 1092 | # test methods" |
| 1093 | # |
| 1094 | # Implicit in the documentation is that testMethodPrefix is respected by |
| 1095 | # all loadTestsFrom* methods. |
| 1096 | def test_testMethodPrefix__loadTestsFromNames(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1097 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1098 | class Foo(unittest.TestCase): |
| 1099 | def test_1(self): pass |
| 1100 | def test_2(self): pass |
| 1101 | def foo_bar(self): pass |
| 1102 | m.Foo = Foo |
| 1103 | |
| 1104 | tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])]) |
| 1105 | tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')]) |
| 1106 | tests_2 = unittest.TestSuite([tests_2]) |
| 1107 | |
| 1108 | loader = unittest.TestLoader() |
| 1109 | loader.testMethodPrefix = 'foo' |
| 1110 | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1) |
| 1111 | |
| 1112 | loader.testMethodPrefix = 'test' |
| 1113 | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2) |
| 1114 | |
| 1115 | # "The default value is 'test'" |
| 1116 | def test_testMethodPrefix__default_value(self): |
| 1117 | loader = unittest.TestLoader() |
Guido van Rossum | e61fd5b | 2007-07-11 12:20:59 +0000 | [diff] [blame] | 1118 | self.assertEqual(loader.testMethodPrefix, 'test') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1119 | |
| 1120 | ################################################################ |
| 1121 | ### /Tests for TestLoader.testMethodPrefix |
| 1122 | |
| 1123 | ### Tests for TestLoader.sortTestMethodsUsing |
| 1124 | ################################################################ |
| 1125 | |
| 1126 | # "Function to be used to compare method names when sorting them in |
| 1127 | # getTestCaseNames() and all the loadTestsFromX() methods" |
| 1128 | def test_sortTestMethodsUsing__loadTestsFromTestCase(self): |
| 1129 | def reversed_cmp(x, y): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1130 | return -((x > y) - (x < y)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1131 | |
| 1132 | class Foo(unittest.TestCase): |
| 1133 | def test_1(self): pass |
| 1134 | def test_2(self): pass |
| 1135 | |
| 1136 | loader = unittest.TestLoader() |
| 1137 | loader.sortTestMethodsUsing = reversed_cmp |
| 1138 | |
| 1139 | tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
| 1140 | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 1141 | |
| 1142 | # "Function to be used to compare method names when sorting them in |
| 1143 | # getTestCaseNames() and all the loadTestsFromX() methods" |
| 1144 | def test_sortTestMethodsUsing__loadTestsFromModule(self): |
| 1145 | def reversed_cmp(x, y): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1146 | return -((x > y) - (x < y)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1147 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1148 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1149 | class Foo(unittest.TestCase): |
| 1150 | def test_1(self): pass |
| 1151 | def test_2(self): pass |
| 1152 | m.Foo = Foo |
| 1153 | |
| 1154 | loader = unittest.TestLoader() |
| 1155 | loader.sortTestMethodsUsing = reversed_cmp |
| 1156 | |
| 1157 | tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
| 1158 | self.assertEqual(list(loader.loadTestsFromModule(m)), tests) |
| 1159 | |
| 1160 | # "Function to be used to compare method names when sorting them in |
| 1161 | # getTestCaseNames() and all the loadTestsFromX() methods" |
| 1162 | def test_sortTestMethodsUsing__loadTestsFromName(self): |
| 1163 | def reversed_cmp(x, y): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1164 | return -((x > y) - (x < y)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1165 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1166 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1167 | class Foo(unittest.TestCase): |
| 1168 | def test_1(self): pass |
| 1169 | def test_2(self): pass |
| 1170 | m.Foo = Foo |
| 1171 | |
| 1172 | loader = unittest.TestLoader() |
| 1173 | loader.sortTestMethodsUsing = reversed_cmp |
| 1174 | |
| 1175 | tests = loader.suiteClass([Foo('test_2'), Foo('test_1')]) |
| 1176 | self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
| 1177 | |
| 1178 | # "Function to be used to compare method names when sorting them in |
| 1179 | # getTestCaseNames() and all the loadTestsFromX() methods" |
| 1180 | def test_sortTestMethodsUsing__loadTestsFromNames(self): |
| 1181 | def reversed_cmp(x, y): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1182 | return -((x > y) - (x < y)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1183 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1184 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1185 | class Foo(unittest.TestCase): |
| 1186 | def test_1(self): pass |
| 1187 | def test_2(self): pass |
| 1188 | m.Foo = Foo |
| 1189 | |
| 1190 | loader = unittest.TestLoader() |
| 1191 | loader.sortTestMethodsUsing = reversed_cmp |
| 1192 | |
| 1193 | tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])] |
| 1194 | self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests) |
| 1195 | |
| 1196 | # "Function to be used to compare method names when sorting them in |
| 1197 | # getTestCaseNames()" |
| 1198 | # |
| 1199 | # Does it actually affect getTestCaseNames()? |
| 1200 | def test_sortTestMethodsUsing__getTestCaseNames(self): |
| 1201 | def reversed_cmp(x, y): |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1202 | return -((x > y) - (x < y)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1203 | |
| 1204 | class Foo(unittest.TestCase): |
| 1205 | def test_1(self): pass |
| 1206 | def test_2(self): pass |
| 1207 | |
| 1208 | loader = unittest.TestLoader() |
| 1209 | loader.sortTestMethodsUsing = reversed_cmp |
| 1210 | |
| 1211 | test_names = ['test_2', 'test_1'] |
| 1212 | self.assertEqual(loader.getTestCaseNames(Foo), test_names) |
| 1213 | |
| 1214 | # "The default value is the built-in cmp() function" |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1215 | # Since cmp is now defunct, we simply verify that the results |
| 1216 | # occur in the same order as they would with the default sort. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1217 | def test_sortTestMethodsUsing__default_value(self): |
| 1218 | loader = unittest.TestLoader() |
Mark Dickinson | a56c467 | 2009-01-27 18:17:45 +0000 | [diff] [blame] | 1219 | |
| 1220 | class Foo(unittest.TestCase): |
| 1221 | def test_2(self): pass |
| 1222 | def test_3(self): pass |
| 1223 | def test_1(self): pass |
| 1224 | |
| 1225 | test_names = ['test_2', 'test_3', 'test_1'] |
| 1226 | self.assertEqual(loader.getTestCaseNames(Foo), sorted(test_names)) |
| 1227 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1228 | |
| 1229 | # "it can be set to None to disable the sort." |
| 1230 | # |
| 1231 | # XXX How is this different from reassigning cmp? Are the tests returned |
| 1232 | # in a random order or something? This behaviour should die |
| 1233 | def test_sortTestMethodsUsing__None(self): |
| 1234 | class Foo(unittest.TestCase): |
| 1235 | def test_1(self): pass |
| 1236 | def test_2(self): pass |
| 1237 | |
| 1238 | loader = unittest.TestLoader() |
| 1239 | loader.sortTestMethodsUsing = None |
| 1240 | |
| 1241 | test_names = ['test_2', 'test_1'] |
| 1242 | self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names)) |
| 1243 | |
| 1244 | ################################################################ |
| 1245 | ### /Tests for TestLoader.sortTestMethodsUsing |
| 1246 | |
| 1247 | ### Tests for TestLoader.suiteClass |
| 1248 | ################################################################ |
| 1249 | |
| 1250 | # "Callable object that constructs a test suite from a list of tests." |
| 1251 | def test_suiteClass__loadTestsFromTestCase(self): |
| 1252 | class Foo(unittest.TestCase): |
| 1253 | def test_1(self): pass |
| 1254 | def test_2(self): pass |
| 1255 | def foo_bar(self): pass |
| 1256 | |
| 1257 | tests = [Foo('test_1'), Foo('test_2')] |
| 1258 | |
| 1259 | loader = unittest.TestLoader() |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 1260 | loader.classSuiteClass = MyClassSuite |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1261 | self.assertEqual(loader.loadTestsFromTestCase(Foo), tests) |
| 1262 | |
| 1263 | # It is implicit in the documentation for TestLoader.suiteClass that |
| 1264 | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1265 | def test_suiteClass__loadTestsFromModule(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1266 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1267 | class Foo(unittest.TestCase): |
| 1268 | def test_1(self): pass |
| 1269 | def test_2(self): pass |
| 1270 | def foo_bar(self): pass |
| 1271 | m.Foo = Foo |
| 1272 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 1273 | tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1274 | |
| 1275 | loader = unittest.TestLoader() |
| 1276 | loader.suiteClass = list |
| 1277 | self.assertEqual(loader.loadTestsFromModule(m), tests) |
| 1278 | |
| 1279 | # It is implicit in the documentation for TestLoader.suiteClass that |
| 1280 | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1281 | def test_suiteClass__loadTestsFromName(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1282 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1283 | class Foo(unittest.TestCase): |
| 1284 | def test_1(self): pass |
| 1285 | def test_2(self): pass |
| 1286 | def foo_bar(self): pass |
| 1287 | m.Foo = Foo |
| 1288 | |
| 1289 | tests = [Foo('test_1'), Foo('test_2')] |
| 1290 | |
| 1291 | loader = unittest.TestLoader() |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 1292 | loader.classSuiteClass = MyClassSuite |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1293 | self.assertEqual(loader.loadTestsFromName('Foo', m), tests) |
| 1294 | |
| 1295 | # It is implicit in the documentation for TestLoader.suiteClass that |
| 1296 | # all TestLoader.loadTestsFrom* methods respect it. Let's make sure |
| 1297 | def test_suiteClass__loadTestsFromNames(self): |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1298 | m = types.ModuleType('m') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1299 | class Foo(unittest.TestCase): |
| 1300 | def test_1(self): pass |
| 1301 | def test_2(self): pass |
| 1302 | def foo_bar(self): pass |
| 1303 | m.Foo = Foo |
| 1304 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 1305 | tests = [unittest.ClassTestSuite([Foo('test_1'), Foo('test_2')], Foo)] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1306 | |
| 1307 | loader = unittest.TestLoader() |
| 1308 | loader.suiteClass = list |
| 1309 | self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests) |
| 1310 | |
| 1311 | # "The default value is the TestSuite class" |
| 1312 | def test_suiteClass__default_value(self): |
| 1313 | loader = unittest.TestLoader() |
| 1314 | self.failUnless(loader.suiteClass is unittest.TestSuite) |
| 1315 | |
| 1316 | ################################################################ |
| 1317 | ### /Tests for TestLoader.suiteClass |
| 1318 | |
| 1319 | ### Support code for Test_TestSuite |
| 1320 | ################################################################ |
| 1321 | |
| 1322 | class Foo(unittest.TestCase): |
| 1323 | def test_1(self): pass |
| 1324 | def test_2(self): pass |
| 1325 | def test_3(self): pass |
| 1326 | def runTest(self): pass |
| 1327 | |
| 1328 | def _mk_TestSuite(*names): |
| 1329 | return unittest.TestSuite(Foo(n) for n in names) |
| 1330 | |
| 1331 | ################################################################ |
| 1332 | ### /Support code for Test_TestSuite |
| 1333 | |
| 1334 | class Test_TestSuite(TestCase, TestEquality): |
| 1335 | |
| 1336 | ### Set up attributes needed by inherited tests |
| 1337 | ################################################################ |
| 1338 | |
| 1339 | # Used by TestEquality.test_eq |
| 1340 | eq_pairs = [(unittest.TestSuite(), unittest.TestSuite()) |
| 1341 | ,(unittest.TestSuite(), unittest.TestSuite([])) |
| 1342 | ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_1'))] |
| 1343 | |
| 1344 | # Used by TestEquality.test_ne |
| 1345 | ne_pairs = [(unittest.TestSuite(), _mk_TestSuite('test_1')) |
| 1346 | ,(unittest.TestSuite([]), _mk_TestSuite('test_1')) |
| 1347 | ,(_mk_TestSuite('test_1', 'test_2'), _mk_TestSuite('test_1', 'test_3')) |
| 1348 | ,(_mk_TestSuite('test_1'), _mk_TestSuite('test_2'))] |
| 1349 | |
| 1350 | ################################################################ |
| 1351 | ### /Set up attributes needed by inherited tests |
| 1352 | |
| 1353 | ### Tests for TestSuite.__init__ |
| 1354 | ################################################################ |
| 1355 | |
| 1356 | # "class TestSuite([tests])" |
| 1357 | # |
| 1358 | # The tests iterable should be optional |
| 1359 | def test_init__tests_optional(self): |
| 1360 | suite = unittest.TestSuite() |
| 1361 | |
| 1362 | self.assertEqual(suite.countTestCases(), 0) |
| 1363 | |
| 1364 | # "class TestSuite([tests])" |
| 1365 | # ... |
| 1366 | # "If tests is given, it must be an iterable of individual test cases |
| 1367 | # or other test suites that will be used to build the suite initially" |
| 1368 | # |
| 1369 | # TestSuite should deal with empty tests iterables by allowing the |
| 1370 | # creation of an empty suite |
| 1371 | def test_init__empty_tests(self): |
| 1372 | suite = unittest.TestSuite([]) |
| 1373 | |
| 1374 | self.assertEqual(suite.countTestCases(), 0) |
| 1375 | |
| 1376 | # "class TestSuite([tests])" |
| 1377 | # ... |
| 1378 | # "If tests is given, it must be an iterable of individual test cases |
| 1379 | # or other test suites that will be used to build the suite initially" |
| 1380 | # |
| 1381 | # TestSuite should allow any iterable to provide tests |
| 1382 | def test_init__tests_from_any_iterable(self): |
| 1383 | def tests(): |
| 1384 | yield unittest.FunctionTestCase(lambda: None) |
| 1385 | yield unittest.FunctionTestCase(lambda: None) |
| 1386 | |
| 1387 | suite_1 = unittest.TestSuite(tests()) |
| 1388 | self.assertEqual(suite_1.countTestCases(), 2) |
| 1389 | |
| 1390 | suite_2 = unittest.TestSuite(suite_1) |
| 1391 | self.assertEqual(suite_2.countTestCases(), 2) |
| 1392 | |
| 1393 | suite_3 = unittest.TestSuite(set(suite_1)) |
| 1394 | self.assertEqual(suite_3.countTestCases(), 2) |
| 1395 | |
| 1396 | # "class TestSuite([tests])" |
| 1397 | # ... |
| 1398 | # "If tests is given, it must be an iterable of individual test cases |
| 1399 | # or other test suites that will be used to build the suite initially" |
| 1400 | # |
| 1401 | # Does TestSuite() also allow other TestSuite() instances to be present |
| 1402 | # in the tests iterable? |
| 1403 | def test_init__TestSuite_instances_in_tests(self): |
| 1404 | def tests(): |
| 1405 | ftc = unittest.FunctionTestCase(lambda: None) |
| 1406 | yield unittest.TestSuite([ftc]) |
| 1407 | yield unittest.FunctionTestCase(lambda: None) |
| 1408 | |
| 1409 | suite = unittest.TestSuite(tests()) |
| 1410 | self.assertEqual(suite.countTestCases(), 2) |
| 1411 | |
| 1412 | ################################################################ |
| 1413 | ### /Tests for TestSuite.__init__ |
| 1414 | |
| 1415 | # Container types should support the iter protocol |
| 1416 | def test_iter(self): |
| 1417 | test1 = unittest.FunctionTestCase(lambda: None) |
| 1418 | test2 = unittest.FunctionTestCase(lambda: None) |
| 1419 | suite = unittest.TestSuite((test1, test2)) |
| 1420 | |
| 1421 | self.assertEqual(list(suite), [test1, test2]) |
| 1422 | |
| 1423 | # "Return the number of tests represented by the this test object. |
| 1424 | # ...this method is also implemented by the TestSuite class, which can |
| 1425 | # return larger [greater than 1] values" |
| 1426 | # |
| 1427 | # Presumably an empty TestSuite returns 0? |
| 1428 | def test_countTestCases_zero_simple(self): |
| 1429 | suite = unittest.TestSuite() |
| 1430 | |
| 1431 | self.assertEqual(suite.countTestCases(), 0) |
| 1432 | |
| 1433 | # "Return the number of tests represented by the this test object. |
| 1434 | # ...this method is also implemented by the TestSuite class, which can |
| 1435 | # return larger [greater than 1] values" |
| 1436 | # |
| 1437 | # Presumably an empty TestSuite (even if it contains other empty |
| 1438 | # TestSuite instances) returns 0? |
| 1439 | def test_countTestCases_zero_nested(self): |
| 1440 | class Test1(unittest.TestCase): |
| 1441 | def test(self): |
| 1442 | pass |
| 1443 | |
| 1444 | suite = unittest.TestSuite([unittest.TestSuite()]) |
| 1445 | |
| 1446 | self.assertEqual(suite.countTestCases(), 0) |
| 1447 | |
| 1448 | # "Return the number of tests represented by the this test object. |
| 1449 | # ...this method is also implemented by the TestSuite class, which can |
| 1450 | # return larger [greater than 1] values" |
| 1451 | def test_countTestCases_simple(self): |
| 1452 | test1 = unittest.FunctionTestCase(lambda: None) |
| 1453 | test2 = unittest.FunctionTestCase(lambda: None) |
| 1454 | suite = unittest.TestSuite((test1, test2)) |
| 1455 | |
| 1456 | self.assertEqual(suite.countTestCases(), 2) |
| 1457 | |
| 1458 | # "Return the number of tests represented by the this test object. |
| 1459 | # ...this method is also implemented by the TestSuite class, which can |
| 1460 | # return larger [greater than 1] values" |
| 1461 | # |
| 1462 | # Make sure this holds for nested TestSuite instances, too |
| 1463 | def test_countTestCases_nested(self): |
| 1464 | class Test1(unittest.TestCase): |
| 1465 | def test1(self): pass |
| 1466 | def test2(self): pass |
| 1467 | |
| 1468 | test2 = unittest.FunctionTestCase(lambda: None) |
| 1469 | test3 = unittest.FunctionTestCase(lambda: None) |
| 1470 | child = unittest.TestSuite((Test1('test2'), test2)) |
| 1471 | parent = unittest.TestSuite((test3, child, Test1('test1'))) |
| 1472 | |
| 1473 | self.assertEqual(parent.countTestCases(), 4) |
| 1474 | |
| 1475 | # "Run the tests associated with this suite, collecting the result into |
| 1476 | # the test result object passed as result." |
| 1477 | # |
| 1478 | # And if there are no tests? What then? |
| 1479 | def test_run__empty_suite(self): |
| 1480 | events = [] |
| 1481 | result = LoggingResult(events) |
| 1482 | |
| 1483 | suite = unittest.TestSuite() |
| 1484 | |
| 1485 | suite.run(result) |
| 1486 | |
| 1487 | self.assertEqual(events, []) |
| 1488 | |
| 1489 | # "Note that unlike TestCase.run(), TestSuite.run() requires the |
| 1490 | # "result object to be passed in." |
| 1491 | def test_run__requires_result(self): |
| 1492 | suite = unittest.TestSuite() |
| 1493 | |
| 1494 | try: |
| 1495 | suite.run() |
| 1496 | except TypeError: |
| 1497 | pass |
| 1498 | else: |
| 1499 | self.fail("Failed to raise TypeError") |
| 1500 | |
| 1501 | # "Run the tests associated with this suite, collecting the result into |
| 1502 | # the test result object passed as result." |
| 1503 | def test_run(self): |
| 1504 | events = [] |
| 1505 | result = LoggingResult(events) |
| 1506 | |
| 1507 | class LoggingCase(unittest.TestCase): |
| 1508 | def run(self, result): |
| 1509 | events.append('run %s' % self._testMethodName) |
| 1510 | |
| 1511 | def test1(self): pass |
| 1512 | def test2(self): pass |
| 1513 | |
| 1514 | tests = [LoggingCase('test1'), LoggingCase('test2')] |
| 1515 | |
| 1516 | unittest.TestSuite(tests).run(result) |
| 1517 | |
| 1518 | self.assertEqual(events, ['run test1', 'run test2']) |
| 1519 | |
| 1520 | # "Add a TestCase ... to the suite" |
| 1521 | def test_addTest__TestCase(self): |
| 1522 | class Foo(unittest.TestCase): |
| 1523 | def test(self): pass |
| 1524 | |
| 1525 | test = Foo('test') |
| 1526 | suite = unittest.TestSuite() |
| 1527 | |
| 1528 | suite.addTest(test) |
| 1529 | |
| 1530 | self.assertEqual(suite.countTestCases(), 1) |
| 1531 | self.assertEqual(list(suite), [test]) |
| 1532 | |
| 1533 | # "Add a ... TestSuite to the suite" |
| 1534 | def test_addTest__TestSuite(self): |
| 1535 | class Foo(unittest.TestCase): |
| 1536 | def test(self): pass |
| 1537 | |
| 1538 | suite_2 = unittest.TestSuite([Foo('test')]) |
| 1539 | |
| 1540 | suite = unittest.TestSuite() |
| 1541 | suite.addTest(suite_2) |
| 1542 | |
| 1543 | self.assertEqual(suite.countTestCases(), 1) |
| 1544 | self.assertEqual(list(suite), [suite_2]) |
| 1545 | |
| 1546 | # "Add all the tests from an iterable of TestCase and TestSuite |
| 1547 | # instances to this test suite." |
| 1548 | # |
| 1549 | # "This is equivalent to iterating over tests, calling addTest() for |
| 1550 | # each element" |
| 1551 | def test_addTests(self): |
| 1552 | class Foo(unittest.TestCase): |
| 1553 | def test_1(self): pass |
| 1554 | def test_2(self): pass |
| 1555 | |
| 1556 | test_1 = Foo('test_1') |
| 1557 | test_2 = Foo('test_2') |
| 1558 | inner_suite = unittest.TestSuite([test_2]) |
| 1559 | |
| 1560 | def gen(): |
| 1561 | yield test_1 |
| 1562 | yield test_2 |
| 1563 | yield inner_suite |
| 1564 | |
| 1565 | suite_1 = unittest.TestSuite() |
| 1566 | suite_1.addTests(gen()) |
| 1567 | |
| 1568 | self.assertEqual(list(suite_1), list(gen())) |
| 1569 | |
| 1570 | # "This is equivalent to iterating over tests, calling addTest() for |
| 1571 | # each element" |
| 1572 | suite_2 = unittest.TestSuite() |
| 1573 | for t in gen(): |
| 1574 | suite_2.addTest(t) |
| 1575 | |
| 1576 | self.assertEqual(suite_1, suite_2) |
| 1577 | |
| 1578 | # "Add all the tests from an iterable of TestCase and TestSuite |
| 1579 | # instances to this test suite." |
| 1580 | # |
| 1581 | # What happens if it doesn't get an iterable? |
| 1582 | def test_addTest__noniterable(self): |
| 1583 | suite = unittest.TestSuite() |
| 1584 | |
| 1585 | try: |
| 1586 | suite.addTests(5) |
| 1587 | except TypeError: |
| 1588 | pass |
| 1589 | else: |
| 1590 | self.fail("Failed to raise TypeError") |
| 1591 | |
| 1592 | def test_addTest__noncallable(self): |
| 1593 | suite = unittest.TestSuite() |
| 1594 | self.assertRaises(TypeError, suite.addTest, 5) |
| 1595 | |
| 1596 | def test_addTest__casesuiteclass(self): |
| 1597 | suite = unittest.TestSuite() |
| 1598 | self.assertRaises(TypeError, suite.addTest, Test_TestSuite) |
| 1599 | self.assertRaises(TypeError, suite.addTest, unittest.TestSuite) |
| 1600 | |
| 1601 | def test_addTests__string(self): |
| 1602 | suite = unittest.TestSuite() |
| 1603 | self.assertRaises(TypeError, suite.addTests, "foo") |
| 1604 | |
| 1605 | |
| 1606 | class Test_FunctionTestCase(TestCase): |
| 1607 | |
| 1608 | # "Return the number of tests represented by the this test object. For |
| 1609 | # TestCase instances, this will always be 1" |
| 1610 | def test_countTestCases(self): |
| 1611 | test = unittest.FunctionTestCase(lambda: None) |
| 1612 | |
| 1613 | self.assertEqual(test.countTestCases(), 1) |
| 1614 | |
| 1615 | # "When a setUp() method is defined, the test runner will run that method |
| 1616 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 1617 | # test runner will invoke that method after each test. In the example, |
| 1618 | # setUp() was used to create a fresh sequence for each test." |
| 1619 | # |
| 1620 | # Make sure the proper call order is maintained, even if setUp() raises |
| 1621 | # an exception. |
| 1622 | def test_run_call_order__error_in_setUp(self): |
| 1623 | events = [] |
| 1624 | result = LoggingResult(events) |
| 1625 | |
| 1626 | def setUp(): |
| 1627 | events.append('setUp') |
| 1628 | raise RuntimeError('raised by setUp') |
| 1629 | |
| 1630 | def test(): |
| 1631 | events.append('test') |
| 1632 | |
| 1633 | def tearDown(): |
| 1634 | events.append('tearDown') |
| 1635 | |
| 1636 | expected = ['startTest', 'setUp', 'addError', 'stopTest'] |
| 1637 | unittest.FunctionTestCase(test, setUp, tearDown).run(result) |
| 1638 | self.assertEqual(events, expected) |
| 1639 | |
| 1640 | # "When a setUp() method is defined, the test runner will run that method |
| 1641 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 1642 | # test runner will invoke that method after each test. In the example, |
| 1643 | # setUp() was used to create a fresh sequence for each test." |
| 1644 | # |
| 1645 | # Make sure the proper call order is maintained, even if the test raises |
| 1646 | # an error (as opposed to a failure). |
| 1647 | def test_run_call_order__error_in_test(self): |
| 1648 | events = [] |
| 1649 | result = LoggingResult(events) |
| 1650 | |
| 1651 | def setUp(): |
| 1652 | events.append('setUp') |
| 1653 | |
| 1654 | def test(): |
| 1655 | events.append('test') |
| 1656 | raise RuntimeError('raised by test') |
| 1657 | |
| 1658 | def tearDown(): |
| 1659 | events.append('tearDown') |
| 1660 | |
| 1661 | expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', |
| 1662 | 'stopTest'] |
| 1663 | unittest.FunctionTestCase(test, setUp, tearDown).run(result) |
| 1664 | self.assertEqual(events, expected) |
| 1665 | |
| 1666 | # "When a setUp() method is defined, the test runner will run that method |
| 1667 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 1668 | # test runner will invoke that method after each test. In the example, |
| 1669 | # setUp() was used to create a fresh sequence for each test." |
| 1670 | # |
| 1671 | # Make sure the proper call order is maintained, even if the test signals |
| 1672 | # a failure (as opposed to an error). |
| 1673 | def test_run_call_order__failure_in_test(self): |
| 1674 | events = [] |
| 1675 | result = LoggingResult(events) |
| 1676 | |
| 1677 | def setUp(): |
| 1678 | events.append('setUp') |
| 1679 | |
| 1680 | def test(): |
| 1681 | events.append('test') |
| 1682 | self.fail('raised by test') |
| 1683 | |
| 1684 | def tearDown(): |
| 1685 | events.append('tearDown') |
| 1686 | |
| 1687 | expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', |
| 1688 | 'stopTest'] |
| 1689 | unittest.FunctionTestCase(test, setUp, tearDown).run(result) |
| 1690 | self.assertEqual(events, expected) |
| 1691 | |
| 1692 | # "When a setUp() method is defined, the test runner will run that method |
| 1693 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 1694 | # test runner will invoke that method after each test. In the example, |
| 1695 | # setUp() was used to create a fresh sequence for each test." |
| 1696 | # |
| 1697 | # Make sure the proper call order is maintained, even if tearDown() raises |
| 1698 | # an exception. |
| 1699 | def test_run_call_order__error_in_tearDown(self): |
| 1700 | events = [] |
| 1701 | result = LoggingResult(events) |
| 1702 | |
| 1703 | def setUp(): |
| 1704 | events.append('setUp') |
| 1705 | |
| 1706 | def test(): |
| 1707 | events.append('test') |
| 1708 | |
| 1709 | def tearDown(): |
| 1710 | events.append('tearDown') |
| 1711 | raise RuntimeError('raised by tearDown') |
| 1712 | |
| 1713 | expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', |
| 1714 | 'stopTest'] |
| 1715 | unittest.FunctionTestCase(test, setUp, tearDown).run(result) |
| 1716 | self.assertEqual(events, expected) |
| 1717 | |
| 1718 | # "Return a string identifying the specific test case." |
| 1719 | # |
| 1720 | # Because of the vague nature of the docs, I'm not going to lock this |
| 1721 | # test down too much. Really all that can be asserted is that the id() |
| 1722 | # will be a string (either 8-byte or unicode -- again, because the docs |
| 1723 | # just say "string") |
| 1724 | def test_id(self): |
| 1725 | test = unittest.FunctionTestCase(lambda: None) |
| 1726 | |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 1727 | self.failUnless(isinstance(test.id(), str)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1728 | |
| 1729 | # "Returns a one-line description of the test, or None if no description |
| 1730 | # has been provided. The default implementation of this method returns |
| 1731 | # the first line of the test method's docstring, if available, or None." |
| 1732 | def test_shortDescription__no_docstring(self): |
| 1733 | test = unittest.FunctionTestCase(lambda: None) |
| 1734 | |
| 1735 | self.assertEqual(test.shortDescription(), None) |
| 1736 | |
| 1737 | # "Returns a one-line description of the test, or None if no description |
| 1738 | # has been provided. The default implementation of this method returns |
| 1739 | # the first line of the test method's docstring, if available, or None." |
| 1740 | def test_shortDescription__singleline_docstring(self): |
| 1741 | desc = "this tests foo" |
| 1742 | test = unittest.FunctionTestCase(lambda: None, description=desc) |
| 1743 | |
| 1744 | self.assertEqual(test.shortDescription(), "this tests foo") |
| 1745 | |
| 1746 | class Test_TestResult(TestCase): |
| 1747 | # Note: there are not separate tests for TestResult.wasSuccessful(), |
| 1748 | # TestResult.errors, TestResult.failures, TestResult.testsRun or |
| 1749 | # TestResult.shouldStop because these only have meaning in terms of |
| 1750 | # other TestResult methods. |
| 1751 | # |
| 1752 | # Accordingly, tests for the aforenamed attributes are incorporated |
| 1753 | # in with the tests for the defining methods. |
| 1754 | ################################################################ |
| 1755 | |
| 1756 | def test_init(self): |
| 1757 | result = unittest.TestResult() |
| 1758 | |
| 1759 | self.failUnless(result.wasSuccessful()) |
| 1760 | self.assertEqual(len(result.errors), 0) |
| 1761 | self.assertEqual(len(result.failures), 0) |
| 1762 | self.assertEqual(result.testsRun, 0) |
| 1763 | self.assertEqual(result.shouldStop, False) |
| 1764 | |
| 1765 | # "This method can be called to signal that the set of tests being |
| 1766 | # run should be aborted by setting the TestResult's shouldStop |
| 1767 | # attribute to True." |
| 1768 | def test_stop(self): |
| 1769 | result = unittest.TestResult() |
| 1770 | |
| 1771 | result.stop() |
| 1772 | |
| 1773 | self.assertEqual(result.shouldStop, True) |
| 1774 | |
| 1775 | # "Called when the test case test is about to be run. The default |
| 1776 | # implementation simply increments the instance's testsRun counter." |
| 1777 | def test_startTest(self): |
| 1778 | class Foo(unittest.TestCase): |
| 1779 | def test_1(self): |
| 1780 | pass |
| 1781 | |
| 1782 | test = Foo('test_1') |
| 1783 | |
| 1784 | result = unittest.TestResult() |
| 1785 | |
| 1786 | result.startTest(test) |
| 1787 | |
| 1788 | self.failUnless(result.wasSuccessful()) |
| 1789 | self.assertEqual(len(result.errors), 0) |
| 1790 | self.assertEqual(len(result.failures), 0) |
| 1791 | self.assertEqual(result.testsRun, 1) |
| 1792 | self.assertEqual(result.shouldStop, False) |
| 1793 | |
| 1794 | result.stopTest(test) |
| 1795 | |
| 1796 | # "Called after the test case test has been executed, regardless of |
| 1797 | # the outcome. The default implementation does nothing." |
| 1798 | def test_stopTest(self): |
| 1799 | class Foo(unittest.TestCase): |
| 1800 | def test_1(self): |
| 1801 | pass |
| 1802 | |
| 1803 | test = Foo('test_1') |
| 1804 | |
| 1805 | result = unittest.TestResult() |
| 1806 | |
| 1807 | result.startTest(test) |
| 1808 | |
| 1809 | self.failUnless(result.wasSuccessful()) |
| 1810 | self.assertEqual(len(result.errors), 0) |
| 1811 | self.assertEqual(len(result.failures), 0) |
| 1812 | self.assertEqual(result.testsRun, 1) |
| 1813 | self.assertEqual(result.shouldStop, False) |
| 1814 | |
| 1815 | result.stopTest(test) |
| 1816 | |
| 1817 | # Same tests as above; make sure nothing has changed |
| 1818 | self.failUnless(result.wasSuccessful()) |
| 1819 | self.assertEqual(len(result.errors), 0) |
| 1820 | self.assertEqual(len(result.failures), 0) |
| 1821 | self.assertEqual(result.testsRun, 1) |
| 1822 | self.assertEqual(result.shouldStop, False) |
| 1823 | |
| 1824 | # "addSuccess(test)" |
| 1825 | # ... |
| 1826 | # "Called when the test case test succeeds" |
| 1827 | # ... |
| 1828 | # "wasSuccessful() - Returns True if all tests run so far have passed, |
| 1829 | # otherwise returns False" |
| 1830 | # ... |
| 1831 | # "testsRun - The total number of tests run so far." |
| 1832 | # ... |
| 1833 | # "errors - A list containing 2-tuples of TestCase instances and |
| 1834 | # formatted tracebacks. Each tuple represents a test which raised an |
| 1835 | # unexpected exception. Contains formatted |
| 1836 | # tracebacks instead of sys.exc_info() results." |
| 1837 | # ... |
| 1838 | # "failures - A list containing 2-tuples of TestCase instances and |
| 1839 | # formatted tracebacks. Each tuple represents a test where a failure was |
| 1840 | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 1841 | # methods. Contains formatted tracebacks instead |
| 1842 | # of sys.exc_info() results." |
| 1843 | def test_addSuccess(self): |
| 1844 | class Foo(unittest.TestCase): |
| 1845 | def test_1(self): |
| 1846 | pass |
| 1847 | |
| 1848 | test = Foo('test_1') |
| 1849 | |
| 1850 | result = unittest.TestResult() |
| 1851 | |
| 1852 | result.startTest(test) |
| 1853 | result.addSuccess(test) |
| 1854 | result.stopTest(test) |
| 1855 | |
| 1856 | self.failUnless(result.wasSuccessful()) |
| 1857 | self.assertEqual(len(result.errors), 0) |
| 1858 | self.assertEqual(len(result.failures), 0) |
| 1859 | self.assertEqual(result.testsRun, 1) |
| 1860 | self.assertEqual(result.shouldStop, False) |
| 1861 | |
| 1862 | # "addFailure(test, err)" |
| 1863 | # ... |
| 1864 | # "Called when the test case test signals a failure. err is a tuple of |
| 1865 | # the form returned by sys.exc_info(): (type, value, traceback)" |
| 1866 | # ... |
| 1867 | # "wasSuccessful() - Returns True if all tests run so far have passed, |
| 1868 | # otherwise returns False" |
| 1869 | # ... |
| 1870 | # "testsRun - The total number of tests run so far." |
| 1871 | # ... |
| 1872 | # "errors - A list containing 2-tuples of TestCase instances and |
| 1873 | # formatted tracebacks. Each tuple represents a test which raised an |
| 1874 | # unexpected exception. Contains formatted |
| 1875 | # tracebacks instead of sys.exc_info() results." |
| 1876 | # ... |
| 1877 | # "failures - A list containing 2-tuples of TestCase instances and |
| 1878 | # formatted tracebacks. Each tuple represents a test where a failure was |
| 1879 | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 1880 | # methods. Contains formatted tracebacks instead |
| 1881 | # of sys.exc_info() results." |
| 1882 | def test_addFailure(self): |
| 1883 | import sys |
| 1884 | |
| 1885 | class Foo(unittest.TestCase): |
| 1886 | def test_1(self): |
| 1887 | pass |
| 1888 | |
| 1889 | test = Foo('test_1') |
| 1890 | try: |
| 1891 | test.fail("foo") |
| 1892 | except: |
| 1893 | exc_info_tuple = sys.exc_info() |
| 1894 | |
| 1895 | result = unittest.TestResult() |
| 1896 | |
| 1897 | result.startTest(test) |
| 1898 | result.addFailure(test, exc_info_tuple) |
| 1899 | result.stopTest(test) |
| 1900 | |
| 1901 | self.failIf(result.wasSuccessful()) |
| 1902 | self.assertEqual(len(result.errors), 0) |
| 1903 | self.assertEqual(len(result.failures), 1) |
| 1904 | self.assertEqual(result.testsRun, 1) |
| 1905 | self.assertEqual(result.shouldStop, False) |
| 1906 | |
| 1907 | test_case, formatted_exc = result.failures[0] |
| 1908 | self.failUnless(test_case is test) |
| 1909 | self.failUnless(isinstance(formatted_exc, str)) |
| 1910 | |
| 1911 | # "addError(test, err)" |
| 1912 | # ... |
| 1913 | # "Called when the test case test raises an unexpected exception err |
| 1914 | # is a tuple of the form returned by sys.exc_info(): |
| 1915 | # (type, value, traceback)" |
| 1916 | # ... |
| 1917 | # "wasSuccessful() - Returns True if all tests run so far have passed, |
| 1918 | # otherwise returns False" |
| 1919 | # ... |
| 1920 | # "testsRun - The total number of tests run so far." |
| 1921 | # ... |
| 1922 | # "errors - A list containing 2-tuples of TestCase instances and |
| 1923 | # formatted tracebacks. Each tuple represents a test which raised an |
| 1924 | # unexpected exception. Contains formatted |
| 1925 | # tracebacks instead of sys.exc_info() results." |
| 1926 | # ... |
| 1927 | # "failures - A list containing 2-tuples of TestCase instances and |
| 1928 | # formatted tracebacks. Each tuple represents a test where a failure was |
| 1929 | # explicitly signalled using the TestCase.fail*() or TestCase.assert*() |
| 1930 | # methods. Contains formatted tracebacks instead |
| 1931 | # of sys.exc_info() results." |
| 1932 | def test_addError(self): |
| 1933 | import sys |
| 1934 | |
| 1935 | class Foo(unittest.TestCase): |
| 1936 | def test_1(self): |
| 1937 | pass |
| 1938 | |
| 1939 | test = Foo('test_1') |
| 1940 | try: |
| 1941 | raise TypeError() |
| 1942 | except: |
| 1943 | exc_info_tuple = sys.exc_info() |
| 1944 | |
| 1945 | result = unittest.TestResult() |
| 1946 | |
| 1947 | result.startTest(test) |
| 1948 | result.addError(test, exc_info_tuple) |
| 1949 | result.stopTest(test) |
| 1950 | |
| 1951 | self.failIf(result.wasSuccessful()) |
| 1952 | self.assertEqual(len(result.errors), 1) |
| 1953 | self.assertEqual(len(result.failures), 0) |
| 1954 | self.assertEqual(result.testsRun, 1) |
| 1955 | self.assertEqual(result.shouldStop, False) |
| 1956 | |
| 1957 | test_case, formatted_exc = result.errors[0] |
| 1958 | self.failUnless(test_case is test) |
| 1959 | self.failUnless(isinstance(formatted_exc, str)) |
| 1960 | |
| 1961 | ### Support code for Test_TestCase |
| 1962 | ################################################################ |
| 1963 | |
| 1964 | class Foo(unittest.TestCase): |
| 1965 | def runTest(self): pass |
| 1966 | def test1(self): pass |
| 1967 | |
| 1968 | class Bar(Foo): |
| 1969 | def test2(self): pass |
| 1970 | |
| 1971 | ################################################################ |
| 1972 | ### /Support code for Test_TestCase |
| 1973 | |
| 1974 | class Test_TestCase(TestCase, TestEquality, TestHashing): |
| 1975 | |
| 1976 | ### Set up attributes used by inherited tests |
| 1977 | ################################################################ |
| 1978 | |
| 1979 | # Used by TestHashing.test_hash and TestEquality.test_eq |
| 1980 | eq_pairs = [(Foo('test1'), Foo('test1'))] |
| 1981 | |
| 1982 | # Used by TestEquality.test_ne |
| 1983 | ne_pairs = [(Foo('test1'), Foo('runTest')) |
| 1984 | ,(Foo('test1'), Bar('test1')) |
| 1985 | ,(Foo('test1'), Bar('test2'))] |
| 1986 | |
| 1987 | ################################################################ |
| 1988 | ### /Set up attributes used by inherited tests |
| 1989 | |
| 1990 | |
| 1991 | # "class TestCase([methodName])" |
| 1992 | # ... |
| 1993 | # "Each instance of TestCase will run a single test method: the |
| 1994 | # method named methodName." |
| 1995 | # ... |
| 1996 | # "methodName defaults to "runTest"." |
| 1997 | # |
| 1998 | # Make sure it really is optional, and that it defaults to the proper |
| 1999 | # thing. |
| 2000 | def test_init__no_test_name(self): |
| 2001 | class Test(unittest.TestCase): |
| 2002 | def runTest(self): raise MyException() |
| 2003 | def test(self): pass |
| 2004 | |
| 2005 | self.assertEqual(Test().id()[-13:], '.Test.runTest') |
| 2006 | |
| 2007 | # "class TestCase([methodName])" |
| 2008 | # ... |
| 2009 | # "Each instance of TestCase will run a single test method: the |
| 2010 | # method named methodName." |
| 2011 | def test_init__test_name__valid(self): |
| 2012 | class Test(unittest.TestCase): |
| 2013 | def runTest(self): raise MyException() |
| 2014 | def test(self): pass |
| 2015 | |
| 2016 | self.assertEqual(Test('test').id()[-10:], '.Test.test') |
| 2017 | |
| 2018 | # "class TestCase([methodName])" |
| 2019 | # ... |
| 2020 | # "Each instance of TestCase will run a single test method: the |
| 2021 | # method named methodName." |
| 2022 | def test_init__test_name__invalid(self): |
| 2023 | class Test(unittest.TestCase): |
| 2024 | def runTest(self): raise MyException() |
| 2025 | def test(self): pass |
| 2026 | |
| 2027 | try: |
| 2028 | Test('testfoo') |
| 2029 | except ValueError: |
| 2030 | pass |
| 2031 | else: |
| 2032 | self.fail("Failed to raise ValueError") |
| 2033 | |
| 2034 | # "Return the number of tests represented by the this test object. For |
| 2035 | # TestCase instances, this will always be 1" |
| 2036 | def test_countTestCases(self): |
| 2037 | class Foo(unittest.TestCase): |
| 2038 | def test(self): pass |
| 2039 | |
| 2040 | self.assertEqual(Foo('test').countTestCases(), 1) |
| 2041 | |
| 2042 | # "Return the default type of test result object to be used to run this |
| 2043 | # test. For TestCase instances, this will always be |
| 2044 | # unittest.TestResult; subclasses of TestCase should |
| 2045 | # override this as necessary." |
| 2046 | def test_defaultTestResult(self): |
| 2047 | class Foo(unittest.TestCase): |
| 2048 | def runTest(self): |
| 2049 | pass |
| 2050 | |
| 2051 | result = Foo().defaultTestResult() |
| 2052 | self.assertEqual(type(result), unittest.TestResult) |
| 2053 | |
| 2054 | # "When a setUp() method is defined, the test runner will run that method |
| 2055 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 2056 | # test runner will invoke that method after each test. In the example, |
| 2057 | # setUp() was used to create a fresh sequence for each test." |
| 2058 | # |
| 2059 | # Make sure the proper call order is maintained, even if setUp() raises |
| 2060 | # an exception. |
| 2061 | def test_run_call_order__error_in_setUp(self): |
| 2062 | events = [] |
| 2063 | result = LoggingResult(events) |
| 2064 | |
| 2065 | class Foo(unittest.TestCase): |
| 2066 | def setUp(self): |
| 2067 | events.append('setUp') |
| 2068 | raise RuntimeError('raised by Foo.setUp') |
| 2069 | |
| 2070 | def test(self): |
| 2071 | events.append('test') |
| 2072 | |
| 2073 | def tearDown(self): |
| 2074 | events.append('tearDown') |
| 2075 | |
| 2076 | Foo('test').run(result) |
| 2077 | expected = ['startTest', 'setUp', 'addError', 'stopTest'] |
| 2078 | self.assertEqual(events, expected) |
| 2079 | |
| 2080 | # "When a setUp() method is defined, the test runner will run that method |
| 2081 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 2082 | # test runner will invoke that method after each test. In the example, |
| 2083 | # setUp() was used to create a fresh sequence for each test." |
| 2084 | # |
| 2085 | # Make sure the proper call order is maintained, even if the test raises |
| 2086 | # an error (as opposed to a failure). |
| 2087 | def test_run_call_order__error_in_test(self): |
| 2088 | events = [] |
| 2089 | result = LoggingResult(events) |
| 2090 | |
| 2091 | class Foo(unittest.TestCase): |
| 2092 | def setUp(self): |
| 2093 | events.append('setUp') |
| 2094 | |
| 2095 | def test(self): |
| 2096 | events.append('test') |
| 2097 | raise RuntimeError('raised by Foo.test') |
| 2098 | |
| 2099 | def tearDown(self): |
| 2100 | events.append('tearDown') |
| 2101 | |
| 2102 | expected = ['startTest', 'setUp', 'test', 'addError', 'tearDown', |
| 2103 | 'stopTest'] |
| 2104 | Foo('test').run(result) |
| 2105 | self.assertEqual(events, expected) |
| 2106 | |
| 2107 | # "When a setUp() method is defined, the test runner will run that method |
| 2108 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 2109 | # test runner will invoke that method after each test. In the example, |
| 2110 | # setUp() was used to create a fresh sequence for each test." |
| 2111 | # |
| 2112 | # Make sure the proper call order is maintained, even if the test signals |
| 2113 | # a failure (as opposed to an error). |
| 2114 | def test_run_call_order__failure_in_test(self): |
| 2115 | events = [] |
| 2116 | result = LoggingResult(events) |
| 2117 | |
| 2118 | class Foo(unittest.TestCase): |
| 2119 | def setUp(self): |
| 2120 | events.append('setUp') |
| 2121 | |
| 2122 | def test(self): |
| 2123 | events.append('test') |
| 2124 | self.fail('raised by Foo.test') |
| 2125 | |
| 2126 | def tearDown(self): |
| 2127 | events.append('tearDown') |
| 2128 | |
| 2129 | expected = ['startTest', 'setUp', 'test', 'addFailure', 'tearDown', |
| 2130 | 'stopTest'] |
| 2131 | Foo('test').run(result) |
| 2132 | self.assertEqual(events, expected) |
| 2133 | |
| 2134 | # "When a setUp() method is defined, the test runner will run that method |
| 2135 | # prior to each test. Likewise, if a tearDown() method is defined, the |
| 2136 | # test runner will invoke that method after each test. In the example, |
| 2137 | # setUp() was used to create a fresh sequence for each test." |
| 2138 | # |
| 2139 | # Make sure the proper call order is maintained, even if tearDown() raises |
| 2140 | # an exception. |
| 2141 | def test_run_call_order__error_in_tearDown(self): |
| 2142 | events = [] |
| 2143 | result = LoggingResult(events) |
| 2144 | |
| 2145 | class Foo(unittest.TestCase): |
| 2146 | def setUp(self): |
| 2147 | events.append('setUp') |
| 2148 | |
| 2149 | def test(self): |
| 2150 | events.append('test') |
| 2151 | |
| 2152 | def tearDown(self): |
| 2153 | events.append('tearDown') |
| 2154 | raise RuntimeError('raised by Foo.tearDown') |
| 2155 | |
| 2156 | Foo('test').run(result) |
| 2157 | expected = ['startTest', 'setUp', 'test', 'tearDown', 'addError', |
| 2158 | 'stopTest'] |
| 2159 | self.assertEqual(events, expected) |
| 2160 | |
| 2161 | # "This class attribute gives the exception raised by the test() method. |
| 2162 | # If a test framework needs to use a specialized exception, possibly to |
| 2163 | # carry additional information, it must subclass this exception in |
| 2164 | # order to ``play fair'' with the framework. The initial value of this |
| 2165 | # attribute is AssertionError" |
| 2166 | def test_failureException__default(self): |
| 2167 | class Foo(unittest.TestCase): |
| 2168 | def test(self): |
| 2169 | pass |
| 2170 | |
| 2171 | self.failUnless(Foo('test').failureException is AssertionError) |
| 2172 | |
| 2173 | # "This class attribute gives the exception raised by the test() method. |
| 2174 | # If a test framework needs to use a specialized exception, possibly to |
| 2175 | # carry additional information, it must subclass this exception in |
| 2176 | # order to ``play fair'' with the framework." |
| 2177 | # |
| 2178 | # Make sure TestCase.run() respects the designated failureException |
| 2179 | def test_failureException__subclassing__explicit_raise(self): |
| 2180 | events = [] |
| 2181 | result = LoggingResult(events) |
| 2182 | |
| 2183 | class Foo(unittest.TestCase): |
| 2184 | def test(self): |
| 2185 | raise RuntimeError() |
| 2186 | |
| 2187 | failureException = RuntimeError |
| 2188 | |
| 2189 | self.failUnless(Foo('test').failureException is RuntimeError) |
| 2190 | |
| 2191 | |
| 2192 | Foo('test').run(result) |
| 2193 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 2194 | self.assertEqual(events, expected) |
| 2195 | |
| 2196 | # "This class attribute gives the exception raised by the test() method. |
| 2197 | # If a test framework needs to use a specialized exception, possibly to |
| 2198 | # carry additional information, it must subclass this exception in |
| 2199 | # order to ``play fair'' with the framework." |
| 2200 | # |
| 2201 | # Make sure TestCase.run() respects the designated failureException |
| 2202 | def test_failureException__subclassing__implicit_raise(self): |
| 2203 | events = [] |
| 2204 | result = LoggingResult(events) |
| 2205 | |
| 2206 | class Foo(unittest.TestCase): |
| 2207 | def test(self): |
| 2208 | self.fail("foo") |
| 2209 | |
| 2210 | failureException = RuntimeError |
| 2211 | |
| 2212 | self.failUnless(Foo('test').failureException is RuntimeError) |
| 2213 | |
| 2214 | |
| 2215 | Foo('test').run(result) |
| 2216 | expected = ['startTest', 'addFailure', 'stopTest'] |
| 2217 | self.assertEqual(events, expected) |
| 2218 | |
| 2219 | # "The default implementation does nothing." |
| 2220 | def test_setUp(self): |
| 2221 | class Foo(unittest.TestCase): |
| 2222 | def runTest(self): |
| 2223 | pass |
| 2224 | |
| 2225 | # ... and nothing should happen |
| 2226 | Foo().setUp() |
| 2227 | |
| 2228 | # "The default implementation does nothing." |
| 2229 | def test_tearDown(self): |
| 2230 | class Foo(unittest.TestCase): |
| 2231 | def runTest(self): |
| 2232 | pass |
| 2233 | |
| 2234 | # ... and nothing should happen |
| 2235 | Foo().tearDown() |
| 2236 | |
| 2237 | # "Return a string identifying the specific test case." |
| 2238 | # |
| 2239 | # Because of the vague nature of the docs, I'm not going to lock this |
| 2240 | # test down too much. Really all that can be asserted is that the id() |
| 2241 | # will be a string (either 8-byte or unicode -- again, because the docs |
| 2242 | # just say "string") |
| 2243 | def test_id(self): |
| 2244 | class Foo(unittest.TestCase): |
| 2245 | def runTest(self): |
| 2246 | pass |
| 2247 | |
Guido van Rossum | 3172c5d | 2007-10-16 18:12:55 +0000 | [diff] [blame] | 2248 | self.failUnless(isinstance(Foo().id(), str)) |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2249 | |
| 2250 | # "Returns a one-line description of the test, or None if no description |
| 2251 | # has been provided. The default implementation of this method returns |
| 2252 | # the first line of the test method's docstring, if available, or None." |
| 2253 | def test_shortDescription__no_docstring(self): |
| 2254 | class Foo(unittest.TestCase): |
| 2255 | def runTest(self): |
| 2256 | pass |
| 2257 | |
| 2258 | self.assertEqual(Foo().shortDescription(), None) |
| 2259 | |
| 2260 | # "Returns a one-line description of the test, or None if no description |
| 2261 | # has been provided. The default implementation of this method returns |
| 2262 | # the first line of the test method's docstring, if available, or None." |
| 2263 | def test_shortDescription__singleline_docstring(self): |
| 2264 | class Foo(unittest.TestCase): |
| 2265 | def runTest(self): |
| 2266 | "this tests foo" |
| 2267 | pass |
| 2268 | |
| 2269 | self.assertEqual(Foo().shortDescription(), "this tests foo") |
| 2270 | |
| 2271 | # "Returns a one-line description of the test, or None if no description |
| 2272 | # has been provided. The default implementation of this method returns |
| 2273 | # the first line of the test method's docstring, if available, or None." |
| 2274 | def test_shortDescription__multiline_docstring(self): |
| 2275 | class Foo(unittest.TestCase): |
| 2276 | def runTest(self): |
| 2277 | """this tests foo |
| 2278 | blah, bar and baz are also tested""" |
| 2279 | pass |
| 2280 | |
| 2281 | self.assertEqual(Foo().shortDescription(), "this tests foo") |
| 2282 | |
| 2283 | # "If result is omitted or None, a temporary result object is created |
| 2284 | # and used, but is not made available to the caller" |
| 2285 | def test_run__uses_defaultTestResult(self): |
| 2286 | events = [] |
| 2287 | |
| 2288 | class Foo(unittest.TestCase): |
| 2289 | def test(self): |
| 2290 | events.append('test') |
| 2291 | |
| 2292 | def defaultTestResult(self): |
| 2293 | return LoggingResult(events) |
| 2294 | |
| 2295 | # Make run() find a result object on its own |
| 2296 | Foo('test').run() |
| 2297 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2298 | expected = ['startTest', 'test', 'addSuccess', 'stopTest'] |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2299 | self.assertEqual(events, expected) |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2300 | |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2301 | |
| 2302 | class Test_TestSkipping(TestCase): |
| 2303 | |
| 2304 | def test_skipping(self): |
| 2305 | class Foo(unittest.TestCase): |
| 2306 | def test_skip_me(self): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 2307 | self.skipTest("skip") |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2308 | events = [] |
| 2309 | result = LoggingResult(events) |
| 2310 | test = Foo("test_skip_me") |
| 2311 | test.run(result) |
| 2312 | self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) |
| 2313 | self.assertEqual(result.skipped, [(test, "skip")]) |
| 2314 | |
| 2315 | # Try letting setUp skip the test now. |
| 2316 | class Foo(unittest.TestCase): |
| 2317 | def setUp(self): |
Benjamin Peterson | e549ead | 2009-03-28 21:42:05 +0000 | [diff] [blame] | 2318 | self.skipTest("testing") |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2319 | def test_nothing(self): pass |
| 2320 | events = [] |
| 2321 | result = LoggingResult(events) |
| 2322 | test = Foo("test_nothing") |
| 2323 | test.run(result) |
| 2324 | self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) |
| 2325 | self.assertEqual(result.skipped, [(test, "testing")]) |
| 2326 | self.assertEqual(result.testsRun, 1) |
| 2327 | |
| 2328 | def test_skipping_decorators(self): |
| 2329 | op_table = ((unittest.skipUnless, False, True), |
| 2330 | (unittest.skipIf, True, False)) |
| 2331 | for deco, do_skip, dont_skip in op_table: |
| 2332 | class Foo(unittest.TestCase): |
| 2333 | @deco(do_skip, "testing") |
| 2334 | def test_skip(self): pass |
| 2335 | |
| 2336 | @deco(dont_skip, "testing") |
| 2337 | def test_dont_skip(self): pass |
| 2338 | test_do_skip = Foo("test_skip") |
| 2339 | test_dont_skip = Foo("test_dont_skip") |
| 2340 | suite = unittest.ClassTestSuite([test_do_skip, test_dont_skip], Foo) |
| 2341 | events = [] |
| 2342 | result = LoggingResult(events) |
| 2343 | suite.run(result) |
| 2344 | self.assertEqual(len(result.skipped), 1) |
| 2345 | expected = ['startTest', 'addSkip', 'stopTest', |
| 2346 | 'startTest', 'addSuccess', 'stopTest'] |
| 2347 | self.assertEqual(events, expected) |
| 2348 | self.assertEqual(result.testsRun, 2) |
| 2349 | self.assertEqual(result.skipped, [(test_do_skip, "testing")]) |
| 2350 | self.assertTrue(result.wasSuccessful()) |
| 2351 | |
| 2352 | def test_skip_class(self): |
| 2353 | @unittest.skip("testing") |
| 2354 | class Foo(unittest.TestCase): |
| 2355 | def test_1(self): |
| 2356 | record.append(1) |
| 2357 | record = [] |
| 2358 | result = unittest.TestResult() |
| 2359 | suite = unittest.ClassTestSuite([Foo("test_1")], Foo) |
| 2360 | suite.run(result) |
| 2361 | self.assertEqual(result.skipped, [(suite, "testing")]) |
| 2362 | self.assertEqual(record, []) |
| 2363 | |
| 2364 | def test_expected_failure(self): |
| 2365 | class Foo(unittest.TestCase): |
| 2366 | @unittest.expectedFailure |
| 2367 | def test_die(self): |
| 2368 | self.fail("help me!") |
| 2369 | events = [] |
| 2370 | result = LoggingResult(events) |
| 2371 | test = Foo("test_die") |
| 2372 | test.run(result) |
| 2373 | self.assertEqual(events, |
| 2374 | ['startTest', 'addExpectedFailure', 'stopTest']) |
Benjamin Peterson | 52baa29 | 2009-03-24 00:56:30 +0000 | [diff] [blame] | 2375 | self.assertEqual(result.expectedFailures[0][0], test) |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2376 | self.assertTrue(result.wasSuccessful()) |
| 2377 | |
| 2378 | def test_unexpected_success(self): |
| 2379 | class Foo(unittest.TestCase): |
| 2380 | @unittest.expectedFailure |
| 2381 | def test_die(self): |
| 2382 | pass |
| 2383 | events = [] |
| 2384 | result = LoggingResult(events) |
| 2385 | test = Foo("test_die") |
| 2386 | test.run(result) |
| 2387 | self.assertEqual(events, |
| 2388 | ['startTest', 'addUnexpectedSuccess', 'stopTest']) |
| 2389 | self.assertFalse(result.failures) |
Benjamin Peterson | 52baa29 | 2009-03-24 00:56:30 +0000 | [diff] [blame] | 2390 | self.assertEqual(result.unexpectedSuccesses, [test]) |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2391 | self.assertTrue(result.wasSuccessful()) |
| 2392 | |
| 2393 | |
| 2394 | |
Jeffrey Yasskin | aaaef11 | 2007-09-07 15:00:39 +0000 | [diff] [blame] | 2395 | class Test_Assertions(TestCase): |
| 2396 | def test_AlmostEqual(self): |
| 2397 | self.failUnlessAlmostEqual(1.00000001, 1.0) |
| 2398 | self.failIfAlmostEqual(1.0000001, 1.0) |
| 2399 | self.assertRaises(AssertionError, |
| 2400 | self.failUnlessAlmostEqual, 1.0000001, 1.0) |
| 2401 | self.assertRaises(AssertionError, |
| 2402 | self.failIfAlmostEqual, 1.00000001, 1.0) |
| 2403 | |
| 2404 | self.failUnlessAlmostEqual(1.1, 1.0, places=0) |
| 2405 | self.assertRaises(AssertionError, |
| 2406 | self.failUnlessAlmostEqual, 1.1, 1.0, places=1) |
| 2407 | |
| 2408 | self.failUnlessAlmostEqual(0, .1+.1j, places=0) |
| 2409 | self.failIfAlmostEqual(0, .1+.1j, places=1) |
| 2410 | self.assertRaises(AssertionError, |
| 2411 | self.failUnlessAlmostEqual, 0, .1+.1j, places=1) |
| 2412 | self.assertRaises(AssertionError, |
| 2413 | self.failIfAlmostEqual, 0, .1+.1j, places=0) |
| 2414 | |
Antoine Pitrou | 5acd41e | 2008-12-28 14:29:00 +0000 | [diff] [blame] | 2415 | def test_assertRaises(self): |
| 2416 | def _raise(e): |
| 2417 | raise e |
| 2418 | self.assertRaises(KeyError, _raise, KeyError) |
| 2419 | self.assertRaises(KeyError, _raise, KeyError("key")) |
| 2420 | try: |
| 2421 | self.assertRaises(KeyError, lambda: None) |
| 2422 | except AssertionError as e: |
| 2423 | self.assert_("KeyError not raised" in str(e), str(e)) |
| 2424 | else: |
| 2425 | self.fail("assertRaises() didn't fail") |
| 2426 | try: |
| 2427 | self.assertRaises(KeyError, _raise, ValueError) |
| 2428 | except ValueError: |
| 2429 | pass |
| 2430 | else: |
| 2431 | self.fail("assertRaises() didn't let exception pass through") |
| 2432 | with self.assertRaises(KeyError): |
| 2433 | raise KeyError |
| 2434 | with self.assertRaises(KeyError): |
| 2435 | raise KeyError("key") |
| 2436 | try: |
| 2437 | with self.assertRaises(KeyError): |
| 2438 | pass |
| 2439 | except AssertionError as e: |
| 2440 | self.assert_("KeyError not raised" in str(e), str(e)) |
| 2441 | else: |
| 2442 | self.fail("assertRaises() didn't fail") |
| 2443 | try: |
| 2444 | with self.assertRaises(KeyError): |
| 2445 | raise ValueError |
| 2446 | except ValueError: |
| 2447 | pass |
| 2448 | else: |
| 2449 | self.fail("assertRaises() didn't let exception pass through") |
| 2450 | |
| 2451 | |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2452 | ###################################################################### |
| 2453 | ## Main |
| 2454 | ###################################################################### |
| 2455 | |
| 2456 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2457 | support.run_unittest(Test_TestCase, Test_TestLoader, |
Jeffrey Yasskin | aaaef11 | 2007-09-07 15:00:39 +0000 | [diff] [blame] | 2458 | Test_TestSuite, Test_TestResult, Test_FunctionTestCase, |
Benjamin Peterson | 5254c04 | 2009-03-23 22:25:03 +0000 | [diff] [blame] | 2459 | Test_TestSkipping, Test_Assertions) |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2460 | |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 2461 | if __name__ == "__main__": |
Jim Fulton | fafd874 | 2004-08-28 15:22:12 +0000 | [diff] [blame] | 2462 | test_main() |