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

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

#countcontent
1n/aimport os.path
2n/afrom os.path import abspath
3n/aimport re
4n/aimport sys
5n/aimport types
6n/aimport pickle
7n/aimport builtins
8n/afrom test import support
9n/a
10n/aimport unittest
11n/aimport unittest.test
12n/a
13n/a
14n/aclass TestableTestProgram(unittest.TestProgram):
15n/a module = None
16n/a exit = True
17n/a defaultTest = failfast = catchbreak = buffer = None
18n/a verbosity = 1
19n/a progName = ''
20n/a testRunner = testLoader = None
21n/a
22n/a def __init__(self):
23n/a pass
24n/a
25n/a
26n/aclass TestDiscovery(unittest.TestCase):
27n/a
28n/a # Heavily mocked tests so I can avoid hitting the filesystem
29n/a def test_get_name_from_path(self):
30n/a loader = unittest.TestLoader()
31n/a loader._top_level_dir = '/foo'
32n/a name = loader._get_name_from_path('/foo/bar/baz.py')
33n/a self.assertEqual(name, 'bar.baz')
34n/a
35n/a if not __debug__:
36n/a # asserts are off
37n/a return
38n/a
39n/a with self.assertRaises(AssertionError):
40n/a loader._get_name_from_path('/bar/baz.py')
41n/a
42n/a def test_find_tests(self):
43n/a loader = unittest.TestLoader()
44n/a
45n/a original_listdir = os.listdir
46n/a def restore_listdir():
47n/a os.listdir = original_listdir
48n/a original_isfile = os.path.isfile
49n/a def restore_isfile():
50n/a os.path.isfile = original_isfile
51n/a original_isdir = os.path.isdir
52n/a def restore_isdir():
53n/a os.path.isdir = original_isdir
54n/a
55n/a path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir',
56n/a 'test.foo', 'test-not-a-module.py', 'another_dir'],
57n/a ['test4.py', 'test3.py', ]]
58n/a os.listdir = lambda path: path_lists.pop(0)
59n/a self.addCleanup(restore_listdir)
60n/a
61n/a def isdir(path):
62n/a return path.endswith('dir')
63n/a os.path.isdir = isdir
64n/a self.addCleanup(restore_isdir)
65n/a
66n/a def isfile(path):
67n/a # another_dir is not a package and so shouldn't be recursed into
68n/a return not path.endswith('dir') and not 'another_dir' in path
69n/a os.path.isfile = isfile
70n/a self.addCleanup(restore_isfile)
71n/a
72n/a loader._get_module_from_name = lambda path: path + ' module'
73n/a orig_load_tests = loader.loadTestsFromModule
74n/a def loadTestsFromModule(module, pattern=None):
75n/a # This is where load_tests is called.
76n/a base = orig_load_tests(module, pattern=pattern)
77n/a return base + [module + ' tests']
78n/a loader.loadTestsFromModule = loadTestsFromModule
79n/a loader.suiteClass = lambda thing: thing
80n/a
81n/a top_level = os.path.abspath('/foo')
82n/a loader._top_level_dir = top_level
83n/a suite = list(loader._find_tests(top_level, 'test*.py'))
84n/a
85n/a # The test suites found should be sorted alphabetically for reliable
86n/a # execution order.
87n/a expected = [[name + ' module tests'] for name in
88n/a ('test1', 'test2', 'test_dir')]
89n/a expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
90n/a ('test3', 'test4')])
91n/a self.assertEqual(suite, expected)
92n/a
93n/a def test_find_tests_socket(self):
94n/a # A socket is neither a directory nor a regular file.
95n/a # https://bugs.python.org/issue25320
96n/a loader = unittest.TestLoader()
97n/a
98n/a original_listdir = os.listdir
99n/a def restore_listdir():
100n/a os.listdir = original_listdir
101n/a original_isfile = os.path.isfile
102n/a def restore_isfile():
103n/a os.path.isfile = original_isfile
104n/a original_isdir = os.path.isdir
105n/a def restore_isdir():
106n/a os.path.isdir = original_isdir
107n/a
108n/a path_lists = [['socket']]
109n/a os.listdir = lambda path: path_lists.pop(0)
110n/a self.addCleanup(restore_listdir)
111n/a
112n/a os.path.isdir = lambda path: False
113n/a self.addCleanup(restore_isdir)
114n/a
115n/a os.path.isfile = lambda path: False
116n/a self.addCleanup(restore_isfile)
117n/a
118n/a loader._get_module_from_name = lambda path: path + ' module'
119n/a orig_load_tests = loader.loadTestsFromModule
120n/a def loadTestsFromModule(module, pattern=None):
121n/a # This is where load_tests is called.
122n/a base = orig_load_tests(module, pattern=pattern)
123n/a return base + [module + ' tests']
124n/a loader.loadTestsFromModule = loadTestsFromModule
125n/a loader.suiteClass = lambda thing: thing
126n/a
127n/a top_level = os.path.abspath('/foo')
128n/a loader._top_level_dir = top_level
129n/a suite = list(loader._find_tests(top_level, 'test*.py'))
130n/a
131n/a self.assertEqual(suite, [])
132n/a
133n/a def test_find_tests_with_package(self):
134n/a loader = unittest.TestLoader()
135n/a
136n/a original_listdir = os.listdir
137n/a def restore_listdir():
138n/a os.listdir = original_listdir
139n/a original_isfile = os.path.isfile
140n/a def restore_isfile():
141n/a os.path.isfile = original_isfile
142n/a original_isdir = os.path.isdir
143n/a def restore_isdir():
144n/a os.path.isdir = original_isdir
145n/a
146n/a directories = ['a_directory', 'test_directory', 'test_directory2']
147n/a path_lists = [directories, [], [], []]
148n/a os.listdir = lambda path: path_lists.pop(0)
149n/a self.addCleanup(restore_listdir)
150n/a
151n/a os.path.isdir = lambda path: True
152n/a self.addCleanup(restore_isdir)
153n/a
154n/a os.path.isfile = lambda path: os.path.basename(path) not in directories
155n/a self.addCleanup(restore_isfile)
156n/a
157n/a class Module(object):
158n/a paths = []
159n/a load_tests_args = []
160n/a
161n/a def __init__(self, path):
162n/a self.path = path
163n/a self.paths.append(path)
164n/a if os.path.basename(path) == 'test_directory':
165n/a def load_tests(loader, tests, pattern):
166n/a self.load_tests_args.append((loader, tests, pattern))
167n/a return [self.path + ' load_tests']
168n/a self.load_tests = load_tests
169n/a
170n/a def __eq__(self, other):
171n/a return self.path == other.path
172n/a
173n/a loader._get_module_from_name = lambda name: Module(name)
174n/a orig_load_tests = loader.loadTestsFromModule
175n/a def loadTestsFromModule(module, pattern=None):
176n/a # This is where load_tests is called.
177n/a base = orig_load_tests(module, pattern=pattern)
178n/a return base + [module.path + ' module tests']
179n/a loader.loadTestsFromModule = loadTestsFromModule
180n/a loader.suiteClass = lambda thing: thing
181n/a
182n/a loader._top_level_dir = '/foo'
183n/a # this time no '.py' on the pattern so that it can match
184n/a # a test package
185n/a suite = list(loader._find_tests('/foo', 'test*'))
186n/a
187n/a # We should have loaded tests from the a_directory and test_directory2
188n/a # directly and via load_tests for the test_directory package, which
189n/a # still calls the baseline module loader.
190n/a self.assertEqual(suite,
191n/a [['a_directory module tests'],
192n/a ['test_directory load_tests',
193n/a 'test_directory module tests'],
194n/a ['test_directory2 module tests']])
195n/a
196n/a
197n/a # The test module paths should be sorted for reliable execution order
198n/a self.assertEqual(Module.paths,
199n/a ['a_directory', 'test_directory', 'test_directory2'])
200n/a
201n/a # load_tests should have been called once with loader, tests and pattern
202n/a # (but there are no tests in our stub module itself, so thats [] at the
203n/a # time of call.
204n/a self.assertEqual(Module.load_tests_args,
205n/a [(loader, [], 'test*')])
206n/a
207n/a def test_find_tests_default_calls_package_load_tests(self):
208n/a loader = unittest.TestLoader()
209n/a
210n/a original_listdir = os.listdir
211n/a def restore_listdir():
212n/a os.listdir = original_listdir
213n/a original_isfile = os.path.isfile
214n/a def restore_isfile():
215n/a os.path.isfile = original_isfile
216n/a original_isdir = os.path.isdir
217n/a def restore_isdir():
218n/a os.path.isdir = original_isdir
219n/a
220n/a directories = ['a_directory', 'test_directory', 'test_directory2']
221n/a path_lists = [directories, [], [], []]
222n/a os.listdir = lambda path: path_lists.pop(0)
223n/a self.addCleanup(restore_listdir)
224n/a
225n/a os.path.isdir = lambda path: True
226n/a self.addCleanup(restore_isdir)
227n/a
228n/a os.path.isfile = lambda path: os.path.basename(path) not in directories
229n/a self.addCleanup(restore_isfile)
230n/a
231n/a class Module(object):
232n/a paths = []
233n/a load_tests_args = []
234n/a
235n/a def __init__(self, path):
236n/a self.path = path
237n/a self.paths.append(path)
238n/a if os.path.basename(path) == 'test_directory':
239n/a def load_tests(loader, tests, pattern):
240n/a self.load_tests_args.append((loader, tests, pattern))
241n/a return [self.path + ' load_tests']
242n/a self.load_tests = load_tests
243n/a
244n/a def __eq__(self, other):
245n/a return self.path == other.path
246n/a
247n/a loader._get_module_from_name = lambda name: Module(name)
248n/a orig_load_tests = loader.loadTestsFromModule
249n/a def loadTestsFromModule(module, pattern=None):
250n/a # This is where load_tests is called.
251n/a base = orig_load_tests(module, pattern=pattern)
252n/a return base + [module.path + ' module tests']
253n/a loader.loadTestsFromModule = loadTestsFromModule
254n/a loader.suiteClass = lambda thing: thing
255n/a
256n/a loader._top_level_dir = '/foo'
257n/a # this time no '.py' on the pattern so that it can match
258n/a # a test package
259n/a suite = list(loader._find_tests('/foo', 'test*.py'))
260n/a
261n/a # We should have loaded tests from the a_directory and test_directory2
262n/a # directly and via load_tests for the test_directory package, which
263n/a # still calls the baseline module loader.
264n/a self.assertEqual(suite,
265n/a [['a_directory module tests'],
266n/a ['test_directory load_tests',
267n/a 'test_directory module tests'],
268n/a ['test_directory2 module tests']])
269n/a # The test module paths should be sorted for reliable execution order
270n/a self.assertEqual(Module.paths,
271n/a ['a_directory', 'test_directory', 'test_directory2'])
272n/a
273n/a
274n/a # load_tests should have been called once with loader, tests and pattern
275n/a self.assertEqual(Module.load_tests_args,
276n/a [(loader, [], 'test*.py')])
277n/a
278n/a def test_find_tests_customize_via_package_pattern(self):
279n/a # This test uses the example 'do-nothing' load_tests from
280n/a # https://docs.python.org/3/library/unittest.html#load-tests-protocol
281n/a # to make sure that that actually works.
282n/a # Housekeeping
283n/a original_listdir = os.listdir
284n/a def restore_listdir():
285n/a os.listdir = original_listdir
286n/a self.addCleanup(restore_listdir)
287n/a original_isfile = os.path.isfile
288n/a def restore_isfile():
289n/a os.path.isfile = original_isfile
290n/a self.addCleanup(restore_isfile)
291n/a original_isdir = os.path.isdir
292n/a def restore_isdir():
293n/a os.path.isdir = original_isdir
294n/a self.addCleanup(restore_isdir)
295n/a self.addCleanup(sys.path.remove, abspath('/foo'))
296n/a
297n/a # Test data: we expect the following:
298n/a # a listdir to find our package, and isfile and isdir checks on it.
299n/a # a module-from-name call to turn that into a module
300n/a # followed by load_tests.
301n/a # then our load_tests will call discover() which is messy
302n/a # but that finally chains into find_tests again for the child dir -
303n/a # which is why we don't have an infinite loop.
304n/a # We expect to see:
305n/a # the module load tests for both package and plain module called,
306n/a # and the plain module result nested by the package module load_tests
307n/a # indicating that it was processed and could have been mutated.
308n/a vfs = {abspath('/foo'): ['my_package'],
309n/a abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
310n/a def list_dir(path):
311n/a return list(vfs[path])
312n/a os.listdir = list_dir
313n/a os.path.isdir = lambda path: not path.endswith('.py')
314n/a os.path.isfile = lambda path: path.endswith('.py')
315n/a
316n/a class Module(object):
317n/a paths = []
318n/a load_tests_args = []
319n/a
320n/a def __init__(self, path):
321n/a self.path = path
322n/a self.paths.append(path)
323n/a if path.endswith('test_module'):
324n/a def load_tests(loader, tests, pattern):
325n/a self.load_tests_args.append((loader, tests, pattern))
326n/a return [self.path + ' load_tests']
327n/a else:
328n/a def load_tests(loader, tests, pattern):
329n/a self.load_tests_args.append((loader, tests, pattern))
330n/a # top level directory cached on loader instance
331n/a __file__ = '/foo/my_package/__init__.py'
332n/a this_dir = os.path.dirname(__file__)
333n/a pkg_tests = loader.discover(
334n/a start_dir=this_dir, pattern=pattern)
335n/a return [self.path + ' load_tests', tests
336n/a ] + pkg_tests
337n/a self.load_tests = load_tests
338n/a
339n/a def __eq__(self, other):
340n/a return self.path == other.path
341n/a
342n/a loader = unittest.TestLoader()
343n/a loader._get_module_from_name = lambda name: Module(name)
344n/a loader.suiteClass = lambda thing: thing
345n/a
346n/a loader._top_level_dir = abspath('/foo')
347n/a # this time no '.py' on the pattern so that it can match
348n/a # a test package
349n/a suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
350n/a
351n/a # We should have loaded tests from both my_package and
352n/a # my_package.test_module, and also run the load_tests hook in both.
353n/a # (normally this would be nested TestSuites.)
354n/a self.assertEqual(suite,
355n/a [['my_package load_tests', [],
356n/a ['my_package.test_module load_tests']]])
357n/a # Parents before children.
358n/a self.assertEqual(Module.paths,
359n/a ['my_package', 'my_package.test_module'])
360n/a
361n/a # load_tests should have been called twice with loader, tests and pattern
362n/a self.assertEqual(Module.load_tests_args,
363n/a [(loader, [], 'test*.py'),
364n/a (loader, [], 'test*.py')])
365n/a
366n/a def test_discover(self):
367n/a loader = unittest.TestLoader()
368n/a
369n/a original_isfile = os.path.isfile
370n/a original_isdir = os.path.isdir
371n/a def restore_isfile():
372n/a os.path.isfile = original_isfile
373n/a
374n/a os.path.isfile = lambda path: False
375n/a self.addCleanup(restore_isfile)
376n/a
377n/a orig_sys_path = sys.path[:]
378n/a def restore_path():
379n/a sys.path[:] = orig_sys_path
380n/a self.addCleanup(restore_path)
381n/a
382n/a full_path = os.path.abspath(os.path.normpath('/foo'))
383n/a with self.assertRaises(ImportError):
384n/a loader.discover('/foo/bar', top_level_dir='/foo')
385n/a
386n/a self.assertEqual(loader._top_level_dir, full_path)
387n/a self.assertIn(full_path, sys.path)
388n/a
389n/a os.path.isfile = lambda path: True
390n/a os.path.isdir = lambda path: True
391n/a
392n/a def restore_isdir():
393n/a os.path.isdir = original_isdir
394n/a self.addCleanup(restore_isdir)
395n/a
396n/a _find_tests_args = []
397n/a def _find_tests(start_dir, pattern, namespace=None):
398n/a _find_tests_args.append((start_dir, pattern))
399n/a return ['tests']
400n/a loader._find_tests = _find_tests
401n/a loader.suiteClass = str
402n/a
403n/a suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')
404n/a
405n/a top_level_dir = os.path.abspath('/foo/bar')
406n/a start_dir = os.path.abspath('/foo/bar/baz')
407n/a self.assertEqual(suite, "['tests']")
408n/a self.assertEqual(loader._top_level_dir, top_level_dir)
409n/a self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
410n/a self.assertIn(top_level_dir, sys.path)
411n/a
412n/a def test_discover_start_dir_is_package_calls_package_load_tests(self):
413n/a # This test verifies that the package load_tests in a package is indeed
414n/a # invoked when the start_dir is a package (and not the top level).
415n/a # http://bugs.python.org/issue22457
416n/a
417n/a # Test data: we expect the following:
418n/a # an isfile to verify the package, then importing and scanning
419n/a # as per _find_tests' normal behaviour.
420n/a # We expect to see our load_tests hook called once.
421n/a vfs = {abspath('/toplevel'): ['startdir'],
422n/a abspath('/toplevel/startdir'): ['__init__.py']}
423n/a def list_dir(path):
424n/a return list(vfs[path])
425n/a self.addCleanup(setattr, os, 'listdir', os.listdir)
426n/a os.listdir = list_dir
427n/a self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
428n/a os.path.isfile = lambda path: path.endswith('.py')
429n/a self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
430n/a os.path.isdir = lambda path: not path.endswith('.py')
431n/a self.addCleanup(sys.path.remove, abspath('/toplevel'))
432n/a
433n/a class Module(object):
434n/a paths = []
435n/a load_tests_args = []
436n/a
437n/a def __init__(self, path):
438n/a self.path = path
439n/a
440n/a def load_tests(self, loader, tests, pattern):
441n/a return ['load_tests called ' + self.path]
442n/a
443n/a def __eq__(self, other):
444n/a return self.path == other.path
445n/a
446n/a loader = unittest.TestLoader()
447n/a loader._get_module_from_name = lambda name: Module(name)
448n/a loader.suiteClass = lambda thing: thing
449n/a
450n/a suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')
451n/a
452n/a # We should have loaded tests from the package __init__.
453n/a # (normally this would be nested TestSuites.)
454n/a self.assertEqual(suite,
455n/a [['load_tests called startdir']])
456n/a
457n/a def setup_import_issue_tests(self, fakefile):
458n/a listdir = os.listdir
459n/a os.listdir = lambda _: [fakefile]
460n/a isfile = os.path.isfile
461n/a os.path.isfile = lambda _: True
462n/a orig_sys_path = sys.path[:]
463n/a def restore():
464n/a os.path.isfile = isfile
465n/a os.listdir = listdir
466n/a sys.path[:] = orig_sys_path
467n/a self.addCleanup(restore)
468n/a
469n/a def setup_import_issue_package_tests(self, vfs):
470n/a self.addCleanup(setattr, os, 'listdir', os.listdir)
471n/a self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
472n/a self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
473n/a self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
474n/a def list_dir(path):
475n/a return list(vfs[path])
476n/a os.listdir = list_dir
477n/a os.path.isdir = lambda path: not path.endswith('.py')
478n/a os.path.isfile = lambda path: path.endswith('.py')
479n/a
480n/a def test_discover_with_modules_that_fail_to_import(self):
481n/a loader = unittest.TestLoader()
482n/a
483n/a self.setup_import_issue_tests('test_this_does_not_exist.py')
484n/a
485n/a suite = loader.discover('.')
486n/a self.assertIn(os.getcwd(), sys.path)
487n/a self.assertEqual(suite.countTestCases(), 1)
488n/a # Errors loading the suite are also captured for introspection.
489n/a self.assertNotEqual([], loader.errors)
490n/a self.assertEqual(1, len(loader.errors))
491n/a error = loader.errors[0]
492n/a self.assertTrue(
493n/a 'Failed to import test module: test_this_does_not_exist' in error,
494n/a 'missing error string in %r' % error)
495n/a test = list(list(suite)[0])[0] # extract test from suite
496n/a
497n/a with self.assertRaises(ImportError):
498n/a test.test_this_does_not_exist()
499n/a
500n/a def test_discover_with_init_modules_that_fail_to_import(self):
501n/a vfs = {abspath('/foo'): ['my_package'],
502n/a abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
503n/a self.setup_import_issue_package_tests(vfs)
504n/a import_calls = []
505n/a def _get_module_from_name(name):
506n/a import_calls.append(name)
507n/a raise ImportError("Cannot import Name")
508n/a loader = unittest.TestLoader()
509n/a loader._get_module_from_name = _get_module_from_name
510n/a suite = loader.discover(abspath('/foo'))
511n/a
512n/a self.assertIn(abspath('/foo'), sys.path)
513n/a self.assertEqual(suite.countTestCases(), 1)
514n/a # Errors loading the suite are also captured for introspection.
515n/a self.assertNotEqual([], loader.errors)
516n/a self.assertEqual(1, len(loader.errors))
517n/a error = loader.errors[0]
518n/a self.assertTrue(
519n/a 'Failed to import test module: my_package' in error,
520n/a 'missing error string in %r' % error)
521n/a test = list(list(suite)[0])[0] # extract test from suite
522n/a with self.assertRaises(ImportError):
523n/a test.my_package()
524n/a self.assertEqual(import_calls, ['my_package'])
525n/a
526n/a # Check picklability
527n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
528n/a pickle.loads(pickle.dumps(test, proto))
529n/a
530n/a def test_discover_with_module_that_raises_SkipTest_on_import(self):
531n/a loader = unittest.TestLoader()
532n/a
533n/a def _get_module_from_name(name):
534n/a raise unittest.SkipTest('skipperoo')
535n/a loader._get_module_from_name = _get_module_from_name
536n/a
537n/a self.setup_import_issue_tests('test_skip_dummy.py')
538n/a
539n/a suite = loader.discover('.')
540n/a self.assertEqual(suite.countTestCases(), 1)
541n/a
542n/a result = unittest.TestResult()
543n/a suite.run(result)
544n/a self.assertEqual(len(result.skipped), 1)
545n/a
546n/a # Check picklability
547n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
548n/a pickle.loads(pickle.dumps(suite, proto))
549n/a
550n/a def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
551n/a vfs = {abspath('/foo'): ['my_package'],
552n/a abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
553n/a self.setup_import_issue_package_tests(vfs)
554n/a import_calls = []
555n/a def _get_module_from_name(name):
556n/a import_calls.append(name)
557n/a raise unittest.SkipTest('skipperoo')
558n/a loader = unittest.TestLoader()
559n/a loader._get_module_from_name = _get_module_from_name
560n/a suite = loader.discover(abspath('/foo'))
561n/a
562n/a self.assertIn(abspath('/foo'), sys.path)
563n/a self.assertEqual(suite.countTestCases(), 1)
564n/a result = unittest.TestResult()
565n/a suite.run(result)
566n/a self.assertEqual(len(result.skipped), 1)
567n/a self.assertEqual(result.testsRun, 1)
568n/a self.assertEqual(import_calls, ['my_package'])
569n/a
570n/a # Check picklability
571n/a for proto in range(pickle.HIGHEST_PROTOCOL + 1):
572n/a pickle.loads(pickle.dumps(suite, proto))
573n/a
574n/a def test_command_line_handling_parseArgs(self):
575n/a program = TestableTestProgram()
576n/a
577n/a args = []
578n/a program._do_discovery = args.append
579n/a program.parseArgs(['something', 'discover'])
580n/a self.assertEqual(args, [[]])
581n/a
582n/a args[:] = []
583n/a program.parseArgs(['something', 'discover', 'foo', 'bar'])
584n/a self.assertEqual(args, [['foo', 'bar']])
585n/a
586n/a def test_command_line_handling_discover_by_default(self):
587n/a program = TestableTestProgram()
588n/a
589n/a args = []
590n/a program._do_discovery = args.append
591n/a program.parseArgs(['something'])
592n/a self.assertEqual(args, [[]])
593n/a self.assertEqual(program.verbosity, 1)
594n/a self.assertIs(program.buffer, False)
595n/a self.assertIs(program.catchbreak, False)
596n/a self.assertIs(program.failfast, False)
597n/a
598n/a def test_command_line_handling_discover_by_default_with_options(self):
599n/a program = TestableTestProgram()
600n/a
601n/a args = []
602n/a program._do_discovery = args.append
603n/a program.parseArgs(['something', '-v', '-b', '-v', '-c', '-f'])
604n/a self.assertEqual(args, [[]])
605n/a self.assertEqual(program.verbosity, 2)
606n/a self.assertIs(program.buffer, True)
607n/a self.assertIs(program.catchbreak, True)
608n/a self.assertIs(program.failfast, True)
609n/a
610n/a
611n/a def test_command_line_handling_do_discovery_too_many_arguments(self):
612n/a program = TestableTestProgram()
613n/a program.testLoader = None
614n/a
615n/a with support.captured_stderr() as stderr, \
616n/a self.assertRaises(SystemExit) as cm:
617n/a # too many args
618n/a program._do_discovery(['one', 'two', 'three', 'four'])
619n/a self.assertEqual(cm.exception.args, (2,))
620n/a self.assertIn('usage:', stderr.getvalue())
621n/a
622n/a
623n/a def test_command_line_handling_do_discovery_uses_default_loader(self):
624n/a program = object.__new__(unittest.TestProgram)
625n/a program._initArgParsers()
626n/a
627n/a class Loader(object):
628n/a args = []
629n/a def discover(self, start_dir, pattern, top_level_dir):
630n/a self.args.append((start_dir, pattern, top_level_dir))
631n/a return 'tests'
632n/a
633n/a program.testLoader = Loader()
634n/a program._do_discovery(['-v'])
635n/a self.assertEqual(Loader.args, [('.', 'test*.py', None)])
636n/a
637n/a def test_command_line_handling_do_discovery_calls_loader(self):
638n/a program = TestableTestProgram()
639n/a
640n/a class Loader(object):
641n/a args = []
642n/a def discover(self, start_dir, pattern, top_level_dir):
643n/a self.args.append((start_dir, pattern, top_level_dir))
644n/a return 'tests'
645n/a
646n/a program._do_discovery(['-v'], Loader=Loader)
647n/a self.assertEqual(program.verbosity, 2)
648n/a self.assertEqual(program.test, 'tests')
649n/a self.assertEqual(Loader.args, [('.', 'test*.py', None)])
650n/a
651n/a Loader.args = []
652n/a program = TestableTestProgram()
653n/a program._do_discovery(['--verbose'], Loader=Loader)
654n/a self.assertEqual(program.test, 'tests')
655n/a self.assertEqual(Loader.args, [('.', 'test*.py', None)])
656n/a
657n/a Loader.args = []
658n/a program = TestableTestProgram()
659n/a program._do_discovery([], Loader=Loader)
660n/a self.assertEqual(program.test, 'tests')
661n/a self.assertEqual(Loader.args, [('.', 'test*.py', None)])
662n/a
663n/a Loader.args = []
664n/a program = TestableTestProgram()
665n/a program._do_discovery(['fish'], Loader=Loader)
666n/a self.assertEqual(program.test, 'tests')
667n/a self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
668n/a
669n/a Loader.args = []
670n/a program = TestableTestProgram()
671n/a program._do_discovery(['fish', 'eggs'], Loader=Loader)
672n/a self.assertEqual(program.test, 'tests')
673n/a self.assertEqual(Loader.args, [('fish', 'eggs', None)])
674n/a
675n/a Loader.args = []
676n/a program = TestableTestProgram()
677n/a program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
678n/a self.assertEqual(program.test, 'tests')
679n/a self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
680n/a
681n/a Loader.args = []
682n/a program = TestableTestProgram()
683n/a program._do_discovery(['-s', 'fish'], Loader=Loader)
684n/a self.assertEqual(program.test, 'tests')
685n/a self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
686n/a
687n/a Loader.args = []
688n/a program = TestableTestProgram()
689n/a program._do_discovery(['-t', 'fish'], Loader=Loader)
690n/a self.assertEqual(program.test, 'tests')
691n/a self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
692n/a
693n/a Loader.args = []
694n/a program = TestableTestProgram()
695n/a program._do_discovery(['-p', 'fish'], Loader=Loader)
696n/a self.assertEqual(program.test, 'tests')
697n/a self.assertEqual(Loader.args, [('.', 'fish', None)])
698n/a self.assertFalse(program.failfast)
699n/a self.assertFalse(program.catchbreak)
700n/a
701n/a Loader.args = []
702n/a program = TestableTestProgram()
703n/a program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
704n/a Loader=Loader)
705n/a self.assertEqual(program.test, 'tests')
706n/a self.assertEqual(Loader.args, [('fish', 'eggs', None)])
707n/a self.assertEqual(program.verbosity, 2)
708n/a self.assertTrue(program.failfast)
709n/a self.assertTrue(program.catchbreak)
710n/a
711n/a def setup_module_clash(self):
712n/a class Module(object):
713n/a __file__ = 'bar/foo.py'
714n/a sys.modules['foo'] = Module
715n/a full_path = os.path.abspath('foo')
716n/a original_listdir = os.listdir
717n/a original_isfile = os.path.isfile
718n/a original_isdir = os.path.isdir
719n/a
720n/a def cleanup():
721n/a os.listdir = original_listdir
722n/a os.path.isfile = original_isfile
723n/a os.path.isdir = original_isdir
724n/a del sys.modules['foo']
725n/a if full_path in sys.path:
726n/a sys.path.remove(full_path)
727n/a self.addCleanup(cleanup)
728n/a
729n/a def listdir(_):
730n/a return ['foo.py']
731n/a def isfile(_):
732n/a return True
733n/a def isdir(_):
734n/a return True
735n/a os.listdir = listdir
736n/a os.path.isfile = isfile
737n/a os.path.isdir = isdir
738n/a return full_path
739n/a
740n/a def test_detect_module_clash(self):
741n/a full_path = self.setup_module_clash()
742n/a loader = unittest.TestLoader()
743n/a
744n/a mod_dir = os.path.abspath('bar')
745n/a expected_dir = os.path.abspath('foo')
746n/a msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. "
747n/a "Is this module globally installed?" % (mod_dir, expected_dir))
748n/a self.assertRaisesRegex(
749n/a ImportError, '^%s$' % msg, loader.discover,
750n/a start_dir='foo', pattern='foo.py'
751n/a )
752n/a self.assertEqual(sys.path[0], full_path)
753n/a
754n/a def test_module_symlink_ok(self):
755n/a full_path = self.setup_module_clash()
756n/a
757n/a original_realpath = os.path.realpath
758n/a
759n/a mod_dir = os.path.abspath('bar')
760n/a expected_dir = os.path.abspath('foo')
761n/a
762n/a def cleanup():
763n/a os.path.realpath = original_realpath
764n/a self.addCleanup(cleanup)
765n/a
766n/a def realpath(path):
767n/a if path == os.path.join(mod_dir, 'foo.py'):
768n/a return os.path.join(expected_dir, 'foo.py')
769n/a return path
770n/a os.path.realpath = realpath
771n/a loader = unittest.TestLoader()
772n/a loader.discover(start_dir='foo', pattern='foo.py')
773n/a
774n/a def test_discovery_from_dotted_path(self):
775n/a loader = unittest.TestLoader()
776n/a
777n/a tests = [self]
778n/a expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
779n/a
780n/a self.wasRun = False
781n/a def _find_tests(start_dir, pattern, namespace=None):
782n/a self.wasRun = True
783n/a self.assertEqual(start_dir, expectedPath)
784n/a return tests
785n/a loader._find_tests = _find_tests
786n/a suite = loader.discover('unittest.test')
787n/a self.assertTrue(self.wasRun)
788n/a self.assertEqual(suite._tests, tests)
789n/a
790n/a
791n/a def test_discovery_from_dotted_path_builtin_modules(self):
792n/a
793n/a loader = unittest.TestLoader()
794n/a
795n/a listdir = os.listdir
796n/a os.listdir = lambda _: ['test_this_does_not_exist.py']
797n/a isfile = os.path.isfile
798n/a isdir = os.path.isdir
799n/a os.path.isdir = lambda _: False
800n/a orig_sys_path = sys.path[:]
801n/a def restore():
802n/a os.path.isfile = isfile
803n/a os.path.isdir = isdir
804n/a os.listdir = listdir
805n/a sys.path[:] = orig_sys_path
806n/a self.addCleanup(restore)
807n/a
808n/a with self.assertRaises(TypeError) as cm:
809n/a loader.discover('sys')
810n/a self.assertEqual(str(cm.exception),
811n/a 'Can not use builtin modules '
812n/a 'as dotted module names')
813n/a
814n/a def test_discovery_from_dotted_namespace_packages(self):
815n/a loader = unittest.TestLoader()
816n/a
817n/a orig_import = __import__
818n/a package = types.ModuleType('package')
819n/a package.__path__ = ['/a', '/b']
820n/a package.__spec__ = types.SimpleNamespace(
821n/a loader=None,
822n/a submodule_search_locations=['/a', '/b']
823n/a )
824n/a
825n/a def _import(packagename, *args, **kwargs):
826n/a sys.modules[packagename] = package
827n/a return package
828n/a
829n/a def cleanup():
830n/a builtins.__import__ = orig_import
831n/a self.addCleanup(cleanup)
832n/a builtins.__import__ = _import
833n/a
834n/a _find_tests_args = []
835n/a def _find_tests(start_dir, pattern, namespace=None):
836n/a _find_tests_args.append((start_dir, pattern))
837n/a return ['%s/tests' % start_dir]
838n/a
839n/a loader._find_tests = _find_tests
840n/a loader.suiteClass = list
841n/a suite = loader.discover('package')
842n/a self.assertEqual(suite, ['/a/tests', '/b/tests'])
843n/a
844n/a def test_discovery_failed_discovery(self):
845n/a loader = unittest.TestLoader()
846n/a package = types.ModuleType('package')
847n/a orig_import = __import__
848n/a
849n/a def _import(packagename, *args, **kwargs):
850n/a sys.modules[packagename] = package
851n/a return package
852n/a
853n/a def cleanup():
854n/a builtins.__import__ = orig_import
855n/a self.addCleanup(cleanup)
856n/a builtins.__import__ = _import
857n/a
858n/a with self.assertRaises(TypeError) as cm:
859n/a loader.discover('package')
860n/a self.assertEqual(str(cm.exception),
861n/a 'don\'t know how to discover from {!r}'
862n/a .format(package))
863n/a
864n/a
865n/aif __name__ == '__main__':
866n/a unittest.main()