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