Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
unittest.test.test_discovery.TestDiscovery Class Reference

List of all members.

Public Member Functions

def test_get_name_from_path
def test_find_tests
def test_find_tests_with_package
def test_discover
def test_discover_with_modules_that_fail_to_import
def test_command_line_handling_parseArgs
def test_command_line_handling_discover_by_default
def test_command_line_handling_discover_by_default_with_options
def test_command_line_handling_do_discovery_too_many_arguments
def test_command_line_handling_do_discovery_calls_loader
def test_detect_module_clash
def test_discovery_from_dotted_path

Public Attributes

 path
 load_tests
 called
 wasRun

Detailed Description

Definition at line 20 of file test_discovery.py.


Member Function Documentation

Definition at line 222 of file test_discovery.py.

00222 
00223     def test_command_line_handling_discover_by_default(self):
00224         program = TestableTestProgram()
00225         program.module = None
00226 
00227         self.called = False
00228         def do_discovery(argv):
00229             self.called = True
00230             self.assertEqual(argv, [])
00231         program._do_discovery = do_discovery
00232         program.parseArgs(['something'])
00233         self.assertTrue(self.called)

Definition at line 234 of file test_discovery.py.

00234 
00235     def test_command_line_handling_discover_by_default_with_options(self):
00236         program = TestableTestProgram()
00237         program.module = None
00238 
00239         args = ['something', '-v', '-b', '-v', '-c', '-f']
00240         self.called = False
00241         def do_discovery(argv):
00242             self.called = True
00243             self.assertEqual(argv, args[1:])
00244         program._do_discovery = do_discovery
00245         program.parseArgs(args)
00246         self.assertTrue(self.called)
00247 

Here is the call graph for this function:

Definition at line 262 of file test_discovery.py.

00262 
00263     def test_command_line_handling_do_discovery_calls_loader(self):
00264         program = TestableTestProgram()
00265 
00266         class Loader(object):
00267             args = []
00268             def discover(self, start_dir, pattern, top_level_dir):
00269                 self.args.append((start_dir, pattern, top_level_dir))
00270                 return 'tests'
00271 
00272         program._do_discovery(['-v'], Loader=Loader)
00273         self.assertEqual(program.verbosity, 2)
00274         self.assertEqual(program.test, 'tests')
00275         self.assertEqual(Loader.args, [('.', 'test*.py', None)])
00276 
00277         Loader.args = []
00278         program = TestableTestProgram()
00279         program._do_discovery(['--verbose'], Loader=Loader)
00280         self.assertEqual(program.test, 'tests')
00281         self.assertEqual(Loader.args, [('.', 'test*.py', None)])
00282 
00283         Loader.args = []
00284         program = TestableTestProgram()
00285         program._do_discovery([], Loader=Loader)
00286         self.assertEqual(program.test, 'tests')
00287         self.assertEqual(Loader.args, [('.', 'test*.py', None)])
00288 
00289         Loader.args = []
00290         program = TestableTestProgram()
00291         program._do_discovery(['fish'], Loader=Loader)
00292         self.assertEqual(program.test, 'tests')
00293         self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
00294 
00295         Loader.args = []
00296         program = TestableTestProgram()
00297         program._do_discovery(['fish', 'eggs'], Loader=Loader)
00298         self.assertEqual(program.test, 'tests')
00299         self.assertEqual(Loader.args, [('fish', 'eggs', None)])
00300 
00301         Loader.args = []
00302         program = TestableTestProgram()
00303         program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
00304         self.assertEqual(program.test, 'tests')
00305         self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
00306 
00307         Loader.args = []
00308         program = TestableTestProgram()
00309         program._do_discovery(['-s', 'fish'], Loader=Loader)
00310         self.assertEqual(program.test, 'tests')
00311         self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
00312 
00313         Loader.args = []
00314         program = TestableTestProgram()
00315         program._do_discovery(['-t', 'fish'], Loader=Loader)
00316         self.assertEqual(program.test, 'tests')
00317         self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
00318 
00319         Loader.args = []
00320         program = TestableTestProgram()
00321         program._do_discovery(['-p', 'fish'], Loader=Loader)
00322         self.assertEqual(program.test, 'tests')
00323         self.assertEqual(Loader.args, [('.', 'fish', None)])
00324         self.assertFalse(program.failfast)
00325         self.assertFalse(program.catchbreak)
00326 
00327         Loader.args = []
00328         program = TestableTestProgram()
00329         program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
00330                               Loader=Loader)
00331         self.assertEqual(program.test, 'tests')
00332         self.assertEqual(Loader.args, [('fish', 'eggs', None)])
00333         self.assertEqual(program.verbosity, 2)
00334         self.assertTrue(program.failfast)
00335         self.assertTrue(program.catchbreak)

Here is the call graph for this function:

Definition at line 248 of file test_discovery.py.

00248 
00249     def test_command_line_handling_do_discovery_too_many_arguments(self):
00250         class Stop(Exception):
00251             pass
00252         def usageExit():
00253             raise Stop
00254 
00255         program = TestableTestProgram()
00256         program.usageExit = usageExit
00257 
00258         with self.assertRaises(Stop):
00259             # too many args
00260             program._do_discovery(['one', 'two', 'three', 'four'])
00261 

Here is the call graph for this function:

Definition at line 209 of file test_discovery.py.

00209 
00210     def test_command_line_handling_parseArgs(self):
00211         program = TestableTestProgram()
00212 
00213         args = []
00214         def do_discovery(argv):
00215             args.extend(argv)
00216         program._do_discovery = do_discovery
00217         program.parseArgs(['something', 'discover'])
00218         self.assertEqual(args, [])
00219 
00220         program.parseArgs(['something', 'discover', 'foo', 'bar'])
00221         self.assertEqual(args, ['foo', 'bar'])

Here is the call graph for this function:

Definition at line 336 of file test_discovery.py.

00336 
00337     def test_detect_module_clash(self):
00338         class Module(object):
00339             __file__ = 'bar/foo.py'
00340         sys.modules['foo'] = Module
00341         full_path = os.path.abspath('foo')
00342         original_listdir = os.listdir
00343         original_isfile = os.path.isfile
00344         original_isdir = os.path.isdir
00345 
00346         def cleanup():
00347             os.listdir = original_listdir
00348             os.path.isfile = original_isfile
00349             os.path.isdir = original_isdir
00350             del sys.modules['foo']
00351             if full_path in sys.path:
00352                 sys.path.remove(full_path)
00353         self.addCleanup(cleanup)
00354 
00355         def listdir(_):
00356             return ['foo.py']
00357         def isfile(_):
00358             return True
00359         def isdir(_):
00360             return True
00361         os.listdir = listdir
00362         os.path.isfile = isfile
00363         os.path.isdir = isdir
00364 
00365         loader = unittest.TestLoader()
00366 
00367         mod_dir = os.path.abspath('bar')
00368         expected_dir = os.path.abspath('foo')
00369         msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. "
00370                 "Is this module globally installed?" % (mod_dir, expected_dir))
00371         self.assertRaisesRegex(
00372             ImportError, '^%s$' % msg, loader.discover,
00373             start_dir='foo', pattern='foo.py'
00374         )
00375         self.assertEqual(sys.path[0], full_path)
00376 

Here is the call graph for this function:

Definition at line 141 of file test_discovery.py.

00141 
00142     def test_discover(self):
00143         loader = unittest.TestLoader()
00144 
00145         original_isfile = os.path.isfile
00146         original_isdir = os.path.isdir
00147         def restore_isfile():
00148             os.path.isfile = original_isfile
00149 
00150         os.path.isfile = lambda path: False
00151         self.addCleanup(restore_isfile)
00152 
00153         orig_sys_path = sys.path[:]
00154         def restore_path():
00155             sys.path[:] = orig_sys_path
00156         self.addCleanup(restore_path)
00157 
00158         full_path = os.path.abspath(os.path.normpath('/foo'))
00159         with self.assertRaises(ImportError):
00160             loader.discover('/foo/bar', top_level_dir='/foo')
00161 
00162         self.assertEqual(loader._top_level_dir, full_path)
00163         self.assertIn(full_path, sys.path)
00164 
00165         os.path.isfile = lambda path: True
00166         os.path.isdir = lambda path: True
00167 
00168         def restore_isdir():
00169             os.path.isdir = original_isdir
00170         self.addCleanup(restore_isdir)
00171 
00172         _find_tests_args = []
00173         def _find_tests(start_dir, pattern):
00174             _find_tests_args.append((start_dir, pattern))
00175             return ['tests']
00176         loader._find_tests = _find_tests
00177         loader.suiteClass = str
00178 
00179         suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')
00180 
00181         top_level_dir = os.path.abspath('/foo/bar')
00182         start_dir = os.path.abspath('/foo/bar/baz')
00183         self.assertEqual(suite, "['tests']")
00184         self.assertEqual(loader._top_level_dir, top_level_dir)
00185         self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
00186         self.assertIn(top_level_dir, sys.path)

Here is the call graph for this function:

Definition at line 187 of file test_discovery.py.

00187 
00188     def test_discover_with_modules_that_fail_to_import(self):
00189         loader = unittest.TestLoader()
00190 
00191         listdir = os.listdir
00192         os.listdir = lambda _: ['test_this_does_not_exist.py']
00193         isfile = os.path.isfile
00194         os.path.isfile = lambda _: True
00195         orig_sys_path = sys.path[:]
00196         def restore():
00197             os.path.isfile = isfile
00198             os.listdir = listdir
00199             sys.path[:] = orig_sys_path
00200         self.addCleanup(restore)
00201 
00202         suite = loader.discover('.')
00203         self.assertIn(os.getcwd(), sys.path)
00204         self.assertEqual(suite.countTestCases(), 1)
00205         test = list(list(suite)[0])[0] # extract test from suite
00206 
00207         with self.assertRaises(ImportError):
00208             test.test_this_does_not_exist()

Here is the call graph for this function:

Definition at line 377 of file test_discovery.py.

00377 
00378     def test_discovery_from_dotted_path(self):
00379         loader = unittest.TestLoader()
00380 
00381         tests = [self]
00382         expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
00383 
00384         self.wasRun = False
00385         def _find_tests(start_dir, pattern):
00386             self.wasRun = True
00387             self.assertEqual(start_dir, expectedPath)
00388             return tests
00389         loader._find_tests = _find_tests
00390         suite = loader.discover('unittest.test')
00391         self.assertTrue(self.wasRun)
00392         self.assertEqual(suite._tests, tests)
00393 

Definition at line 36 of file test_discovery.py.

00036 
00037     def test_find_tests(self):
00038         loader = unittest.TestLoader()
00039 
00040         original_listdir = os.listdir
00041         def restore_listdir():
00042             os.listdir = original_listdir
00043         original_isfile = os.path.isfile
00044         def restore_isfile():
00045             os.path.isfile = original_isfile
00046         original_isdir = os.path.isdir
00047         def restore_isdir():
00048             os.path.isdir = original_isdir
00049 
00050         path_lists = [['test1.py', 'test2.py', 'not_a_test.py', 'test_dir',
00051                        'test.foo', 'test-not-a-module.py', 'another_dir'],
00052                       ['test3.py', 'test4.py', ]]
00053         os.listdir = lambda path: path_lists.pop(0)
00054         self.addCleanup(restore_listdir)
00055 
00056         def isdir(path):
00057             return path.endswith('dir')
00058         os.path.isdir = isdir
00059         self.addCleanup(restore_isdir)
00060 
00061         def isfile(path):
00062             # another_dir is not a package and so shouldn't be recursed into
00063             return not path.endswith('dir') and not 'another_dir' in path
00064         os.path.isfile = isfile
00065         self.addCleanup(restore_isfile)
00066 
00067         loader._get_module_from_name = lambda path: path + ' module'
00068         loader.loadTestsFromModule = lambda module: module + ' tests'
00069 
00070         top_level = os.path.abspath('/foo')
00071         loader._top_level_dir = top_level
00072         suite = list(loader._find_tests(top_level, 'test*.py'))
00073 
00074         expected = [name + ' module tests' for name in
00075                     ('test1', 'test2')]
00076         expected.extend([('test_dir.%s' % name) + ' module tests' for name in
00077                     ('test3', 'test4')])
00078         self.assertEqual(suite, expected)

Here is the call graph for this function:

Definition at line 79 of file test_discovery.py.

00079 
00080     def test_find_tests_with_package(self):
00081         loader = unittest.TestLoader()
00082 
00083         original_listdir = os.listdir
00084         def restore_listdir():
00085             os.listdir = original_listdir
00086         original_isfile = os.path.isfile
00087         def restore_isfile():
00088             os.path.isfile = original_isfile
00089         original_isdir = os.path.isdir
00090         def restore_isdir():
00091             os.path.isdir = original_isdir
00092 
00093         directories = ['a_directory', 'test_directory', 'test_directory2']
00094         path_lists = [directories, [], [], []]
00095         os.listdir = lambda path: path_lists.pop(0)
00096         self.addCleanup(restore_listdir)
00097 
00098         os.path.isdir = lambda path: True
00099         self.addCleanup(restore_isdir)
00100 
00101         os.path.isfile = lambda path: os.path.basename(path) not in directories
00102         self.addCleanup(restore_isfile)
00103 
00104         class Module(object):
00105             paths = []
00106             load_tests_args = []
00107 
00108             def __init__(self, path):
00109                 self.path = path
00110                 self.paths.append(path)
00111                 if os.path.basename(path) == 'test_directory':
00112                     def load_tests(loader, tests, pattern):
00113                         self.load_tests_args.append((loader, tests, pattern))
00114                         return 'load_tests'
00115                     self.load_tests = load_tests
00116 
00117             def __eq__(self, other):
00118                 return self.path == other.path
00119 
00120         loader._get_module_from_name = lambda name: Module(name)
00121         def loadTestsFromModule(module, use_load_tests):
00122             if use_load_tests:
00123                 raise self.failureException('use_load_tests should be False for packages')
00124             return module.path + ' module tests'
00125         loader.loadTestsFromModule = loadTestsFromModule
00126 
00127         loader._top_level_dir = '/foo'
00128         # this time no '.py' on the pattern so that it can match
00129         # a test package
00130         suite = list(loader._find_tests('/foo', 'test*'))
00131 
00132         # We should have loaded tests from the test_directory package by calling load_tests
00133         # and directly from the test_directory2 package
00134         self.assertEqual(suite,
00135                          ['load_tests', 'test_directory2' + ' module tests'])
00136         self.assertEqual(Module.paths, ['test_directory', 'test_directory2'])
00137 
00138         # load_tests should have been called once with loader, tests and pattern
00139         self.assertEqual(Module.load_tests_args,
00140                          [(loader, 'test_directory' + ' module tests', 'test*')])

Here is the call graph for this function:

Definition at line 23 of file test_discovery.py.

00023 
00024     def test_get_name_from_path(self):
00025         loader = unittest.TestLoader()
00026         loader._top_level_dir = '/foo'
00027         name = loader._get_name_from_path('/foo/bar/baz.py')
00028         self.assertEqual(name, 'bar.baz')
00029 
00030         if not __debug__:
00031             # asserts are off
00032             return
00033 
00034         with self.assertRaises(AssertionError):
00035             loader._get_name_from_path('/bar/baz.py')

Here is the call graph for this function:


Member Data Documentation

Definition at line 226 of file test_discovery.py.

Definition at line 114 of file test_discovery.py.

Definition at line 108 of file test_discovery.py.

Definition at line 383 of file test_discovery.py.


The documentation for this class was generated from the following file: