python (3.11.7)
1 import functools
2 import sys
3 import types
4 import warnings
5
6 import unittest
7
8 # Decorator used in the deprecation tests to reset the warning registry for
9 # test isolation and reproducibility.
10 def warningregistry(func):
11 def wrapper(*args, **kws):
12 missing = []
13 saved = getattr(warnings, '__warningregistry__', missing).copy()
14 try:
15 return func(*args, **kws)
16 finally:
17 if saved is missing:
18 try:
19 del warnings.__warningregistry__
20 except AttributeError:
21 pass
22 else:
23 warnings.__warningregistry__ = saved
24 return wrapper
25
26
27 class ESC[4;38;5;81mTest_TestLoader(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
28
29 ### Basic object tests
30 ################################################################
31
32 def test___init__(self):
33 loader = unittest.TestLoader()
34 self.assertEqual([], loader.errors)
35
36 ### Tests for TestLoader.loadTestsFromTestCase
37 ################################################################
38
39 # "Return a suite of all test cases contained in the TestCase-derived
40 # class testCaseClass"
41 def test_loadTestsFromTestCase(self):
42 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
43 def test_1(self): pass
44 def test_2(self): pass
45 def foo_bar(self): pass
46
47 tests = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
48
49 loader = unittest.TestLoader()
50 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
51
52 # "Return a suite of all test cases contained in the TestCase-derived
53 # class testCaseClass"
54 #
55 # Make sure it does the right thing even if no tests were found
56 def test_loadTestsFromTestCase__no_matches(self):
57 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
58 def foo_bar(self): pass
59
60 empty_suite = unittest.TestSuite()
61
62 loader = unittest.TestLoader()
63 self.assertEqual(loader.loadTestsFromTestCase(Foo), empty_suite)
64
65 # "Return a suite of all test cases contained in the TestCase-derived
66 # class testCaseClass"
67 #
68 # What happens if loadTestsFromTestCase() is given an object
69 # that isn't a subclass of TestCase? Specifically, what happens
70 # if testCaseClass is a subclass of TestSuite?
71 #
72 # This is checked for specifically in the code, so we better add a
73 # test for it.
74 def test_loadTestsFromTestCase__TestSuite_subclass(self):
75 class ESC[4;38;5;81mNotATestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
76 pass
77
78 loader = unittest.TestLoader()
79 try:
80 loader.loadTestsFromTestCase(NotATestCase)
81 except TypeError:
82 pass
83 else:
84 self.fail('Should raise TypeError')
85
86 # "Return a suite of all test cases contained in the TestCase-derived
87 # class testCaseClass"
88 #
89 # Make sure loadTestsFromTestCase() picks up the default test method
90 # name (as specified by TestCase), even though the method name does
91 # not match the default TestLoader.testMethodPrefix string
92 def test_loadTestsFromTestCase__default_method_name(self):
93 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
94 def runTest(self):
95 pass
96
97 loader = unittest.TestLoader()
98 # This has to be false for the test to succeed
99 self.assertFalse('runTest'.startswith(loader.testMethodPrefix))
100
101 suite = loader.loadTestsFromTestCase(Foo)
102 self.assertIsInstance(suite, loader.suiteClass)
103 self.assertEqual(list(suite), [Foo('runTest')])
104
105 # "Do not load any tests from `TestCase` class itself."
106 def test_loadTestsFromTestCase__from_TestCase(self):
107 loader = unittest.TestLoader()
108
109 suite = loader.loadTestsFromTestCase(unittest.TestCase)
110 self.assertIsInstance(suite, loader.suiteClass)
111 self.assertEqual(list(suite), [])
112
113 # "Do not load any tests from `FunctionTestCase` class."
114 def test_loadTestsFromTestCase__from_FunctionTestCase(self):
115 loader = unittest.TestLoader()
116
117 suite = loader.loadTestsFromTestCase(unittest.FunctionTestCase)
118 self.assertIsInstance(suite, loader.suiteClass)
119 self.assertEqual(list(suite), [])
120
121 ################################################################
122 ### /Tests for TestLoader.loadTestsFromTestCase
123
124 ### Tests for TestLoader.loadTestsFromModule
125 ################################################################
126
127 # "This method searches `module` for classes derived from TestCase"
128 def test_loadTestsFromModule__TestCase_subclass(self):
129 m = types.ModuleType('m')
130 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
131 def test(self):
132 pass
133 m.testcase_1 = MyTestCase
134
135 loader = unittest.TestLoader()
136 suite = loader.loadTestsFromModule(m)
137 self.assertIsInstance(suite, loader.suiteClass)
138
139 expected = [loader.suiteClass([MyTestCase('test')])]
140 self.assertEqual(list(suite), expected)
141
142 # "This test ensures that internal `TestCase` subclasses are not loaded"
143 def test_loadTestsFromModule__TestCase_subclass_internals(self):
144 # See https://github.com/python/cpython/issues/84867
145 m = types.ModuleType('m')
146 # Simulate imported names:
147 m.TestCase = unittest.TestCase
148 m.FunctionTestCase = unittest.FunctionTestCase
149
150 loader = unittest.TestLoader()
151 suite = loader.loadTestsFromModule(m)
152 self.assertIsInstance(suite, loader.suiteClass)
153 self.assertEqual(list(suite), [])
154
155 # "This method searches `module` for classes derived from TestCase"
156 #
157 # What happens if no tests are found (no TestCase instances)?
158 def test_loadTestsFromModule__no_TestCase_instances(self):
159 m = types.ModuleType('m')
160
161 loader = unittest.TestLoader()
162 suite = loader.loadTestsFromModule(m)
163 self.assertIsInstance(suite, loader.suiteClass)
164 self.assertEqual(list(suite), [])
165
166 # "This method searches `module` for classes derived from TestCase"
167 #
168 # What happens if no tests are found (TestCases instances, but no tests)?
169 def test_loadTestsFromModule__no_TestCase_tests(self):
170 m = types.ModuleType('m')
171 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
172 pass
173 m.testcase_1 = MyTestCase
174
175 loader = unittest.TestLoader()
176 suite = loader.loadTestsFromModule(m)
177 self.assertIsInstance(suite, loader.suiteClass)
178
179 self.assertEqual(list(suite), [loader.suiteClass()])
180
181 # "This method searches `module` for classes derived from TestCase"s
182 #
183 # What happens if loadTestsFromModule() is given something other
184 # than a module?
185 #
186 # XXX Currently, it succeeds anyway. This flexibility
187 # should either be documented or loadTestsFromModule() should
188 # raise a TypeError
189 #
190 # XXX Certain people are using this behaviour. We'll add a test for it
191 def test_loadTestsFromModule__not_a_module(self):
192 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
193 def test(self):
194 pass
195
196 class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
197 test_2 = MyTestCase
198
199 loader = unittest.TestLoader()
200 suite = loader.loadTestsFromModule(NotAModule)
201
202 reference = [unittest.TestSuite([MyTestCase('test')])]
203 self.assertEqual(list(suite), reference)
204
205
206 # Check that loadTestsFromModule honors (or not) a module
207 # with a load_tests function.
208 @warningregistry
209 def test_loadTestsFromModule__load_tests(self):
210 m = types.ModuleType('m')
211 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
212 def test(self):
213 pass
214 m.testcase_1 = MyTestCase
215
216 load_tests_args = []
217 def load_tests(loader, tests, pattern):
218 self.assertIsInstance(tests, unittest.TestSuite)
219 load_tests_args.extend((loader, tests, pattern))
220 return tests
221 m.load_tests = load_tests
222
223 loader = unittest.TestLoader()
224 suite = loader.loadTestsFromModule(m)
225 self.assertIsInstance(suite, unittest.TestSuite)
226 self.assertEqual(load_tests_args, [loader, suite, None])
227 # With Python 3.5, the undocumented and unofficial use_load_tests is
228 # ignored (and deprecated).
229 load_tests_args = []
230 with warnings.catch_warnings(record=False):
231 warnings.simplefilter('ignore')
232 suite = loader.loadTestsFromModule(m, use_load_tests=False)
233 self.assertEqual(load_tests_args, [loader, suite, None])
234
235 @warningregistry
236 def test_loadTestsFromModule__use_load_tests_deprecated_positional(self):
237 m = types.ModuleType('m')
238 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
239 def test(self):
240 pass
241 m.testcase_1 = MyTestCase
242
243 load_tests_args = []
244 def load_tests(loader, tests, pattern):
245 self.assertIsInstance(tests, unittest.TestSuite)
246 load_tests_args.extend((loader, tests, pattern))
247 return tests
248 m.load_tests = load_tests
249 # The method still works.
250 loader = unittest.TestLoader()
251 # use_load_tests=True as a positional argument.
252 with warnings.catch_warnings(record=True) as w:
253 warnings.simplefilter('always')
254 suite = loader.loadTestsFromModule(m, False)
255 self.assertIsInstance(suite, unittest.TestSuite)
256 # load_tests was still called because use_load_tests is deprecated
257 # and ignored.
258 self.assertEqual(load_tests_args, [loader, suite, None])
259 # We got a warning.
260 self.assertIs(w[-1].category, DeprecationWarning)
261 self.assertEqual(str(w[-1].message),
262 'use_load_tests is deprecated and ignored')
263
264 @warningregistry
265 def test_loadTestsFromModule__use_load_tests_deprecated_keyword(self):
266 m = types.ModuleType('m')
267 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
268 def test(self):
269 pass
270 m.testcase_1 = MyTestCase
271
272 load_tests_args = []
273 def load_tests(loader, tests, pattern):
274 self.assertIsInstance(tests, unittest.TestSuite)
275 load_tests_args.extend((loader, tests, pattern))
276 return tests
277 m.load_tests = load_tests
278 # The method still works.
279 loader = unittest.TestLoader()
280 with warnings.catch_warnings(record=True) as w:
281 warnings.simplefilter('always')
282 suite = loader.loadTestsFromModule(m, use_load_tests=False)
283 self.assertIsInstance(suite, unittest.TestSuite)
284 # load_tests was still called because use_load_tests is deprecated
285 # and ignored.
286 self.assertEqual(load_tests_args, [loader, suite, None])
287 # We got a warning.
288 self.assertIs(w[-1].category, DeprecationWarning)
289 self.assertEqual(str(w[-1].message),
290 'use_load_tests is deprecated and ignored')
291
292 @warningregistry
293 def test_loadTestsFromModule__too_many_positional_args(self):
294 m = types.ModuleType('m')
295 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
296 def test(self):
297 pass
298 m.testcase_1 = MyTestCase
299
300 load_tests_args = []
301 def load_tests(loader, tests, pattern):
302 self.assertIsInstance(tests, unittest.TestSuite)
303 load_tests_args.extend((loader, tests, pattern))
304 return tests
305 m.load_tests = load_tests
306 loader = unittest.TestLoader()
307 with self.assertRaises(TypeError) as cm, \
308 warnings.catch_warnings(record=True) as w:
309 warnings.simplefilter('always')
310 loader.loadTestsFromModule(m, False, 'testme.*')
311 # We still got the deprecation warning.
312 self.assertIs(w[-1].category, DeprecationWarning)
313 self.assertEqual(str(w[-1].message),
314 'use_load_tests is deprecated and ignored')
315 # We also got a TypeError for too many positional arguments.
316 self.assertEqual(type(cm.exception), TypeError)
317 self.assertEqual(
318 str(cm.exception),
319 'loadTestsFromModule() takes 1 positional argument but 3 were given')
320
321 @warningregistry
322 def test_loadTestsFromModule__use_load_tests_other_bad_keyword(self):
323 m = types.ModuleType('m')
324 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
325 def test(self):
326 pass
327 m.testcase_1 = MyTestCase
328
329 load_tests_args = []
330 def load_tests(loader, tests, pattern):
331 self.assertIsInstance(tests, unittest.TestSuite)
332 load_tests_args.extend((loader, tests, pattern))
333 return tests
334 m.load_tests = load_tests
335 loader = unittest.TestLoader()
336 with warnings.catch_warnings():
337 warnings.simplefilter('ignore')
338 with self.assertRaises(TypeError) as cm:
339 loader.loadTestsFromModule(
340 m, use_load_tests=False, very_bad=True, worse=False)
341 self.assertEqual(type(cm.exception), TypeError)
342 # The error message names the first bad argument alphabetically,
343 # however use_load_tests (which sorts first) is ignored.
344 self.assertEqual(
345 str(cm.exception),
346 "loadTestsFromModule() got an unexpected keyword argument 'very_bad'")
347
348 def test_loadTestsFromModule__pattern(self):
349 m = types.ModuleType('m')
350 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
351 def test(self):
352 pass
353 m.testcase_1 = MyTestCase
354
355 load_tests_args = []
356 def load_tests(loader, tests, pattern):
357 self.assertIsInstance(tests, unittest.TestSuite)
358 load_tests_args.extend((loader, tests, pattern))
359 return tests
360 m.load_tests = load_tests
361
362 loader = unittest.TestLoader()
363 suite = loader.loadTestsFromModule(m, pattern='testme.*')
364 self.assertIsInstance(suite, unittest.TestSuite)
365 self.assertEqual(load_tests_args, [loader, suite, 'testme.*'])
366
367 def test_loadTestsFromModule__faulty_load_tests(self):
368 m = types.ModuleType('m')
369
370 def load_tests(loader, tests, pattern):
371 raise TypeError('some failure')
372 m.load_tests = load_tests
373
374 loader = unittest.TestLoader()
375 suite = loader.loadTestsFromModule(m)
376 self.assertIsInstance(suite, unittest.TestSuite)
377 self.assertEqual(suite.countTestCases(), 1)
378 # Errors loading the suite are also captured for introspection.
379 self.assertNotEqual([], loader.errors)
380 self.assertEqual(1, len(loader.errors))
381 error = loader.errors[0]
382 self.assertTrue(
383 'Failed to call load_tests:' in error,
384 'missing error string in %r' % error)
385 test = list(suite)[0]
386
387 self.assertRaisesRegex(TypeError, "some failure", test.m)
388
389 ################################################################
390 ### /Tests for TestLoader.loadTestsFromModule()
391
392 ### Tests for TestLoader.loadTestsFromName()
393 ################################################################
394
395 # "The specifier name is a ``dotted name'' that may resolve either to
396 # a module, a test case class, a TestSuite instance, a test method
397 # within a test case class, or a callable object which returns a
398 # TestCase or TestSuite instance."
399 #
400 # Is ValueError raised in response to an empty name?
401 def test_loadTestsFromName__empty_name(self):
402 loader = unittest.TestLoader()
403
404 try:
405 loader.loadTestsFromName('')
406 except ValueError as e:
407 self.assertEqual(str(e), "Empty module name")
408 else:
409 self.fail("TestLoader.loadTestsFromName failed to raise ValueError")
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 #
416 # What happens when the name contains invalid characters?
417 def test_loadTestsFromName__malformed_name(self):
418 loader = unittest.TestLoader()
419
420 suite = loader.loadTestsFromName('abc () //')
421 error, test = self.check_deferred_error(loader, suite)
422 expected = "Failed to import test module: abc () //"
423 expected_regex = r"Failed to import test module: abc \(\) //"
424 self.assertIn(
425 expected, error,
426 'missing error string in %r' % error)
427 self.assertRaisesRegex(
428 ImportError, expected_regex, getattr(test, 'abc () //'))
429
430 # "The specifier name is a ``dotted name'' that may resolve ... to a
431 # module"
432 #
433 # What happens when a module by that name can't be found?
434 def test_loadTestsFromName__unknown_module_name(self):
435 loader = unittest.TestLoader()
436
437 suite = loader.loadTestsFromName('sdasfasfasdf')
438 expected = "No module named 'sdasfasfasdf'"
439 error, test = self.check_deferred_error(loader, suite)
440 self.assertIn(
441 expected, error,
442 'missing error string in %r' % error)
443 self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
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 # What happens when the module is found, but the attribute isn't?
451 def test_loadTestsFromName__unknown_attr_name_on_module(self):
452 loader = unittest.TestLoader()
453
454 suite = loader.loadTestsFromName('unittest.loader.sdasfasfasdf')
455 expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
456 error, test = self.check_deferred_error(loader, suite)
457 self.assertIn(
458 expected, error,
459 'missing error string in %r' % error)
460 self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
461
462 # "The specifier name is a ``dotted name'' that may resolve either to
463 # a module, a test case class, a TestSuite instance, a test method
464 # within a test case class, or a callable object which returns a
465 # TestCase or TestSuite instance."
466 #
467 # What happens when the module is found, but the attribute isn't?
468 def test_loadTestsFromName__unknown_attr_name_on_package(self):
469 loader = unittest.TestLoader()
470
471 suite = loader.loadTestsFromName('unittest.sdasfasfasdf')
472 expected = "No module named 'unittest.sdasfasfasdf'"
473 error, test = self.check_deferred_error(loader, suite)
474 self.assertIn(
475 expected, error,
476 'missing error string in %r' % error)
477 self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
478
479 # "The specifier name is a ``dotted name'' that may resolve either to
480 # a module, a test case class, a TestSuite instance, a test method
481 # within a test case class, or a callable object which returns a
482 # TestCase or TestSuite instance."
483 #
484 # What happens when we provide the module, but the attribute can't be
485 # found?
486 def test_loadTestsFromName__relative_unknown_name(self):
487 loader = unittest.TestLoader()
488
489 suite = loader.loadTestsFromName('sdasfasfasdf', unittest)
490 expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
491 error, test = self.check_deferred_error(loader, suite)
492 self.assertIn(
493 expected, error,
494 'missing error string in %r' % error)
495 self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
496
497 # "The specifier name is a ``dotted name'' that may resolve either to
498 # a module, a test case class, a TestSuite instance, a test method
499 # within a test case class, or a callable object which returns a
500 # TestCase or TestSuite instance."
501 # ...
502 # "The method optionally resolves name relative to the given module"
503 #
504 # Does loadTestsFromName raise ValueError when passed an empty
505 # name relative to a provided module?
506 #
507 # XXX Should probably raise a ValueError instead of an AttributeError
508 def test_loadTestsFromName__relative_empty_name(self):
509 loader = unittest.TestLoader()
510
511 suite = loader.loadTestsFromName('', unittest)
512 error, test = self.check_deferred_error(loader, suite)
513 expected = "has no attribute ''"
514 self.assertIn(
515 expected, error,
516 'missing error string in %r' % error)
517 self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
518
519 # "The specifier name is a ``dotted name'' that may resolve either to
520 # a module, a test case class, a TestSuite instance, a test method
521 # within a test case class, or a callable object which returns a
522 # TestCase or TestSuite instance."
523 # ...
524 # "The method optionally resolves name relative to the given module"
525 #
526 # What happens when an impossible name is given, relative to the provided
527 # `module`?
528 def test_loadTestsFromName__relative_malformed_name(self):
529 loader = unittest.TestLoader()
530
531 # XXX Should this raise AttributeError or ValueError?
532 suite = loader.loadTestsFromName('abc () //', unittest)
533 error, test = self.check_deferred_error(loader, suite)
534 expected = "module 'unittest' has no attribute 'abc () //'"
535 expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
536 self.assertIn(
537 expected, error,
538 'missing error string in %r' % error)
539 self.assertRaisesRegex(
540 AttributeError, expected_regex, getattr(test, 'abc () //'))
541
542 # "The method optionally resolves name relative to the given module"
543 #
544 # Does loadTestsFromName raise TypeError when the `module` argument
545 # isn't a module object?
546 #
547 # XXX Accepts the not-a-module object, ignoring the object's type
548 # This should raise an exception or the method name should be changed
549 #
550 # XXX Some people are relying on this, so keep it for now
551 def test_loadTestsFromName__relative_not_a_module(self):
552 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
553 def test(self):
554 pass
555
556 class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
557 test_2 = MyTestCase
558
559 loader = unittest.TestLoader()
560 suite = loader.loadTestsFromName('test_2', NotAModule)
561
562 reference = [MyTestCase('test')]
563 self.assertEqual(list(suite), reference)
564
565 # "The specifier name is a ``dotted name'' that may resolve either to
566 # a module, a test case class, a TestSuite instance, a test method
567 # within a test case class, or a callable object which returns a
568 # TestCase or TestSuite instance."
569 #
570 # Does it raise an exception if the name resolves to an invalid
571 # object?
572 def test_loadTestsFromName__relative_bad_object(self):
573 m = types.ModuleType('m')
574 m.testcase_1 = object()
575
576 loader = unittest.TestLoader()
577 try:
578 loader.loadTestsFromName('testcase_1', m)
579 except TypeError:
580 pass
581 else:
582 self.fail("Should have raised TypeError")
583
584 # "The specifier name is a ``dotted name'' that may
585 # resolve either to ... a test case class"
586 def test_loadTestsFromName__relative_TestCase_subclass(self):
587 m = types.ModuleType('m')
588 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
589 def test(self):
590 pass
591 m.testcase_1 = MyTestCase
592
593 loader = unittest.TestLoader()
594 suite = loader.loadTestsFromName('testcase_1', m)
595 self.assertIsInstance(suite, loader.suiteClass)
596 self.assertEqual(list(suite), [MyTestCase('test')])
597
598 # "The specifier name is a ``dotted name'' that may resolve either to
599 # a module, a test case class, a TestSuite instance, a test method
600 # within a test case class, or a callable object which returns a
601 # TestCase or TestSuite instance."
602 def test_loadTestsFromName__relative_TestSuite(self):
603 m = types.ModuleType('m')
604 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
605 def test(self):
606 pass
607 m.testsuite = unittest.TestSuite([MyTestCase('test')])
608
609 loader = unittest.TestLoader()
610 suite = loader.loadTestsFromName('testsuite', m)
611 self.assertIsInstance(suite, loader.suiteClass)
612
613 self.assertEqual(list(suite), [MyTestCase('test')])
614
615 # "The specifier name is a ``dotted name'' that may resolve ... to
616 # ... a test method within a test case class"
617 def test_loadTestsFromName__relative_testmethod(self):
618 m = types.ModuleType('m')
619 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
620 def test(self):
621 pass
622 m.testcase_1 = MyTestCase
623
624 loader = unittest.TestLoader()
625 suite = loader.loadTestsFromName('testcase_1.test', m)
626 self.assertIsInstance(suite, loader.suiteClass)
627
628 self.assertEqual(list(suite), [MyTestCase('test')])
629
630 # "The specifier name is a ``dotted name'' that may resolve either to
631 # a module, a test case class, a TestSuite instance, a test method
632 # within a test case class, or a callable object which returns a
633 # TestCase or TestSuite instance."
634 #
635 # Does loadTestsFromName() raise the proper exception when trying to
636 # resolve "a test method within a test case class" that doesn't exist
637 # for the given name (relative to a provided module)?
638 def test_loadTestsFromName__relative_invalid_testmethod(self):
639 m = types.ModuleType('m')
640 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
641 def test(self):
642 pass
643 m.testcase_1 = MyTestCase
644
645 loader = unittest.TestLoader()
646 suite = loader.loadTestsFromName('testcase_1.testfoo', m)
647 expected = "type object 'MyTestCase' has no attribute 'testfoo'"
648 error, test = self.check_deferred_error(loader, suite)
649 self.assertIn(
650 expected, error,
651 'missing error string in %r' % error)
652 self.assertRaisesRegex(AttributeError, expected, test.testfoo)
653
654 # "The specifier name is a ``dotted name'' that may resolve ... to
655 # ... a callable object which returns a ... TestSuite instance"
656 def test_loadTestsFromName__callable__TestSuite(self):
657 m = types.ModuleType('m')
658 testcase_1 = unittest.FunctionTestCase(lambda: None)
659 testcase_2 = unittest.FunctionTestCase(lambda: None)
660 def return_TestSuite():
661 return unittest.TestSuite([testcase_1, testcase_2])
662 m.return_TestSuite = return_TestSuite
663
664 loader = unittest.TestLoader()
665 suite = loader.loadTestsFromName('return_TestSuite', m)
666 self.assertIsInstance(suite, loader.suiteClass)
667 self.assertEqual(list(suite), [testcase_1, testcase_2])
668
669 # "The specifier name is a ``dotted name'' that may resolve ... to
670 # ... a callable object which returns a TestCase ... instance"
671 def test_loadTestsFromName__callable__TestCase_instance(self):
672 m = types.ModuleType('m')
673 testcase_1 = unittest.FunctionTestCase(lambda: None)
674 def return_TestCase():
675 return testcase_1
676 m.return_TestCase = return_TestCase
677
678 loader = unittest.TestLoader()
679 suite = loader.loadTestsFromName('return_TestCase', m)
680 self.assertIsInstance(suite, loader.suiteClass)
681 self.assertEqual(list(suite), [testcase_1])
682
683 # "The specifier name is a ``dotted name'' that may resolve ... to
684 # ... a callable object which returns a TestCase ... instance"
685 #*****************************************************************
686 #Override the suiteClass attribute to ensure that the suiteClass
687 #attribute is used
688 def test_loadTestsFromName__callable__TestCase_instance_ProperSuiteClass(self):
689 class ESC[4;38;5;81mSubTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
690 pass
691 m = types.ModuleType('m')
692 testcase_1 = unittest.FunctionTestCase(lambda: None)
693 def return_TestCase():
694 return testcase_1
695 m.return_TestCase = return_TestCase
696
697 loader = unittest.TestLoader()
698 loader.suiteClass = SubTestSuite
699 suite = loader.loadTestsFromName('return_TestCase', m)
700 self.assertIsInstance(suite, loader.suiteClass)
701 self.assertEqual(list(suite), [testcase_1])
702
703 # "The specifier name is a ``dotted name'' that may resolve ... to
704 # ... a test method within a test case class"
705 #*****************************************************************
706 #Override the suiteClass attribute to ensure that the suiteClass
707 #attribute is used
708 def test_loadTestsFromName__relative_testmethod_ProperSuiteClass(self):
709 class ESC[4;38;5;81mSubTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
710 pass
711 m = types.ModuleType('m')
712 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
713 def test(self):
714 pass
715 m.testcase_1 = MyTestCase
716
717 loader = unittest.TestLoader()
718 loader.suiteClass=SubTestSuite
719 suite = loader.loadTestsFromName('testcase_1.test', m)
720 self.assertIsInstance(suite, loader.suiteClass)
721
722 self.assertEqual(list(suite), [MyTestCase('test')])
723
724 # "The specifier name is a ``dotted name'' that may resolve ... to
725 # ... a callable object which returns a TestCase or TestSuite instance"
726 #
727 # What happens if the callable returns something else?
728 def test_loadTestsFromName__callable__wrong_type(self):
729 m = types.ModuleType('m')
730 def return_wrong():
731 return 6
732 m.return_wrong = return_wrong
733
734 loader = unittest.TestLoader()
735 try:
736 suite = loader.loadTestsFromName('return_wrong', m)
737 except TypeError:
738 pass
739 else:
740 self.fail("TestLoader.loadTestsFromName failed to raise TypeError")
741
742 # "The specifier can refer to modules and packages which have not been
743 # imported; they will be imported as a side-effect"
744 def test_loadTestsFromName__module_not_loaded(self):
745 # We're going to try to load this module as a side-effect, so it
746 # better not be loaded before we try.
747 #
748 module_name = 'unittest.test.dummy'
749 sys.modules.pop(module_name, None)
750
751 loader = unittest.TestLoader()
752 try:
753 suite = loader.loadTestsFromName(module_name)
754
755 self.assertIsInstance(suite, loader.suiteClass)
756 self.assertEqual(list(suite), [])
757
758 # module should now be loaded, thanks to loadTestsFromName()
759 self.assertIn(module_name, sys.modules)
760 finally:
761 if module_name in sys.modules:
762 del sys.modules[module_name]
763
764 ################################################################
765 ### Tests for TestLoader.loadTestsFromName()
766
767 ### Tests for TestLoader.loadTestsFromNames()
768 ################################################################
769
770 def check_deferred_error(self, loader, suite):
771 """Helper function for checking that errors in loading are reported.
772
773 :param loader: A loader with some errors.
774 :param suite: A suite that should have a late bound error.
775 :return: The first error message from the loader and the test object
776 from the suite.
777 """
778 self.assertIsInstance(suite, unittest.TestSuite)
779 self.assertEqual(suite.countTestCases(), 1)
780 # Errors loading the suite are also captured for introspection.
781 self.assertNotEqual([], loader.errors)
782 self.assertEqual(1, len(loader.errors))
783 error = loader.errors[0]
784 test = list(suite)[0]
785 return error, test
786
787 # "Similar to loadTestsFromName(), but takes a sequence of names rather
788 # than a single name."
789 #
790 # What happens if that sequence of names is empty?
791 def test_loadTestsFromNames__empty_name_list(self):
792 loader = unittest.TestLoader()
793
794 suite = loader.loadTestsFromNames([])
795 self.assertIsInstance(suite, loader.suiteClass)
796 self.assertEqual(list(suite), [])
797
798 # "Similar to loadTestsFromName(), but takes a sequence of names rather
799 # than a single name."
800 # ...
801 # "The method optionally resolves name relative to the given module"
802 #
803 # What happens if that sequence of names is empty?
804 #
805 # XXX Should this raise a ValueError or just return an empty TestSuite?
806 def test_loadTestsFromNames__relative_empty_name_list(self):
807 loader = unittest.TestLoader()
808
809 suite = loader.loadTestsFromNames([], unittest)
810 self.assertIsInstance(suite, loader.suiteClass)
811 self.assertEqual(list(suite), [])
812
813 # "The specifier name is a ``dotted name'' that may resolve either to
814 # a module, a test case class, a TestSuite instance, a test method
815 # within a test case class, or a callable object which returns a
816 # TestCase or TestSuite instance."
817 #
818 # Is ValueError raised in response to an empty name?
819 def test_loadTestsFromNames__empty_name(self):
820 loader = unittest.TestLoader()
821
822 try:
823 loader.loadTestsFromNames([''])
824 except ValueError as e:
825 self.assertEqual(str(e), "Empty module name")
826 else:
827 self.fail("TestLoader.loadTestsFromNames failed to raise ValueError")
828
829 # "The specifier name is a ``dotted name'' that may resolve either to
830 # a module, a test case class, a TestSuite instance, a test method
831 # within a test case class, or a callable object which returns a
832 # TestCase or TestSuite instance."
833 #
834 # What happens when presented with an impossible module name?
835 def test_loadTestsFromNames__malformed_name(self):
836 loader = unittest.TestLoader()
837
838 # XXX Should this raise ValueError or ImportError?
839 suite = loader.loadTestsFromNames(['abc () //'])
840 error, test = self.check_deferred_error(loader, list(suite)[0])
841 expected = "Failed to import test module: abc () //"
842 expected_regex = r"Failed to import test module: abc \(\) //"
843 self.assertIn(
844 expected, error,
845 'missing error string in %r' % error)
846 self.assertRaisesRegex(
847 ImportError, expected_regex, getattr(test, 'abc () //'))
848
849 # "The specifier name is a ``dotted name'' that may resolve either to
850 # a module, a test case class, a TestSuite instance, a test method
851 # within a test case class, or a callable object which returns a
852 # TestCase or TestSuite instance."
853 #
854 # What happens when no module can be found for the given name?
855 def test_loadTestsFromNames__unknown_module_name(self):
856 loader = unittest.TestLoader()
857
858 suite = loader.loadTestsFromNames(['sdasfasfasdf'])
859 error, test = self.check_deferred_error(loader, list(suite)[0])
860 expected = "Failed to import test module: sdasfasfasdf"
861 self.assertIn(
862 expected, error,
863 'missing error string in %r' % error)
864 self.assertRaisesRegex(ImportError, expected, test.sdasfasfasdf)
865
866 # "The specifier name is a ``dotted name'' that may resolve either to
867 # a module, a test case class, a TestSuite instance, a test method
868 # within a test case class, or a callable object which returns a
869 # TestCase or TestSuite instance."
870 #
871 # What happens when the module can be found, but not the attribute?
872 def test_loadTestsFromNames__unknown_attr_name(self):
873 loader = unittest.TestLoader()
874
875 suite = loader.loadTestsFromNames(
876 ['unittest.loader.sdasfasfasdf', 'unittest.test.dummy'])
877 error, test = self.check_deferred_error(loader, list(suite)[0])
878 expected = "module 'unittest.loader' has no attribute 'sdasfasfasdf'"
879 self.assertIn(
880 expected, error,
881 'missing error string in %r' % error)
882 self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
883
884 # "The specifier name is a ``dotted name'' that may resolve either to
885 # a module, a test case class, a TestSuite instance, a test method
886 # within a test case class, or a callable object which returns a
887 # TestCase or TestSuite instance."
888 # ...
889 # "The method optionally resolves name relative to the given module"
890 #
891 # What happens when given an unknown attribute on a specified `module`
892 # argument?
893 def test_loadTestsFromNames__unknown_name_relative_1(self):
894 loader = unittest.TestLoader()
895
896 suite = loader.loadTestsFromNames(['sdasfasfasdf'], unittest)
897 error, test = self.check_deferred_error(loader, list(suite)[0])
898 expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
899 self.assertIn(
900 expected, error,
901 'missing error string in %r' % error)
902 self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
903
904 # "The specifier name is a ``dotted name'' that may resolve either to
905 # a module, a test case class, a TestSuite instance, a test method
906 # within a test case class, or a callable object which returns a
907 # TestCase or TestSuite instance."
908 # ...
909 # "The method optionally resolves name relative to the given module"
910 #
911 # Do unknown attributes (relative to a provided module) still raise an
912 # exception even in the presence of valid attribute names?
913 def test_loadTestsFromNames__unknown_name_relative_2(self):
914 loader = unittest.TestLoader()
915
916 suite = loader.loadTestsFromNames(['TestCase', 'sdasfasfasdf'], unittest)
917 error, test = self.check_deferred_error(loader, list(suite)[1])
918 expected = "module 'unittest' has no attribute 'sdasfasfasdf'"
919 self.assertIn(
920 expected, error,
921 'missing error string in %r' % error)
922 self.assertRaisesRegex(AttributeError, expected, test.sdasfasfasdf)
923
924 # "The specifier name is a ``dotted name'' that may resolve either to
925 # a module, a test case class, a TestSuite instance, a test method
926 # within a test case class, or a callable object which returns a
927 # TestCase or TestSuite instance."
928 # ...
929 # "The method optionally resolves name relative to the given module"
930 #
931 # What happens when faced with the empty string?
932 #
933 # XXX This currently raises AttributeError, though ValueError is probably
934 # more appropriate
935 def test_loadTestsFromNames__relative_empty_name(self):
936 loader = unittest.TestLoader()
937
938 suite = loader.loadTestsFromNames([''], unittest)
939 error, test = self.check_deferred_error(loader, list(suite)[0])
940 expected = "has no attribute ''"
941 self.assertIn(
942 expected, error,
943 'missing error string in %r' % error)
944 self.assertRaisesRegex(AttributeError, expected, getattr(test, ''))
945
946 # "The specifier name is a ``dotted name'' that may resolve either to
947 # a module, a test case class, a TestSuite instance, a test method
948 # within a test case class, or a callable object which returns a
949 # TestCase or TestSuite instance."
950 # ...
951 # "The method optionally resolves name relative to the given module"
952 #
953 # What happens when presented with an impossible attribute name?
954 def test_loadTestsFromNames__relative_malformed_name(self):
955 loader = unittest.TestLoader()
956
957 # XXX Should this raise AttributeError or ValueError?
958 suite = loader.loadTestsFromNames(['abc () //'], unittest)
959 error, test = self.check_deferred_error(loader, list(suite)[0])
960 expected = "module 'unittest' has no attribute 'abc () //'"
961 expected_regex = r"module 'unittest' has no attribute 'abc \(\) //'"
962 self.assertIn(
963 expected, error,
964 'missing error string in %r' % error)
965 self.assertRaisesRegex(
966 AttributeError, expected_regex, getattr(test, 'abc () //'))
967
968 # "The method optionally resolves name relative to the given module"
969 #
970 # Does loadTestsFromNames() make sure the provided `module` is in fact
971 # a module?
972 #
973 # XXX This validation is currently not done. This flexibility should
974 # either be documented or a TypeError should be raised.
975 def test_loadTestsFromNames__relative_not_a_module(self):
976 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
977 def test(self):
978 pass
979
980 class ESC[4;38;5;81mNotAModule(ESC[4;38;5;149mobject):
981 test_2 = MyTestCase
982
983 loader = unittest.TestLoader()
984 suite = loader.loadTestsFromNames(['test_2'], NotAModule)
985
986 reference = [unittest.TestSuite([MyTestCase('test')])]
987 self.assertEqual(list(suite), reference)
988
989 # "The specifier name is a ``dotted name'' that may resolve either to
990 # a module, a test case class, a TestSuite instance, a test method
991 # within a test case class, or a callable object which returns a
992 # TestCase or TestSuite instance."
993 #
994 # Does it raise an exception if the name resolves to an invalid
995 # object?
996 def test_loadTestsFromNames__relative_bad_object(self):
997 m = types.ModuleType('m')
998 m.testcase_1 = object()
999
1000 loader = unittest.TestLoader()
1001 try:
1002 loader.loadTestsFromNames(['testcase_1'], m)
1003 except TypeError:
1004 pass
1005 else:
1006 self.fail("Should have raised TypeError")
1007
1008 # "The specifier name is a ``dotted name'' that may resolve ... to
1009 # ... a test case class"
1010 def test_loadTestsFromNames__relative_TestCase_subclass(self):
1011 m = types.ModuleType('m')
1012 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1013 def test(self):
1014 pass
1015 m.testcase_1 = MyTestCase
1016
1017 loader = unittest.TestLoader()
1018 suite = loader.loadTestsFromNames(['testcase_1'], m)
1019 self.assertIsInstance(suite, loader.suiteClass)
1020
1021 expected = loader.suiteClass([MyTestCase('test')])
1022 self.assertEqual(list(suite), [expected])
1023
1024 # "The specifier name is a ``dotted name'' that may resolve ... to
1025 # ... a TestSuite instance"
1026 def test_loadTestsFromNames__relative_TestSuite(self):
1027 m = types.ModuleType('m')
1028 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1029 def test(self):
1030 pass
1031 m.testsuite = unittest.TestSuite([MyTestCase('test')])
1032
1033 loader = unittest.TestLoader()
1034 suite = loader.loadTestsFromNames(['testsuite'], m)
1035 self.assertIsInstance(suite, loader.suiteClass)
1036
1037 self.assertEqual(list(suite), [m.testsuite])
1038
1039 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
1040 # test method within a test case class"
1041 def test_loadTestsFromNames__relative_testmethod(self):
1042 m = types.ModuleType('m')
1043 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1044 def test(self):
1045 pass
1046 m.testcase_1 = MyTestCase
1047
1048 loader = unittest.TestLoader()
1049 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
1050 self.assertIsInstance(suite, loader.suiteClass)
1051
1052 ref_suite = unittest.TestSuite([MyTestCase('test')])
1053 self.assertEqual(list(suite), [ref_suite])
1054
1055 # #14971: Make sure the dotted name resolution works even if the actual
1056 # function doesn't have the same name as is used to find it.
1057 def test_loadTestsFromName__function_with_different_name_than_method(self):
1058 # lambdas have the name '<lambda>'.
1059 m = types.ModuleType('m')
1060 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1061 test = lambda: 1
1062 m.testcase_1 = MyTestCase
1063
1064 loader = unittest.TestLoader()
1065 suite = loader.loadTestsFromNames(['testcase_1.test'], m)
1066 self.assertIsInstance(suite, loader.suiteClass)
1067
1068 ref_suite = unittest.TestSuite([MyTestCase('test')])
1069 self.assertEqual(list(suite), [ref_suite])
1070
1071 # "The specifier name is a ``dotted name'' that may resolve ... to ... a
1072 # test method within a test case class"
1073 #
1074 # Does the method gracefully handle names that initially look like they
1075 # resolve to "a test method within a test case class" but don't?
1076 def test_loadTestsFromNames__relative_invalid_testmethod(self):
1077 m = types.ModuleType('m')
1078 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1079 def test(self):
1080 pass
1081 m.testcase_1 = MyTestCase
1082
1083 loader = unittest.TestLoader()
1084 suite = loader.loadTestsFromNames(['testcase_1.testfoo'], m)
1085 error, test = self.check_deferred_error(loader, list(suite)[0])
1086 expected = "type object 'MyTestCase' has no attribute 'testfoo'"
1087 self.assertIn(
1088 expected, error,
1089 'missing error string in %r' % error)
1090 self.assertRaisesRegex(AttributeError, expected, test.testfoo)
1091
1092 # "The specifier name is a ``dotted name'' that may resolve ... to
1093 # ... a callable object which returns a ... TestSuite instance"
1094 def test_loadTestsFromNames__callable__TestSuite(self):
1095 m = types.ModuleType('m')
1096 testcase_1 = unittest.FunctionTestCase(lambda: None)
1097 testcase_2 = unittest.FunctionTestCase(lambda: None)
1098 def return_TestSuite():
1099 return unittest.TestSuite([testcase_1, testcase_2])
1100 m.return_TestSuite = return_TestSuite
1101
1102 loader = unittest.TestLoader()
1103 suite = loader.loadTestsFromNames(['return_TestSuite'], m)
1104 self.assertIsInstance(suite, loader.suiteClass)
1105
1106 expected = unittest.TestSuite([testcase_1, testcase_2])
1107 self.assertEqual(list(suite), [expected])
1108
1109 # "The specifier name is a ``dotted name'' that may resolve ... to
1110 # ... a callable object which returns a TestCase ... instance"
1111 def test_loadTestsFromNames__callable__TestCase_instance(self):
1112 m = types.ModuleType('m')
1113 testcase_1 = unittest.FunctionTestCase(lambda: None)
1114 def return_TestCase():
1115 return testcase_1
1116 m.return_TestCase = return_TestCase
1117
1118 loader = unittest.TestLoader()
1119 suite = loader.loadTestsFromNames(['return_TestCase'], m)
1120 self.assertIsInstance(suite, loader.suiteClass)
1121
1122 ref_suite = unittest.TestSuite([testcase_1])
1123 self.assertEqual(list(suite), [ref_suite])
1124
1125 # "The specifier name is a ``dotted name'' that may resolve ... to
1126 # ... a callable object which returns a TestCase or TestSuite instance"
1127 #
1128 # Are staticmethods handled correctly?
1129 def test_loadTestsFromNames__callable__call_staticmethod(self):
1130 m = types.ModuleType('m')
1131 class ESC[4;38;5;81mTest1(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1132 def test(self):
1133 pass
1134
1135 testcase_1 = Test1('test')
1136 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1137 @staticmethod
1138 def foo():
1139 return testcase_1
1140 m.Foo = Foo
1141
1142 loader = unittest.TestLoader()
1143 suite = loader.loadTestsFromNames(['Foo.foo'], m)
1144 self.assertIsInstance(suite, loader.suiteClass)
1145
1146 ref_suite = unittest.TestSuite([testcase_1])
1147 self.assertEqual(list(suite), [ref_suite])
1148
1149 # "The specifier name is a ``dotted name'' that may resolve ... to
1150 # ... a callable object which returns a TestCase or TestSuite instance"
1151 #
1152 # What happens when the callable returns something else?
1153 def test_loadTestsFromNames__callable__wrong_type(self):
1154 m = types.ModuleType('m')
1155 def return_wrong():
1156 return 6
1157 m.return_wrong = return_wrong
1158
1159 loader = unittest.TestLoader()
1160 try:
1161 suite = loader.loadTestsFromNames(['return_wrong'], m)
1162 except TypeError:
1163 pass
1164 else:
1165 self.fail("TestLoader.loadTestsFromNames failed to raise TypeError")
1166
1167 # "The specifier can refer to modules and packages which have not been
1168 # imported; they will be imported as a side-effect"
1169 def test_loadTestsFromNames__module_not_loaded(self):
1170 # We're going to try to load this module as a side-effect, so it
1171 # better not be loaded before we try.
1172 #
1173 module_name = 'unittest.test.dummy'
1174 sys.modules.pop(module_name, None)
1175
1176 loader = unittest.TestLoader()
1177 try:
1178 suite = loader.loadTestsFromNames([module_name])
1179
1180 self.assertIsInstance(suite, loader.suiteClass)
1181 self.assertEqual(list(suite), [unittest.TestSuite()])
1182
1183 # module should now be loaded, thanks to loadTestsFromName()
1184 self.assertIn(module_name, sys.modules)
1185 finally:
1186 if module_name in sys.modules:
1187 del sys.modules[module_name]
1188
1189 ################################################################
1190 ### /Tests for TestLoader.loadTestsFromNames()
1191
1192 ### Tests for TestLoader.getTestCaseNames()
1193 ################################################################
1194
1195 # "Return a sorted sequence of method names found within testCaseClass"
1196 #
1197 # Test.foobar is defined to make sure getTestCaseNames() respects
1198 # loader.testMethodPrefix
1199 def test_getTestCaseNames(self):
1200 class ESC[4;38;5;81mTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1201 def test_1(self): pass
1202 def test_2(self): pass
1203 def foobar(self): pass
1204
1205 loader = unittest.TestLoader()
1206
1207 self.assertEqual(loader.getTestCaseNames(Test), ['test_1', 'test_2'])
1208
1209 # "Return a sorted sequence of method names found within testCaseClass"
1210 #
1211 # Does getTestCaseNames() behave appropriately if no tests are found?
1212 def test_getTestCaseNames__no_tests(self):
1213 class ESC[4;38;5;81mTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1214 def foobar(self): pass
1215
1216 loader = unittest.TestLoader()
1217
1218 self.assertEqual(loader.getTestCaseNames(Test), [])
1219
1220 # "Return a sorted sequence of method names found within testCaseClass"
1221 #
1222 # Are not-TestCases handled gracefully?
1223 #
1224 # XXX This should raise a TypeError, not return a list
1225 #
1226 # XXX It's too late in the 2.5 release cycle to fix this, but it should
1227 # probably be revisited for 2.6
1228 def test_getTestCaseNames__not_a_TestCase(self):
1229 class ESC[4;38;5;81mBadCase(ESC[4;38;5;149mint):
1230 def test_foo(self):
1231 pass
1232
1233 loader = unittest.TestLoader()
1234 names = loader.getTestCaseNames(BadCase)
1235
1236 self.assertEqual(names, ['test_foo'])
1237
1238 # "Return a sorted sequence of method names found within testCaseClass"
1239 #
1240 # Make sure inherited names are handled.
1241 #
1242 # TestP.foobar is defined to make sure getTestCaseNames() respects
1243 # loader.testMethodPrefix
1244 def test_getTestCaseNames__inheritance(self):
1245 class ESC[4;38;5;81mTestP(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1246 def test_1(self): pass
1247 def test_2(self): pass
1248 def foobar(self): pass
1249
1250 class ESC[4;38;5;81mTestC(ESC[4;38;5;149mTestP):
1251 def test_1(self): pass
1252 def test_3(self): pass
1253
1254 loader = unittest.TestLoader()
1255
1256 names = ['test_1', 'test_2', 'test_3']
1257 self.assertEqual(loader.getTestCaseNames(TestC), names)
1258
1259 # "Return a sorted sequence of method names found within testCaseClass"
1260 #
1261 # If TestLoader.testNamePatterns is set, only tests that match one of these
1262 # patterns should be included.
1263 def test_getTestCaseNames__testNamePatterns(self):
1264 class ESC[4;38;5;81mMyTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1265 def test_1(self): pass
1266 def test_2(self): pass
1267 def foobar(self): pass
1268
1269 loader = unittest.TestLoader()
1270
1271 loader.testNamePatterns = []
1272 self.assertEqual(loader.getTestCaseNames(MyTest), [])
1273
1274 loader.testNamePatterns = ['*1']
1275 self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])
1276
1277 loader.testNamePatterns = ['*1', '*2']
1278 self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2'])
1279
1280 loader.testNamePatterns = ['*My*']
1281 self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1', 'test_2'])
1282
1283 loader.testNamePatterns = ['*my*']
1284 self.assertEqual(loader.getTestCaseNames(MyTest), [])
1285
1286 # "Return a sorted sequence of method names found within testCaseClass"
1287 #
1288 # If TestLoader.testNamePatterns is set, only tests that match one of these
1289 # patterns should be included.
1290 #
1291 # For backwards compatibility reasons (see bpo-32071), the check may only
1292 # touch a TestCase's attribute if it starts with the test method prefix.
1293 def test_getTestCaseNames__testNamePatterns__attribute_access_regression(self):
1294 class ESC[4;38;5;81mTrap:
1295 def __get__(*ignored):
1296 self.fail('Non-test attribute accessed')
1297
1298 class ESC[4;38;5;81mMyTest(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1299 def test_1(self): pass
1300 foobar = Trap()
1301
1302 loader = unittest.TestLoader()
1303 self.assertEqual(loader.getTestCaseNames(MyTest), ['test_1'])
1304
1305 loader = unittest.TestLoader()
1306 loader.testNamePatterns = []
1307 self.assertEqual(loader.getTestCaseNames(MyTest), [])
1308
1309 ################################################################
1310 ### /Tests for TestLoader.getTestCaseNames()
1311
1312 ### Tests for TestLoader.testMethodPrefix
1313 ################################################################
1314
1315 # "String giving the prefix of method names which will be interpreted as
1316 # test methods"
1317 #
1318 # Implicit in the documentation is that testMethodPrefix is respected by
1319 # all loadTestsFrom* methods.
1320 def test_testMethodPrefix__loadTestsFromTestCase(self):
1321 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1322 def test_1(self): pass
1323 def test_2(self): pass
1324 def foo_bar(self): pass
1325
1326 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1327 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1328
1329 loader = unittest.TestLoader()
1330 loader.testMethodPrefix = 'foo'
1331 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_1)
1332
1333 loader.testMethodPrefix = 'test'
1334 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests_2)
1335
1336 # "String giving the prefix of method names which will be interpreted as
1337 # test methods"
1338 #
1339 # Implicit in the documentation is that testMethodPrefix is respected by
1340 # all loadTestsFrom* methods.
1341 def test_testMethodPrefix__loadTestsFromModule(self):
1342 m = types.ModuleType('m')
1343 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1344 def test_1(self): pass
1345 def test_2(self): pass
1346 def foo_bar(self): pass
1347 m.Foo = Foo
1348
1349 tests_1 = [unittest.TestSuite([Foo('foo_bar')])]
1350 tests_2 = [unittest.TestSuite([Foo('test_1'), Foo('test_2')])]
1351
1352 loader = unittest.TestLoader()
1353 loader.testMethodPrefix = 'foo'
1354 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_1)
1355
1356 loader.testMethodPrefix = 'test'
1357 self.assertEqual(list(loader.loadTestsFromModule(m)), tests_2)
1358
1359 # "String giving the prefix of method names which will be interpreted as
1360 # test methods"
1361 #
1362 # Implicit in the documentation is that testMethodPrefix is respected by
1363 # all loadTestsFrom* methods.
1364 def test_testMethodPrefix__loadTestsFromName(self):
1365 m = types.ModuleType('m')
1366 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1367 def test_1(self): pass
1368 def test_2(self): pass
1369 def foo_bar(self): pass
1370 m.Foo = Foo
1371
1372 tests_1 = unittest.TestSuite([Foo('foo_bar')])
1373 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1374
1375 loader = unittest.TestLoader()
1376 loader.testMethodPrefix = 'foo'
1377 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_1)
1378
1379 loader.testMethodPrefix = 'test'
1380 self.assertEqual(loader.loadTestsFromName('Foo', m), tests_2)
1381
1382 # "String giving the prefix of method names which will be interpreted as
1383 # test methods"
1384 #
1385 # Implicit in the documentation is that testMethodPrefix is respected by
1386 # all loadTestsFrom* methods.
1387 def test_testMethodPrefix__loadTestsFromNames(self):
1388 m = types.ModuleType('m')
1389 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1390 def test_1(self): pass
1391 def test_2(self): pass
1392 def foo_bar(self): pass
1393 m.Foo = Foo
1394
1395 tests_1 = unittest.TestSuite([unittest.TestSuite([Foo('foo_bar')])])
1396 tests_2 = unittest.TestSuite([Foo('test_1'), Foo('test_2')])
1397 tests_2 = unittest.TestSuite([tests_2])
1398
1399 loader = unittest.TestLoader()
1400 loader.testMethodPrefix = 'foo'
1401 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_1)
1402
1403 loader.testMethodPrefix = 'test'
1404 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests_2)
1405
1406 # "The default value is 'test'"
1407 def test_testMethodPrefix__default_value(self):
1408 loader = unittest.TestLoader()
1409 self.assertEqual(loader.testMethodPrefix, 'test')
1410
1411 ################################################################
1412 ### /Tests for TestLoader.testMethodPrefix
1413
1414 ### Tests for TestLoader.sortTestMethodsUsing
1415 ################################################################
1416
1417 # "Function to be used to compare method names when sorting them in
1418 # getTestCaseNames() and all the loadTestsFromX() methods"
1419 def test_sortTestMethodsUsing__loadTestsFromTestCase(self):
1420 def reversed_cmp(x, y):
1421 return -((x > y) - (x < y))
1422
1423 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1424 def test_1(self): pass
1425 def test_2(self): pass
1426
1427 loader = unittest.TestLoader()
1428 loader.sortTestMethodsUsing = reversed_cmp
1429
1430 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1431 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1432
1433 # "Function to be used to compare method names when sorting them in
1434 # getTestCaseNames() and all the loadTestsFromX() methods"
1435 def test_sortTestMethodsUsing__loadTestsFromModule(self):
1436 def reversed_cmp(x, y):
1437 return -((x > y) - (x < y))
1438
1439 m = types.ModuleType('m')
1440 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1441 def test_1(self): pass
1442 def test_2(self): pass
1443 m.Foo = Foo
1444
1445 loader = unittest.TestLoader()
1446 loader.sortTestMethodsUsing = reversed_cmp
1447
1448 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1449 self.assertEqual(list(loader.loadTestsFromModule(m)), tests)
1450
1451 # "Function to be used to compare method names when sorting them in
1452 # getTestCaseNames() and all the loadTestsFromX() methods"
1453 def test_sortTestMethodsUsing__loadTestsFromName(self):
1454 def reversed_cmp(x, y):
1455 return -((x > y) - (x < y))
1456
1457 m = types.ModuleType('m')
1458 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1459 def test_1(self): pass
1460 def test_2(self): pass
1461 m.Foo = Foo
1462
1463 loader = unittest.TestLoader()
1464 loader.sortTestMethodsUsing = reversed_cmp
1465
1466 tests = loader.suiteClass([Foo('test_2'), Foo('test_1')])
1467 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1468
1469 # "Function to be used to compare method names when sorting them in
1470 # getTestCaseNames() and all the loadTestsFromX() methods"
1471 def test_sortTestMethodsUsing__loadTestsFromNames(self):
1472 def reversed_cmp(x, y):
1473 return -((x > y) - (x < y))
1474
1475 m = types.ModuleType('m')
1476 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1477 def test_1(self): pass
1478 def test_2(self): pass
1479 m.Foo = Foo
1480
1481 loader = unittest.TestLoader()
1482 loader.sortTestMethodsUsing = reversed_cmp
1483
1484 tests = [loader.suiteClass([Foo('test_2'), Foo('test_1')])]
1485 self.assertEqual(list(loader.loadTestsFromNames(['Foo'], m)), tests)
1486
1487 # "Function to be used to compare method names when sorting them in
1488 # getTestCaseNames()"
1489 #
1490 # Does it actually affect getTestCaseNames()?
1491 def test_sortTestMethodsUsing__getTestCaseNames(self):
1492 def reversed_cmp(x, y):
1493 return -((x > y) - (x < y))
1494
1495 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1496 def test_1(self): pass
1497 def test_2(self): pass
1498
1499 loader = unittest.TestLoader()
1500 loader.sortTestMethodsUsing = reversed_cmp
1501
1502 test_names = ['test_2', 'test_1']
1503 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
1504
1505 # "The default value is the built-in cmp() function"
1506 # Since cmp is now defunct, we simply verify that the results
1507 # occur in the same order as they would with the default sort.
1508 def test_sortTestMethodsUsing__default_value(self):
1509 loader = unittest.TestLoader()
1510
1511 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1512 def test_2(self): pass
1513 def test_3(self): pass
1514 def test_1(self): pass
1515
1516 test_names = ['test_2', 'test_3', 'test_1']
1517 self.assertEqual(loader.getTestCaseNames(Foo), sorted(test_names))
1518
1519
1520 # "it can be set to None to disable the sort."
1521 #
1522 # XXX How is this different from reassigning cmp? Are the tests returned
1523 # in a random order or something? This behaviour should die
1524 def test_sortTestMethodsUsing__None(self):
1525 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1526 def test_1(self): pass
1527 def test_2(self): pass
1528
1529 loader = unittest.TestLoader()
1530 loader.sortTestMethodsUsing = None
1531
1532 test_names = ['test_2', 'test_1']
1533 self.assertEqual(set(loader.getTestCaseNames(Foo)), set(test_names))
1534
1535 ################################################################
1536 ### /Tests for TestLoader.sortTestMethodsUsing
1537
1538 ### Tests for TestLoader.suiteClass
1539 ################################################################
1540
1541 # "Callable object that constructs a test suite from a list of tests."
1542 def test_suiteClass__loadTestsFromTestCase(self):
1543 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1544 def test_1(self): pass
1545 def test_2(self): pass
1546 def foo_bar(self): pass
1547
1548 tests = [Foo('test_1'), Foo('test_2')]
1549
1550 loader = unittest.TestLoader()
1551 loader.suiteClass = list
1552 self.assertEqual(loader.loadTestsFromTestCase(Foo), tests)
1553
1554 # It is implicit in the documentation for TestLoader.suiteClass that
1555 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1556 def test_suiteClass__loadTestsFromModule(self):
1557 m = types.ModuleType('m')
1558 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1559 def test_1(self): pass
1560 def test_2(self): pass
1561 def foo_bar(self): pass
1562 m.Foo = Foo
1563
1564 tests = [[Foo('test_1'), Foo('test_2')]]
1565
1566 loader = unittest.TestLoader()
1567 loader.suiteClass = list
1568 self.assertEqual(loader.loadTestsFromModule(m), tests)
1569
1570 # It is implicit in the documentation for TestLoader.suiteClass that
1571 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1572 def test_suiteClass__loadTestsFromName(self):
1573 m = types.ModuleType('m')
1574 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1575 def test_1(self): pass
1576 def test_2(self): pass
1577 def foo_bar(self): pass
1578 m.Foo = Foo
1579
1580 tests = [Foo('test_1'), Foo('test_2')]
1581
1582 loader = unittest.TestLoader()
1583 loader.suiteClass = list
1584 self.assertEqual(loader.loadTestsFromName('Foo', m), tests)
1585
1586 # It is implicit in the documentation for TestLoader.suiteClass that
1587 # all TestLoader.loadTestsFrom* methods respect it. Let's make sure
1588 def test_suiteClass__loadTestsFromNames(self):
1589 m = types.ModuleType('m')
1590 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1591 def test_1(self): pass
1592 def test_2(self): pass
1593 def foo_bar(self): pass
1594 m.Foo = Foo
1595
1596 tests = [[Foo('test_1'), Foo('test_2')]]
1597
1598 loader = unittest.TestLoader()
1599 loader.suiteClass = list
1600 self.assertEqual(loader.loadTestsFromNames(['Foo'], m), tests)
1601
1602 # "The default value is the TestSuite class"
1603 def test_suiteClass__default_value(self):
1604 loader = unittest.TestLoader()
1605 self.assertIs(loader.suiteClass, unittest.TestSuite)
1606
1607
1608 def test_partial_functions(self):
1609 def noop(arg):
1610 pass
1611
1612 class ESC[4;38;5;81mFoo(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1613 pass
1614
1615 setattr(Foo, 'test_partial', functools.partial(noop, None))
1616
1617 loader = unittest.TestLoader()
1618
1619 test_names = ['test_partial']
1620 self.assertEqual(loader.getTestCaseNames(Foo), test_names)
1621
1622
1623 class ESC[4;38;5;81mTestObsoleteFunctions(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1624 class ESC[4;38;5;81mMyTestSuite(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestSuite):
1625 pass
1626
1627 class ESC[4;38;5;81mMyTestCase(ESC[4;38;5;149munittestESC[4;38;5;149m.ESC[4;38;5;149mTestCase):
1628 def check_1(self): pass
1629 def check_2(self): pass
1630 def test(self): pass
1631
1632 @staticmethod
1633 def reverse_three_way_cmp(a, b):
1634 return unittest.util.three_way_cmp(b, a)
1635
1636 def test_getTestCaseNames(self):
1637 with self.assertWarns(DeprecationWarning) as w:
1638 tests = unittest.getTestCaseNames(self.MyTestCase,
1639 prefix='check', sortUsing=self.reverse_three_way_cmp,
1640 testNamePatterns=None)
1641 self.assertEqual(w.filename, __file__)
1642 self.assertEqual(tests, ['check_2', 'check_1'])
1643
1644 def test_makeSuite(self):
1645 with self.assertWarns(DeprecationWarning) as w:
1646 suite = unittest.makeSuite(self.MyTestCase,
1647 prefix='check', sortUsing=self.reverse_three_way_cmp,
1648 suiteClass=self.MyTestSuite)
1649 self.assertEqual(w.filename, __file__)
1650 self.assertIsInstance(suite, self.MyTestSuite)
1651 expected = self.MyTestSuite([self.MyTestCase('check_2'),
1652 self.MyTestCase('check_1')])
1653 self.assertEqual(suite, expected)
1654
1655 def test_findTestCases(self):
1656 m = types.ModuleType('m')
1657 m.testcase_1 = self.MyTestCase
1658
1659 with self.assertWarns(DeprecationWarning) as w:
1660 suite = unittest.findTestCases(m,
1661 prefix='check', sortUsing=self.reverse_three_way_cmp,
1662 suiteClass=self.MyTestSuite)
1663 self.assertEqual(w.filename, __file__)
1664 self.assertIsInstance(suite, self.MyTestSuite)
1665 expected = [self.MyTestSuite([self.MyTestCase('check_2'),
1666 self.MyTestCase('check_1')])]
1667 self.assertEqual(list(suite), expected)
1668
1669
1670 if __name__ == "__main__":
1671 unittest.main()