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