]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.2/Lib/unittest/test/test_discovery.py
8 class TestDiscovery(unittest
.TestCase
):
10 # Heavily mocked tests so I can avoid hitting the filesystem
11 def test_get_name_from_path(self
):
12 loader
= unittest
.TestLoader()
14 loader
._top
_level
_dir
= '/foo'
15 name
= loader
._get
_name
_from
_path
('/foo/bar/baz.py')
16 self
.assertEqual(name
, 'bar.baz')
22 with self
.assertRaises(AssertionError):
23 loader
._get
_name
_from
_path
('/bar/baz.py')
25 def test_find_tests(self
):
26 loader
= unittest
.TestLoader()
28 original_listdir
= os
.listdir
29 def restore_listdir():
30 os
.listdir
= original_listdir
31 original_isfile
= os
.path
.isfile
33 os
.path
.isfile
= original_isfile
34 original_isdir
= os
.path
.isdir
36 os
.path
.isdir
= original_isdir
38 path_lists
= [['test1.py', 'test2.py', 'not_a_test.py', 'test_dir',
39 'test.foo', 'test-not-a-module.py', 'another_dir'],
40 ['test3.py', 'test4.py', ]]
41 os
.listdir
= lambda path
: path_lists
.pop(0)
42 self
.addCleanup(restore_listdir
)
45 return path
.endswith('dir')
47 self
.addCleanup(restore_isdir
)
50 # another_dir is not a package and so shouldn't be recursed into
51 return not path
.endswith('dir') and not 'another_dir' in path
52 os
.path
.isfile
= isfile
53 self
.addCleanup(restore_isfile
)
55 loader
._get
_module
_from
_name
= lambda path
: path
+ ' module'
56 loader
.loadTestsFromModule
= lambda module
: module
+ ' tests'
58 top_level
= os
.path
.abspath('/foo')
59 loader
._top
_level
_dir
= top_level
60 suite
= list(loader
._find
_tests
(top_level
, 'test*.py'))
62 expected
= [name
+ ' module tests' for name
in
64 expected
.extend([('test_dir.%s' % name
) + ' module tests' for name
in
66 self
.assertEqual(suite
, expected
)
68 def test_find_tests_with_package(self
):
69 loader
= unittest
.TestLoader()
71 original_listdir
= os
.listdir
72 def restore_listdir():
73 os
.listdir
= original_listdir
74 original_isfile
= os
.path
.isfile
76 os
.path
.isfile
= original_isfile
77 original_isdir
= os
.path
.isdir
79 os
.path
.isdir
= original_isdir
81 directories
= ['a_directory', 'test_directory', 'test_directory2']
82 path_lists
= [directories
, [], [], []]
83 os
.listdir
= lambda path
: path_lists
.pop(0)
84 self
.addCleanup(restore_listdir
)
86 os
.path
.isdir
= lambda path
: True
87 self
.addCleanup(restore_isdir
)
89 os
.path
.isfile
= lambda path
: os
.path
.basename(path
) not in directories
90 self
.addCleanup(restore_isfile
)
96 def __init__(self
, path
):
98 self
.paths
.append(path
)
99 if os
.path
.basename(path
) == 'test_directory':
100 def load_tests(loader
, tests
, pattern
):
101 self
.load_tests_args
.append((loader
, tests
, pattern
))
103 self
.load_tests
= load_tests
105 def __eq__(self
, other
):
106 return self
.path
== other
.path
108 # Silence py3k warning
111 loader
._get
_module
_from
_name
= lambda name
: Module(name
)
112 def loadTestsFromModule(module
, use_load_tests
):
114 raise self
.failureException('use_load_tests should be False for packages')
115 return module
.path
+ ' module tests'
116 loader
.loadTestsFromModule
= loadTestsFromModule
118 loader
._top
_level
_dir
= '/foo'
119 # this time no '.py' on the pattern so that it can match
121 suite
= list(loader
._find
_tests
('/foo', 'test*'))
123 # We should have loaded tests from the test_directory package by calling load_tests
124 # and directly from the test_directory2 package
125 self
.assertEqual(suite
,
126 ['load_tests', 'test_directory2' + ' module tests'])
127 self
.assertEqual(Module
.paths
, ['test_directory', 'test_directory2'])
129 # load_tests should have been called once with loader, tests and pattern
130 self
.assertEqual(Module
.load_tests_args
,
131 [(loader
, 'test_directory' + ' module tests', 'test*')])
133 def test_discover(self
):
134 loader
= unittest
.TestLoader()
136 original_isfile
= os
.path
.isfile
137 original_isdir
= os
.path
.isdir
138 def restore_isfile():
139 os
.path
.isfile
= original_isfile
141 os
.path
.isfile
= lambda path
: False
142 self
.addCleanup(restore_isfile
)
144 orig_sys_path
= sys
.path
[:]
146 sys
.path
[:] = orig_sys_path
147 self
.addCleanup(restore_path
)
149 full_path
= os
.path
.abspath(os
.path
.normpath('/foo'))
150 with self
.assertRaises(ImportError):
151 loader
.discover('/foo/bar', top_level_dir
='/foo')
153 self
.assertEqual(loader
._top
_level
_dir
, full_path
)
154 self
.assertIn(full_path
, sys
.path
)
156 os
.path
.isfile
= lambda path
: True
157 os
.path
.isdir
= lambda path
: True
160 os
.path
.isdir
= original_isdir
161 self
.addCleanup(restore_isdir
)
163 _find_tests_args
= []
164 def _find_tests(start_dir
, pattern
):
165 _find_tests_args
.append((start_dir
, pattern
))
167 loader
._find
_tests
= _find_tests
168 loader
.suiteClass
= str
170 suite
= loader
.discover('/foo/bar/baz', 'pattern', '/foo/bar')
172 top_level_dir
= os
.path
.abspath('/foo/bar')
173 start_dir
= os
.path
.abspath('/foo/bar/baz')
174 self
.assertEqual(suite
, "['tests']")
175 self
.assertEqual(loader
._top
_level
_dir
, top_level_dir
)
176 self
.assertEqual(_find_tests_args
, [(start_dir
, 'pattern')])
177 self
.assertIn(top_level_dir
, sys
.path
)
179 def test_discover_with_modules_that_fail_to_import(self
):
180 loader
= unittest
.TestLoader()
183 os
.listdir
= lambda _
: ['test_this_does_not_exist.py']
184 isfile
= os
.path
.isfile
185 os
.path
.isfile
= lambda _
: True
186 orig_sys_path
= sys
.path
[:]
188 os
.path
.isfile
= isfile
190 sys
.path
[:] = orig_sys_path
191 self
.addCleanup(restore
)
193 suite
= loader
.discover('.')
194 self
.assertIn(os
.getcwd(), sys
.path
)
195 self
.assertEqual(suite
.countTestCases(), 1)
196 test
= list(list(suite
)[0])[0] # extract test from suite
198 with self
.assertRaises(ImportError):
199 test
.test_this_does_not_exist()
201 def test_command_line_handling_parseArgs(self
):
202 # Haha - take that uninstantiable class
203 program
= object.__new
__(unittest
.TestProgram
)
206 def do_discovery(argv
):
208 program
._do
_discovery
= do_discovery
209 program
.parseArgs(['something', 'discover'])
210 self
.assertEqual(args
, [])
212 program
.parseArgs(['something', 'discover', 'foo', 'bar'])
213 self
.assertEqual(args
, ['foo', 'bar'])
215 def test_command_line_handling_do_discovery_too_many_arguments(self
):
216 class Stop(Exception):
221 program
= object.__new
__(unittest
.TestProgram
)
222 program
.usageExit
= usageExit
224 with self
.assertRaises(Stop
):
226 program
._do
_discovery
(['one', 'two', 'three', 'four'])
229 def test_command_line_handling_do_discovery_calls_loader(self
):
230 program
= object.__new
__(unittest
.TestProgram
)
232 class Loader(object):
234 def discover(self
, start_dir
, pattern
, top_level_dir
):
235 self
.args
.append((start_dir
, pattern
, top_level_dir
))
238 program
._do
_discovery
(['-v'], Loader
=Loader
)
239 self
.assertEqual(program
.verbosity
, 2)
240 self
.assertEqual(program
.test
, 'tests')
241 self
.assertEqual(Loader
.args
, [('.', 'test*.py', None)])
244 program
= object.__new
__(unittest
.TestProgram
)
245 program
._do
_discovery
(['--verbose'], Loader
=Loader
)
246 self
.assertEqual(program
.test
, 'tests')
247 self
.assertEqual(Loader
.args
, [('.', 'test*.py', None)])
250 program
= object.__new
__(unittest
.TestProgram
)
251 program
._do
_discovery
([], Loader
=Loader
)
252 self
.assertEqual(program
.test
, 'tests')
253 self
.assertEqual(Loader
.args
, [('.', 'test*.py', None)])
256 program
= object.__new
__(unittest
.TestProgram
)
257 program
._do
_discovery
(['fish'], Loader
=Loader
)
258 self
.assertEqual(program
.test
, 'tests')
259 self
.assertEqual(Loader
.args
, [('fish', 'test*.py', None)])
262 program
= object.__new
__(unittest
.TestProgram
)
263 program
._do
_discovery
(['fish', 'eggs'], Loader
=Loader
)
264 self
.assertEqual(program
.test
, 'tests')
265 self
.assertEqual(Loader
.args
, [('fish', 'eggs', None)])
268 program
= object.__new
__(unittest
.TestProgram
)
269 program
._do
_discovery
(['fish', 'eggs', 'ham'], Loader
=Loader
)
270 self
.assertEqual(program
.test
, 'tests')
271 self
.assertEqual(Loader
.args
, [('fish', 'eggs', 'ham')])
274 program
= object.__new
__(unittest
.TestProgram
)
275 program
._do
_discovery
(['-s', 'fish'], Loader
=Loader
)
276 self
.assertEqual(program
.test
, 'tests')
277 self
.assertEqual(Loader
.args
, [('fish', 'test*.py', None)])
280 program
= object.__new
__(unittest
.TestProgram
)
281 program
._do
_discovery
(['-t', 'fish'], Loader
=Loader
)
282 self
.assertEqual(program
.test
, 'tests')
283 self
.assertEqual(Loader
.args
, [('.', 'test*.py', 'fish')])
286 program
= object.__new
__(unittest
.TestProgram
)
287 program
._do
_discovery
(['-p', 'fish'], Loader
=Loader
)
288 self
.assertEqual(program
.test
, 'tests')
289 self
.assertEqual(Loader
.args
, [('.', 'fish', None)])
290 self
.assertFalse(program
.failfast
)
291 self
.assertFalse(program
.catchbreak
)
294 program
= object.__new
__(unittest
.TestProgram
)
295 program
._do
_discovery
(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
297 self
.assertEqual(program
.test
, 'tests')
298 self
.assertEqual(Loader
.args
, [('fish', 'eggs', None)])
299 self
.assertEqual(program
.verbosity
, 2)
300 self
.assertTrue(program
.failfast
)
301 self
.assertTrue(program
.catchbreak
)
303 def test_detect_module_clash(self
):
304 class Module(object):
305 __file__
= 'bar/foo.py'
306 sys
.modules
['foo'] = Module
307 full_path
= os
.path
.abspath('foo')
308 original_listdir
= os
.listdir
309 original_isfile
= os
.path
.isfile
310 original_isdir
= os
.path
.isdir
313 os
.listdir
= original_listdir
314 os
.path
.isfile
= original_isfile
315 os
.path
.isdir
= original_isdir
316 del sys
.modules
['foo']
317 if full_path
in sys
.path
:
318 sys
.path
.remove(full_path
)
319 self
.addCleanup(cleanup
)
328 os
.path
.isfile
= isfile
329 os
.path
.isdir
= isdir
331 loader
= unittest
.TestLoader()
333 mod_dir
= os
.path
.abspath('bar')
334 expected_dir
= os
.path
.abspath('foo')
335 msg
= re
.escape(r
"'foo' module incorrectly imported from %r. Expected %r. "
336 "Is this module globally installed?" % (mod_dir
, expected_dir
))
337 self
.assertRaisesRegexp(
338 ImportError, '^%s$' % msg
, loader
.discover
,
339 start_dir
='foo', pattern
='foo.py'
341 self
.assertEqual(sys
.path
[0], full_path
)
344 def test_discovery_from_dotted_path(self
):
345 loader
= unittest
.TestLoader()
348 expectedPath
= os
.path
.abspath(os
.path
.dirname(unittest
.test
.__file
__))
351 def _find_tests(start_dir
, pattern
):
353 self
.assertEqual(start_dir
, expectedPath
)
355 loader
._find
_tests
= _find_tests
356 suite
= loader
.discover('unittest.test')
357 self
.assertTrue(self
.wasRun
)
358 self
.assertEqual(suite
._tests
, tests
)
361 if __name__
== '__main__':