diff options
author | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-09-13 19:07:03 (GMT) |
---|---|---|
committer | Michael Foord <fuzzyman@voidspace.org.uk> | 2009-09-13 19:07:03 (GMT) |
commit | e91ea56b307e026ba40140305d8f35f0e1c1143a (patch) | |
tree | 9fc09481fe1ff2b21bdfd56b25860a4e25a7cbd1 /Lib/test/test_unittest.py | |
parent | 6848d82a7c90ccf28ba0991129f99675e7b78e24 (diff) | |
download | cpython-e91ea56b307e026ba40140305d8f35f0e1c1143a.zip cpython-e91ea56b307e026ba40140305d8f35f0e1c1143a.tar.gz cpython-e91ea56b307e026ba40140305d8f35f0e1c1143a.tar.bz2 |
Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
Diffstat (limited to 'Lib/test/test_unittest.py')
-rw-r--r-- | Lib/test/test_unittest.py | 62 |
1 files changed, 33 insertions, 29 deletions
diff --git a/Lib/test/test_unittest.py b/Lib/test/test_unittest.py index 79ee982..459334b 100644 --- a/Lib/test/test_unittest.py +++ b/Lib/test/test_unittest.py @@ -3469,31 +3469,19 @@ class Test_TextTestRunner(TestCase): class TestDiscovery(TestCase): # Heavily mocked tests so I can avoid hitting the filesystem - def test_get_module_from_path(self): + def test_get_name_from_path(self): loader = unittest.TestLoader() - old_import = __import__ - def restore_import(): - __builtin__.__import__ = old_import - __builtin__.__import__ = lambda *_: None - self.addCleanup(restore_import) - - expected_module = object() - def del_module(): - del sys.modules['bar.baz'] - sys.modules['bar.baz'] = expected_module - self.addCleanup(del_module) - loader._top_level_dir = '/foo' - module = loader._get_module_from_path('/foo/bar/baz.py') - self.assertEqual(module, expected_module) + name = loader._get_name_from_path('/foo/bar/baz.py') + self.assertEqual(name, 'bar.baz') if not __debug__: # asserts are off return with self.assertRaises(AssertionError): - loader._get_module_from_path('/bar/baz.py') + loader._get_name_from_path('/bar/baz.py') def test_find_tests(self): loader = unittest.TestLoader() @@ -3509,7 +3497,7 @@ class TestDiscovery(TestCase): os.path.isdir = original_isdir path_lists = [['test1.py', 'test2.py', 'not_a_test.py', 'test_dir', - 'test.foo', 'another_dir'], + 'test.foo', 'test-not-a-module.py', 'another_dir'], ['test3.py', 'test4.py', ]] os.listdir = lambda path: path_lists.pop(0) self.addCleanup(restore_listdir) @@ -3525,16 +3513,16 @@ class TestDiscovery(TestCase): os.path.isfile = isfile self.addCleanup(restore_isfile) - loader._get_module_from_path = lambda path: path + ' module' + loader._get_module_from_name = lambda path: path + ' module' loader.loadTestsFromModule = lambda module: module + ' tests' loader._top_level_dir = '/foo' suite = list(loader._find_tests('/foo', 'test*.py')) - expected = [os.path.join('/foo', name) + ' module tests' for name in - ('test1.py', 'test2.py')] - expected.extend([os.path.join('/foo', 'test_dir', name) + ' module tests' for name in - ('test3.py', 'test4.py')]) + expected = [name + ' module tests' for name in + ('test1', 'test2')] + expected.extend([('test_dir.%s' % name) + ' module tests' for name in + ('test3', 'test4')]) self.assertEqual(suite, expected) def test_find_tests_with_package(self): @@ -3577,7 +3565,7 @@ class TestDiscovery(TestCase): def __eq__(self, other): return self.path == other.path - loader._get_module_from_path = lambda path: Module(path) + loader._get_module_from_name = lambda name: Module(name) def loadTestsFromModule(module, use_load_tests): if use_load_tests: raise self.failureException('use_load_tests should be False for packages') @@ -3592,15 +3580,12 @@ class TestDiscovery(TestCase): # We should have loaded tests from the test_directory package by calling load_tests # and directly from the test_directory2 package self.assertEqual(suite, - ['load_tests', - os.path.join('/foo', 'test_directory2') + ' module tests']) - self.assertEqual(Module.paths, [os.path.join('/foo', 'test_directory'), - os.path.join('/foo', 'test_directory2')]) + ['load_tests', 'test_directory2' + ' module tests']) + self.assertEqual(Module.paths, ['test_directory', 'test_directory2']) # load_tests should have been called once with loader, tests and pattern self.assertEqual(Module.load_tests_args, - [(loader, os.path.join('/foo', 'test_directory') + ' module tests', - 'test*')]) + [(loader, 'test_directory' + ' module tests', 'test*')]) def test_discover(self): loader = unittest.TestLoader() @@ -3640,6 +3625,25 @@ class TestDiscovery(TestCase): self.assertEqual(loader._top_level_dir, top_level_dir) self.assertEqual(_find_tests_args, [(start_dir, 'pattern')]) + def test_discover_with_modules_that_fail_to_import(self): + loader = unittest.TestLoader() + + listdir = os.listdir + os.listdir = lambda _: ['test_this_does_not_exist.py'] + isfile = os.path.isfile + os.path.isfile = lambda _: True + def restore(): + os.path.isfile = isfile + os.listdir = listdir + self.addCleanup(restore) + + suite = loader.discover('.') + self.assertEqual(suite.countTestCases(), 1) + test = list(list(suite)[0])[0] # extract test from suite + + with self.assertRaises(ImportError): + test.test_this_does_not_exist() + def test_command_line_handling_parseArgs(self): # Haha - take that uninstantiable class program = object.__new__(TestProgram) |