Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(2)

Delta Between Two Patch Sets: Lib/unittest/test/test_discovery.py

Issue 16662: load_tests not invoked in package/__init__.py
Left Patch Set: Created 6 years, 6 months ago
Right Patch Set: Created 5 years, 5 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
1 import os 1 import os.path
2 from os.path import abspath
2 import re 3 import re
3 import sys 4 import sys
4 import types 5 import types
5 import builtins 6 import builtins
6 from test import support 7 from test import support
7 8
8 import unittest 9 import unittest
9 import unittest.test 10 import unittest.test
10 11
11 12
(...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 os.listdir = original_listdir 244 os.listdir = original_listdir
244 self.addCleanup(restore_listdir) 245 self.addCleanup(restore_listdir)
245 original_isfile = os.path.isfile 246 original_isfile = os.path.isfile
246 def restore_isfile(): 247 def restore_isfile():
247 os.path.isfile = original_isfile 248 os.path.isfile = original_isfile
248 self.addCleanup(restore_isfile) 249 self.addCleanup(restore_isfile)
249 original_isdir = os.path.isdir 250 original_isdir = os.path.isdir
250 def restore_isdir(): 251 def restore_isdir():
251 os.path.isdir = original_isdir 252 os.path.isdir = original_isdir
252 self.addCleanup(restore_isdir) 253 self.addCleanup(restore_isdir)
253 self.addCleanup(sys.path.remove, '/foo') 254 self.addCleanup(sys.path.remove, abspath('/foo'))
254 255
255 # Test data: we expect the following: 256 # Test data: we expect the following:
256 # a listdir to find our package, and a isfile and isdir check on it. 257 # a listdir to find our package, and a isfile and isdir check on it.
257 # a module-from-name call to turn that into a module 258 # a module-from-name call to turn that into a module
258 # followed by load_tests. 259 # followed by load_tests.
259 # then our load_tests will call discover() which is messy 260 # then our load_tests will call discover() which is messy
260 # but that finally chains into find_tests again for the child dir - 261 # but that finally chains into find_tests again for the child dir -
261 # which is why we don't have a infinite loop. 262 # which is why we don't have a infinite loop.
262 # We expect to see: 263 # We expect to see:
263 # the module load tests for both package and plain module called, 264 # the module load tests for both package and plain module called,
264 # and the plain module result nested by the package module load_tests 265 # and the plain module result nested by the package module load_tests
265 # indicating that it was processed and could have been mutated. 266 # indicating that it was processed and could have been mutated.
266 vfs = {'/foo': ['my_package'], 267 vfs = {abspath('/foo'): ['my_package'],
267 '/foo/my_package': ['__init__.py', 'test_module.py']} 268 abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
268 def list_dir(path): 269 def list_dir(path):
269 return list(vfs[path]) 270 return list(vfs[path])
270 os.listdir = list_dir 271 os.listdir = list_dir
271 os.path.isdir = lambda path: not path.endswith('.py') 272 os.path.isdir = lambda path: not path.endswith('.py')
272 os.path.isfile = lambda path: path.endswith('.py') 273 os.path.isfile = lambda path: path.endswith('.py')
273 274
274 class Module(object): 275 class Module(object):
275 paths = [] 276 paths = []
276 load_tests_args = [] 277 load_tests_args = []
277 278
(...skipping 16 matching lines...) Expand all
294 ] + pkg_tests 295 ] + pkg_tests
295 self.load_tests = load_tests 296 self.load_tests = load_tests
296 297
297 def __eq__(self, other): 298 def __eq__(self, other):
298 return self.path == other.path 299 return self.path == other.path
299 300
300 loader = unittest.TestLoader() 301 loader = unittest.TestLoader()
301 loader._get_module_from_name = lambda name: Module(name) 302 loader._get_module_from_name = lambda name: Module(name)
302 loader.suiteClass = lambda thing: thing 303 loader.suiteClass = lambda thing: thing
303 304
304 loader._top_level_dir = '/foo' 305 loader._top_level_dir = abspath('/foo')
305 # this time no '.py' on the pattern so that it can match 306 # this time no '.py' on the pattern so that it can match
306 # a test package 307 # a test package
307 suite = list(loader._find_tests('/foo', 'test*.py')) 308 suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
308 309
309 # We should have loaded tests from both my_package and 310 # We should have loaded tests from both my_package and
310 # my_pacakge.test_module, and also run the load_tests hook in both. 311 # my_pacakge.test_module, and also run the load_tests hook in both.
311 # (normally this would be nested TestSuites.) 312 # (normally this would be nested TestSuites.)
312 self.assertEqual(suite, 313 self.assertEqual(suite,
313 [['my_package load_tests', [], 314 [['my_package load_tests', [],
314 ['my_package.test_module load_tests']]]) 315 ['my_package.test_module load_tests']]])
315 # Parents before children. 316 # Parents before children.
316 self.assertEqual(Module.paths, 317 self.assertEqual(Module.paths,
317 ['my_package', 'my_package.test_module']) 318 ['my_package', 'my_package.test_module'])
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 398
398 suite = loader.discover('.') 399 suite = loader.discover('.')
399 self.assertIn(os.getcwd(), sys.path) 400 self.assertIn(os.getcwd(), sys.path)
400 self.assertEqual(suite.countTestCases(), 1) 401 self.assertEqual(suite.countTestCases(), 1)
401 test = list(list(suite)[0])[0] # extract test from suite 402 test = list(list(suite)[0])[0] # extract test from suite
402 403
403 with self.assertRaises(ImportError): 404 with self.assertRaises(ImportError):
404 test.test_this_does_not_exist() 405 test.test_this_does_not_exist()
405 406
406 def test_discover_with_init_modules_that_fail_to_import(self): 407 def test_discover_with_init_modules_that_fail_to_import(self):
407 vfs = {'/foo': ['my_package'], 408 vfs = {abspath('/foo'): ['my_package'],
408 '/foo/my_package': ['__init__.py', 'test_module.py']} 409 abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
409 self.setup_import_issue_package_tests(vfs) 410 self.setup_import_issue_package_tests(vfs)
410 import_calls = [] 411 import_calls = []
411 def _get_module_from_name(name): 412 def _get_module_from_name(name):
412 import_calls.append(name) 413 import_calls.append(name)
413 raise ImportError("Cannot import Name") 414 raise ImportError("Cannot import Name")
414 loader = unittest.TestLoader() 415 loader = unittest.TestLoader()
415 loader._get_module_from_name = _get_module_from_name 416 loader._get_module_from_name = _get_module_from_name
416 suite = loader.discover('/foo') 417 suite = loader.discover(abspath('/foo'))
417 418
418 self.assertIn('/foo', sys.path) 419 self.assertIn(abspath('/foo'), sys.path)
419 self.assertEqual(suite.countTestCases(), 1) 420 self.assertEqual(suite.countTestCases(), 1)
420 test = list(list(suite)[0])[0] # extract test from suite 421 test = list(list(suite)[0])[0] # extract test from suite
421 with self.assertRaises(ImportError): 422 with self.assertRaises(ImportError):
422 test.my_package() 423 test.my_package()
423 self.assertEqual(import_calls, ['my_package']) 424 self.assertEqual(import_calls, ['my_package'])
424 425
425 def test_discover_with_module_that_raises_SkipTest_on_import(self): 426 def test_discover_with_module_that_raises_SkipTest_on_import(self):
426 loader = unittest.TestLoader() 427 loader = unittest.TestLoader()
427 428
428 def _get_module_from_name(name): 429 def _get_module_from_name(name):
429 raise unittest.SkipTest('skipperoo') 430 raise unittest.SkipTest('skipperoo')
430 loader._get_module_from_name = _get_module_from_name 431 loader._get_module_from_name = _get_module_from_name
431 432
432 self.setup_import_issue_tests('test_skip_dummy.py') 433 self.setup_import_issue_tests('test_skip_dummy.py')
433 434
434 suite = loader.discover('.') 435 suite = loader.discover('.')
435 self.assertEqual(suite.countTestCases(), 1) 436 self.assertEqual(suite.countTestCases(), 1)
436 437
437 result = unittest.TestResult() 438 result = unittest.TestResult()
438 suite.run(result) 439 suite.run(result)
439 self.assertEqual(len(result.skipped), 1) 440 self.assertEqual(len(result.skipped), 1)
440 441
441 def test_discover_with_init_module_that_raises_SkipTest_on_import(self): 442 def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
442 vfs = {'/foo': ['my_package'], 443 vfs = {abspath('/foo'): ['my_package'],
443 '/foo/my_package': ['__init__.py', 'test_module.py']} 444 abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
444 self.setup_import_issue_package_tests(vfs) 445 self.setup_import_issue_package_tests(vfs)
445 import_calls = [] 446 import_calls = []
446 def _get_module_from_name(name): 447 def _get_module_from_name(name):
447 import_calls.append(name) 448 import_calls.append(name)
448 raise unittest.SkipTest('skipperoo') 449 raise unittest.SkipTest('skipperoo')
449 loader = unittest.TestLoader() 450 loader = unittest.TestLoader()
450 loader._get_module_from_name = _get_module_from_name 451 loader._get_module_from_name = _get_module_from_name
451 suite = loader.discover('/foo') 452 suite = loader.discover(abspath('/foo'))
452 453
453 self.assertIn('/foo', sys.path) 454 self.assertIn(abspath('/foo'), sys.path)
454 self.assertEqual(suite.countTestCases(), 1) 455 self.assertEqual(suite.countTestCases(), 1)
455 result = unittest.TestResult() 456 result = unittest.TestResult()
456 suite.run(result) 457 suite.run(result)
457 self.assertEqual(len(result.skipped), 1) 458 self.assertEqual(len(result.skipped), 1)
458 self.assertEqual(result.testsRun, 1) 459 self.assertEqual(result.testsRun, 1)
459 self.assertEqual(import_calls, ['my_package']) 460 self.assertEqual(import_calls, ['my_package'])
460 461
461 def test_command_line_handling_parseArgs(self): 462 def test_command_line_handling_parseArgs(self):
462 program = TestableTestProgram() 463 program = TestableTestProgram()
463 464
(...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
744 745
745 with self.assertRaises(TypeError) as cm: 746 with self.assertRaises(TypeError) as cm:
746 loader.discover('package') 747 loader.discover('package')
747 self.assertEqual(str(cm.exception), 748 self.assertEqual(str(cm.exception),
748 'don\'t know how to discover from {!r}' 749 'don\'t know how to discover from {!r}'
749 .format(package)) 750 .format(package))
750 751
751 752
752 if __name__ == '__main__': 753 if __name__ == '__main__':
753 unittest.main() 754 unittest.main()
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+