ยปCore Development>Code coverage>Lib/unittest/test/test_loader.py

Python code coverage for Lib/unittest/test/test_loader.py

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