(self)
| 43 | loader._get_name_from_path('/bar/baz.py') |
| 44 | |
| 45 | def test_find_tests(self): |
| 46 | loader = unittest.TestLoader() |
| 47 | |
| 48 | original_listdir = os.listdir |
| 49 | def restore_listdir(): |
| 50 | os.listdir = original_listdir |
| 51 | original_isfile = os.path.isfile |
| 52 | def restore_isfile(): |
| 53 | os.path.isfile = original_isfile |
| 54 | original_isdir = os.path.isdir |
| 55 | def restore_isdir(): |
| 56 | os.path.isdir = original_isdir |
| 57 | |
| 58 | path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir', |
| 59 | 'test.foo', 'test-not-a-module.py', 'another_dir'], |
| 60 | ['test4.py', 'test3.py', ]] |
| 61 | os.listdir = lambda path: path_lists.pop(0) |
| 62 | self.addCleanup(restore_listdir) |
| 63 | |
| 64 | def isdir(path): |
| 65 | return path.endswith('dir') |
| 66 | os.path.isdir = isdir |
| 67 | self.addCleanup(restore_isdir) |
| 68 | |
| 69 | def isfile(path): |
| 70 | # another_dir is not a package and so shouldn't be recursed into |
| 71 | return not path.endswith('dir') and not 'another_dir' in path |
| 72 | os.path.isfile = isfile |
| 73 | self.addCleanup(restore_isfile) |
| 74 | |
| 75 | loader._get_module_from_name = lambda path: path + ' module' |
| 76 | orig_load_tests = loader.loadTestsFromModule |
| 77 | def loadTestsFromModule(module, pattern=None): |
| 78 | # This is where load_tests is called. |
| 79 | base = orig_load_tests(module, pattern=pattern) |
| 80 | return base + [module + ' tests'] |
| 81 | loader.loadTestsFromModule = loadTestsFromModule |
| 82 | loader.suiteClass = lambda thing: thing |
| 83 | |
| 84 | top_level = os.path.abspath('/foo') |
| 85 | loader._top_level_dir = top_level |
| 86 | suite = list(loader._find_tests(top_level, 'test*.py')) |
| 87 | |
| 88 | # The test suites found should be sorted alphabetically for reliable |
| 89 | # execution order. |
| 90 | expected = [[name + ' module tests'] for name in |
| 91 | ('test1', 'test2', 'test_dir')] |
| 92 | expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in |
| 93 | ('test3', 'test4')]) |
| 94 | self.assertEqual(suite, expected) |
| 95 | |
| 96 | def test_find_tests_socket(self): |
| 97 | # A socket is neither a directory nor a regular file. |
nothing calls this directly
no test coverage detected