test_discovery.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. import os.path
  2. from os.path import abspath
  3. import re
  4. import sys
  5. import types
  6. import pickle
  7. from test import support
  8. import test.test_importlib.util
  9. import unittest
  10. import unittest.mock
  11. import unittest.test
  12. class TestableTestProgram(unittest.TestProgram):
  13. module = None
  14. exit = True
  15. defaultTest = failfast = catchbreak = buffer = None
  16. verbosity = 1
  17. progName = ''
  18. testRunner = testLoader = None
  19. def __init__(self):
  20. pass
  21. class TestDiscovery(unittest.TestCase):
  22. # Heavily mocked tests so I can avoid hitting the filesystem
  23. def test_get_name_from_path(self):
  24. loader = unittest.TestLoader()
  25. loader._top_level_dir = '/foo'
  26. name = loader._get_name_from_path('/foo/bar/baz.py')
  27. self.assertEqual(name, 'bar.baz')
  28. if not __debug__:
  29. # asserts are off
  30. return
  31. with self.assertRaises(AssertionError):
  32. loader._get_name_from_path('/bar/baz.py')
  33. def test_find_tests(self):
  34. loader = unittest.TestLoader()
  35. original_listdir = os.listdir
  36. def restore_listdir():
  37. os.listdir = original_listdir
  38. original_isfile = os.path.isfile
  39. def restore_isfile():
  40. os.path.isfile = original_isfile
  41. original_isdir = os.path.isdir
  42. def restore_isdir():
  43. os.path.isdir = original_isdir
  44. path_lists = [['test2.py', 'test1.py', 'not_a_test.py', 'test_dir',
  45. 'test.foo', 'test-not-a-module.py', 'another_dir'],
  46. ['test4.py', 'test3.py', ]]
  47. os.listdir = lambda path: path_lists.pop(0)
  48. self.addCleanup(restore_listdir)
  49. def isdir(path):
  50. return path.endswith('dir')
  51. os.path.isdir = isdir
  52. self.addCleanup(restore_isdir)
  53. def isfile(path):
  54. # another_dir is not a package and so shouldn't be recursed into
  55. return not path.endswith('dir') and not 'another_dir' in path
  56. os.path.isfile = isfile
  57. self.addCleanup(restore_isfile)
  58. loader._get_module_from_name = lambda path: path + ' module'
  59. orig_load_tests = loader.loadTestsFromModule
  60. def loadTestsFromModule(module, pattern=None):
  61. # This is where load_tests is called.
  62. base = orig_load_tests(module, pattern=pattern)
  63. return base + [module + ' tests']
  64. loader.loadTestsFromModule = loadTestsFromModule
  65. loader.suiteClass = lambda thing: thing
  66. top_level = os.path.abspath('/foo')
  67. loader._top_level_dir = top_level
  68. suite = list(loader._find_tests(top_level, 'test*.py'))
  69. # The test suites found should be sorted alphabetically for reliable
  70. # execution order.
  71. expected = [[name + ' module tests'] for name in
  72. ('test1', 'test2', 'test_dir')]
  73. expected.extend([[('test_dir.%s' % name) + ' module tests'] for name in
  74. ('test3', 'test4')])
  75. self.assertEqual(suite, expected)
  76. def test_find_tests_socket(self):
  77. # A socket is neither a directory nor a regular file.
  78. # https://bugs.python.org/issue25320
  79. loader = unittest.TestLoader()
  80. original_listdir = os.listdir
  81. def restore_listdir():
  82. os.listdir = original_listdir
  83. original_isfile = os.path.isfile
  84. def restore_isfile():
  85. os.path.isfile = original_isfile
  86. original_isdir = os.path.isdir
  87. def restore_isdir():
  88. os.path.isdir = original_isdir
  89. path_lists = [['socket']]
  90. os.listdir = lambda path: path_lists.pop(0)
  91. self.addCleanup(restore_listdir)
  92. os.path.isdir = lambda path: False
  93. self.addCleanup(restore_isdir)
  94. os.path.isfile = lambda path: False
  95. self.addCleanup(restore_isfile)
  96. loader._get_module_from_name = lambda path: path + ' module'
  97. orig_load_tests = loader.loadTestsFromModule
  98. def loadTestsFromModule(module, pattern=None):
  99. # This is where load_tests is called.
  100. base = orig_load_tests(module, pattern=pattern)
  101. return base + [module + ' tests']
  102. loader.loadTestsFromModule = loadTestsFromModule
  103. loader.suiteClass = lambda thing: thing
  104. top_level = os.path.abspath('/foo')
  105. loader._top_level_dir = top_level
  106. suite = list(loader._find_tests(top_level, 'test*.py'))
  107. self.assertEqual(suite, [])
  108. def test_find_tests_with_package(self):
  109. loader = unittest.TestLoader()
  110. original_listdir = os.listdir
  111. def restore_listdir():
  112. os.listdir = original_listdir
  113. original_isfile = os.path.isfile
  114. def restore_isfile():
  115. os.path.isfile = original_isfile
  116. original_isdir = os.path.isdir
  117. def restore_isdir():
  118. os.path.isdir = original_isdir
  119. directories = ['a_directory', 'test_directory', 'test_directory2']
  120. path_lists = [directories, [], [], []]
  121. os.listdir = lambda path: path_lists.pop(0)
  122. self.addCleanup(restore_listdir)
  123. os.path.isdir = lambda path: True
  124. self.addCleanup(restore_isdir)
  125. os.path.isfile = lambda path: os.path.basename(path) not in directories
  126. self.addCleanup(restore_isfile)
  127. class Module(object):
  128. paths = []
  129. load_tests_args = []
  130. def __init__(self, path):
  131. self.path = path
  132. self.paths.append(path)
  133. if os.path.basename(path) == 'test_directory':
  134. def load_tests(loader, tests, pattern):
  135. self.load_tests_args.append((loader, tests, pattern))
  136. return [self.path + ' load_tests']
  137. self.load_tests = load_tests
  138. def __eq__(self, other):
  139. return self.path == other.path
  140. loader._get_module_from_name = lambda name: Module(name)
  141. orig_load_tests = loader.loadTestsFromModule
  142. def loadTestsFromModule(module, pattern=None):
  143. # This is where load_tests is called.
  144. base = orig_load_tests(module, pattern=pattern)
  145. return base + [module.path + ' module tests']
  146. loader.loadTestsFromModule = loadTestsFromModule
  147. loader.suiteClass = lambda thing: thing
  148. loader._top_level_dir = '/foo'
  149. # this time no '.py' on the pattern so that it can match
  150. # a test package
  151. suite = list(loader._find_tests('/foo', 'test*'))
  152. # We should have loaded tests from the a_directory and test_directory2
  153. # directly and via load_tests for the test_directory package, which
  154. # still calls the baseline module loader.
  155. self.assertEqual(suite,
  156. [['a_directory module tests'],
  157. ['test_directory load_tests',
  158. 'test_directory module tests'],
  159. ['test_directory2 module tests']])
  160. # The test module paths should be sorted for reliable execution order
  161. self.assertEqual(Module.paths,
  162. ['a_directory', 'test_directory', 'test_directory2'])
  163. # load_tests should have been called once with loader, tests and pattern
  164. # (but there are no tests in our stub module itself, so that is [] at
  165. # the time of call).
  166. self.assertEqual(Module.load_tests_args,
  167. [(loader, [], 'test*')])
  168. def test_find_tests_default_calls_package_load_tests(self):
  169. loader = unittest.TestLoader()
  170. original_listdir = os.listdir
  171. def restore_listdir():
  172. os.listdir = original_listdir
  173. original_isfile = os.path.isfile
  174. def restore_isfile():
  175. os.path.isfile = original_isfile
  176. original_isdir = os.path.isdir
  177. def restore_isdir():
  178. os.path.isdir = original_isdir
  179. directories = ['a_directory', 'test_directory', 'test_directory2']
  180. path_lists = [directories, [], [], []]
  181. os.listdir = lambda path: path_lists.pop(0)
  182. self.addCleanup(restore_listdir)
  183. os.path.isdir = lambda path: True
  184. self.addCleanup(restore_isdir)
  185. os.path.isfile = lambda path: os.path.basename(path) not in directories
  186. self.addCleanup(restore_isfile)
  187. class Module(object):
  188. paths = []
  189. load_tests_args = []
  190. def __init__(self, path):
  191. self.path = path
  192. self.paths.append(path)
  193. if os.path.basename(path) == 'test_directory':
  194. def load_tests(loader, tests, pattern):
  195. self.load_tests_args.append((loader, tests, pattern))
  196. return [self.path + ' load_tests']
  197. self.load_tests = load_tests
  198. def __eq__(self, other):
  199. return self.path == other.path
  200. loader._get_module_from_name = lambda name: Module(name)
  201. orig_load_tests = loader.loadTestsFromModule
  202. def loadTestsFromModule(module, pattern=None):
  203. # This is where load_tests is called.
  204. base = orig_load_tests(module, pattern=pattern)
  205. return base + [module.path + ' module tests']
  206. loader.loadTestsFromModule = loadTestsFromModule
  207. loader.suiteClass = lambda thing: thing
  208. loader._top_level_dir = '/foo'
  209. # this time no '.py' on the pattern so that it can match
  210. # a test package
  211. suite = list(loader._find_tests('/foo', 'test*.py'))
  212. # We should have loaded tests from the a_directory and test_directory2
  213. # directly and via load_tests for the test_directory package, which
  214. # still calls the baseline module loader.
  215. self.assertEqual(suite,
  216. [['a_directory module tests'],
  217. ['test_directory load_tests',
  218. 'test_directory module tests'],
  219. ['test_directory2 module tests']])
  220. # The test module paths should be sorted for reliable execution order
  221. self.assertEqual(Module.paths,
  222. ['a_directory', 'test_directory', 'test_directory2'])
  223. # load_tests should have been called once with loader, tests and pattern
  224. self.assertEqual(Module.load_tests_args,
  225. [(loader, [], 'test*.py')])
  226. def test_find_tests_customize_via_package_pattern(self):
  227. # This test uses the example 'do-nothing' load_tests from
  228. # https://docs.python.org/3/library/unittest.html#load-tests-protocol
  229. # to make sure that that actually works.
  230. # Housekeeping
  231. original_listdir = os.listdir
  232. def restore_listdir():
  233. os.listdir = original_listdir
  234. self.addCleanup(restore_listdir)
  235. original_isfile = os.path.isfile
  236. def restore_isfile():
  237. os.path.isfile = original_isfile
  238. self.addCleanup(restore_isfile)
  239. original_isdir = os.path.isdir
  240. def restore_isdir():
  241. os.path.isdir = original_isdir
  242. self.addCleanup(restore_isdir)
  243. self.addCleanup(sys.path.remove, abspath('/foo'))
  244. # Test data: we expect the following:
  245. # a listdir to find our package, and isfile and isdir checks on it.
  246. # a module-from-name call to turn that into a module
  247. # followed by load_tests.
  248. # then our load_tests will call discover() which is messy
  249. # but that finally chains into find_tests again for the child dir -
  250. # which is why we don't have an infinite loop.
  251. # We expect to see:
  252. # the module load tests for both package and plain module called,
  253. # and the plain module result nested by the package module load_tests
  254. # indicating that it was processed and could have been mutated.
  255. vfs = {abspath('/foo'): ['my_package'],
  256. abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
  257. def list_dir(path):
  258. return list(vfs[path])
  259. os.listdir = list_dir
  260. os.path.isdir = lambda path: not path.endswith('.py')
  261. os.path.isfile = lambda path: path.endswith('.py')
  262. class Module(object):
  263. paths = []
  264. load_tests_args = []
  265. def __init__(self, path):
  266. self.path = path
  267. self.paths.append(path)
  268. if path.endswith('test_module'):
  269. def load_tests(loader, tests, pattern):
  270. self.load_tests_args.append((loader, tests, pattern))
  271. return [self.path + ' load_tests']
  272. else:
  273. def load_tests(loader, tests, pattern):
  274. self.load_tests_args.append((loader, tests, pattern))
  275. # top level directory cached on loader instance
  276. __file__ = '/foo/my_package/__init__.py'
  277. this_dir = os.path.dirname(__file__)
  278. pkg_tests = loader.discover(
  279. start_dir=this_dir, pattern=pattern)
  280. return [self.path + ' load_tests', tests
  281. ] + pkg_tests
  282. self.load_tests = load_tests
  283. def __eq__(self, other):
  284. return self.path == other.path
  285. loader = unittest.TestLoader()
  286. loader._get_module_from_name = lambda name: Module(name)
  287. loader.suiteClass = lambda thing: thing
  288. loader._top_level_dir = abspath('/foo')
  289. # this time no '.py' on the pattern so that it can match
  290. # a test package
  291. suite = list(loader._find_tests(abspath('/foo'), 'test*.py'))
  292. # We should have loaded tests from both my_package and
  293. # my_package.test_module, and also run the load_tests hook in both.
  294. # (normally this would be nested TestSuites.)
  295. self.assertEqual(suite,
  296. [['my_package load_tests', [],
  297. ['my_package.test_module load_tests']]])
  298. # Parents before children.
  299. self.assertEqual(Module.paths,
  300. ['my_package', 'my_package.test_module'])
  301. # load_tests should have been called twice with loader, tests and pattern
  302. self.assertEqual(Module.load_tests_args,
  303. [(loader, [], 'test*.py'),
  304. (loader, [], 'test*.py')])
  305. def test_discover(self):
  306. loader = unittest.TestLoader()
  307. original_isfile = os.path.isfile
  308. original_isdir = os.path.isdir
  309. def restore_isfile():
  310. os.path.isfile = original_isfile
  311. os.path.isfile = lambda path: False
  312. self.addCleanup(restore_isfile)
  313. orig_sys_path = sys.path[:]
  314. def restore_path():
  315. sys.path[:] = orig_sys_path
  316. self.addCleanup(restore_path)
  317. full_path = os.path.abspath(os.path.normpath('/foo'))
  318. with self.assertRaises(ImportError):
  319. loader.discover('/foo/bar', top_level_dir='/foo')
  320. self.assertEqual(loader._top_level_dir, full_path)
  321. self.assertIn(full_path, sys.path)
  322. os.path.isfile = lambda path: True
  323. os.path.isdir = lambda path: True
  324. def restore_isdir():
  325. os.path.isdir = original_isdir
  326. self.addCleanup(restore_isdir)
  327. _find_tests_args = []
  328. def _find_tests(start_dir, pattern, namespace=None):
  329. _find_tests_args.append((start_dir, pattern))
  330. return ['tests']
  331. loader._find_tests = _find_tests
  332. loader.suiteClass = str
  333. suite = loader.discover('/foo/bar/baz', 'pattern', '/foo/bar')
  334. top_level_dir = os.path.abspath('/foo/bar')
  335. start_dir = os.path.abspath('/foo/bar/baz')
  336. self.assertEqual(suite, "['tests']")
  337. self.assertEqual(loader._top_level_dir, top_level_dir)
  338. self.assertEqual(_find_tests_args, [(start_dir, 'pattern')])
  339. self.assertIn(top_level_dir, sys.path)
  340. def test_discover_start_dir_is_package_calls_package_load_tests(self):
  341. # This test verifies that the package load_tests in a package is indeed
  342. # invoked when the start_dir is a package (and not the top level).
  343. # http://bugs.python.org/issue22457
  344. # Test data: we expect the following:
  345. # an isfile to verify the package, then importing and scanning
  346. # as per _find_tests' normal behaviour.
  347. # We expect to see our load_tests hook called once.
  348. vfs = {abspath('/toplevel'): ['startdir'],
  349. abspath('/toplevel/startdir'): ['__init__.py']}
  350. def list_dir(path):
  351. return list(vfs[path])
  352. self.addCleanup(setattr, os, 'listdir', os.listdir)
  353. os.listdir = list_dir
  354. self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
  355. os.path.isfile = lambda path: path.endswith('.py')
  356. self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
  357. os.path.isdir = lambda path: not path.endswith('.py')
  358. self.addCleanup(sys.path.remove, abspath('/toplevel'))
  359. class Module(object):
  360. paths = []
  361. load_tests_args = []
  362. def __init__(self, path):
  363. self.path = path
  364. def load_tests(self, loader, tests, pattern):
  365. return ['load_tests called ' + self.path]
  366. def __eq__(self, other):
  367. return self.path == other.path
  368. loader = unittest.TestLoader()
  369. loader._get_module_from_name = lambda name: Module(name)
  370. loader.suiteClass = lambda thing: thing
  371. suite = loader.discover('/toplevel/startdir', top_level_dir='/toplevel')
  372. # We should have loaded tests from the package __init__.
  373. # (normally this would be nested TestSuites.)
  374. self.assertEqual(suite,
  375. [['load_tests called startdir']])
  376. def setup_import_issue_tests(self, fakefile):
  377. listdir = os.listdir
  378. os.listdir = lambda _: [fakefile]
  379. isfile = os.path.isfile
  380. os.path.isfile = lambda _: True
  381. orig_sys_path = sys.path[:]
  382. def restore():
  383. os.path.isfile = isfile
  384. os.listdir = listdir
  385. sys.path[:] = orig_sys_path
  386. self.addCleanup(restore)
  387. def setup_import_issue_package_tests(self, vfs):
  388. self.addCleanup(setattr, os, 'listdir', os.listdir)
  389. self.addCleanup(setattr, os.path, 'isfile', os.path.isfile)
  390. self.addCleanup(setattr, os.path, 'isdir', os.path.isdir)
  391. self.addCleanup(sys.path.__setitem__, slice(None), list(sys.path))
  392. def list_dir(path):
  393. return list(vfs[path])
  394. os.listdir = list_dir
  395. os.path.isdir = lambda path: not path.endswith('.py')
  396. os.path.isfile = lambda path: path.endswith('.py')
  397. def test_discover_with_modules_that_fail_to_import(self):
  398. loader = unittest.TestLoader()
  399. self.setup_import_issue_tests('test_this_does_not_exist.py')
  400. suite = loader.discover('.')
  401. self.assertIn(os.getcwd(), sys.path)
  402. self.assertEqual(suite.countTestCases(), 1)
  403. # Errors loading the suite are also captured for introspection.
  404. self.assertNotEqual([], loader.errors)
  405. self.assertEqual(1, len(loader.errors))
  406. error = loader.errors[0]
  407. self.assertTrue(
  408. 'Failed to import test module: test_this_does_not_exist' in error,
  409. 'missing error string in %r' % error)
  410. test = list(list(suite)[0])[0] # extract test from suite
  411. with self.assertRaises(ImportError):
  412. test.test_this_does_not_exist()
  413. def test_discover_with_init_modules_that_fail_to_import(self):
  414. vfs = {abspath('/foo'): ['my_package'],
  415. abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
  416. self.setup_import_issue_package_tests(vfs)
  417. import_calls = []
  418. def _get_module_from_name(name):
  419. import_calls.append(name)
  420. raise ImportError("Cannot import Name")
  421. loader = unittest.TestLoader()
  422. loader._get_module_from_name = _get_module_from_name
  423. suite = loader.discover(abspath('/foo'))
  424. self.assertIn(abspath('/foo'), sys.path)
  425. self.assertEqual(suite.countTestCases(), 1)
  426. # Errors loading the suite are also captured for introspection.
  427. self.assertNotEqual([], loader.errors)
  428. self.assertEqual(1, len(loader.errors))
  429. error = loader.errors[0]
  430. self.assertTrue(
  431. 'Failed to import test module: my_package' in error,
  432. 'missing error string in %r' % error)
  433. test = list(list(suite)[0])[0] # extract test from suite
  434. with self.assertRaises(ImportError):
  435. test.my_package()
  436. self.assertEqual(import_calls, ['my_package'])
  437. # Check picklability
  438. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  439. pickle.loads(pickle.dumps(test, proto))
  440. def test_discover_with_module_that_raises_SkipTest_on_import(self):
  441. if not unittest.BaseTestSuite._cleanup:
  442. raise unittest.SkipTest("Suite cleanup is disabled")
  443. loader = unittest.TestLoader()
  444. def _get_module_from_name(name):
  445. raise unittest.SkipTest('skipperoo')
  446. loader._get_module_from_name = _get_module_from_name
  447. self.setup_import_issue_tests('test_skip_dummy.py')
  448. suite = loader.discover('.')
  449. self.assertEqual(suite.countTestCases(), 1)
  450. result = unittest.TestResult()
  451. suite.run(result)
  452. self.assertEqual(len(result.skipped), 1)
  453. # Check picklability
  454. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  455. pickle.loads(pickle.dumps(suite, proto))
  456. def test_discover_with_init_module_that_raises_SkipTest_on_import(self):
  457. if not unittest.BaseTestSuite._cleanup:
  458. raise unittest.SkipTest("Suite cleanup is disabled")
  459. vfs = {abspath('/foo'): ['my_package'],
  460. abspath('/foo/my_package'): ['__init__.py', 'test_module.py']}
  461. self.setup_import_issue_package_tests(vfs)
  462. import_calls = []
  463. def _get_module_from_name(name):
  464. import_calls.append(name)
  465. raise unittest.SkipTest('skipperoo')
  466. loader = unittest.TestLoader()
  467. loader._get_module_from_name = _get_module_from_name
  468. suite = loader.discover(abspath('/foo'))
  469. self.assertIn(abspath('/foo'), sys.path)
  470. self.assertEqual(suite.countTestCases(), 1)
  471. result = unittest.TestResult()
  472. suite.run(result)
  473. self.assertEqual(len(result.skipped), 1)
  474. self.assertEqual(result.testsRun, 1)
  475. self.assertEqual(import_calls, ['my_package'])
  476. # Check picklability
  477. for proto in range(pickle.HIGHEST_PROTOCOL + 1):
  478. pickle.loads(pickle.dumps(suite, proto))
  479. def test_command_line_handling_parseArgs(self):
  480. program = TestableTestProgram()
  481. args = []
  482. program._do_discovery = args.append
  483. program.parseArgs(['something', 'discover'])
  484. self.assertEqual(args, [[]])
  485. args[:] = []
  486. program.parseArgs(['something', 'discover', 'foo', 'bar'])
  487. self.assertEqual(args, [['foo', 'bar']])
  488. def test_command_line_handling_discover_by_default(self):
  489. program = TestableTestProgram()
  490. args = []
  491. program._do_discovery = args.append
  492. program.parseArgs(['something'])
  493. self.assertEqual(args, [[]])
  494. self.assertEqual(program.verbosity, 1)
  495. self.assertIs(program.buffer, False)
  496. self.assertIs(program.catchbreak, False)
  497. self.assertIs(program.failfast, False)
  498. def test_command_line_handling_discover_by_default_with_options(self):
  499. program = TestableTestProgram()
  500. args = []
  501. program._do_discovery = args.append
  502. program.parseArgs(['something', '-v', '-b', '-v', '-c', '-f'])
  503. self.assertEqual(args, [[]])
  504. self.assertEqual(program.verbosity, 2)
  505. self.assertIs(program.buffer, True)
  506. self.assertIs(program.catchbreak, True)
  507. self.assertIs(program.failfast, True)
  508. def test_command_line_handling_do_discovery_too_many_arguments(self):
  509. program = TestableTestProgram()
  510. program.testLoader = None
  511. with support.captured_stderr() as stderr, \
  512. self.assertRaises(SystemExit) as cm:
  513. # too many args
  514. program._do_discovery(['one', 'two', 'three', 'four'])
  515. self.assertEqual(cm.exception.args, (2,))
  516. self.assertIn('usage:', stderr.getvalue())
  517. def test_command_line_handling_do_discovery_uses_default_loader(self):
  518. program = object.__new__(unittest.TestProgram)
  519. program._initArgParsers()
  520. class Loader(object):
  521. args = []
  522. def discover(self, start_dir, pattern, top_level_dir):
  523. self.args.append((start_dir, pattern, top_level_dir))
  524. return 'tests'
  525. program.testLoader = Loader()
  526. program._do_discovery(['-v'])
  527. self.assertEqual(Loader.args, [('.', 'test*.py', None)])
  528. def test_command_line_handling_do_discovery_calls_loader(self):
  529. program = TestableTestProgram()
  530. class Loader(object):
  531. args = []
  532. def discover(self, start_dir, pattern, top_level_dir):
  533. self.args.append((start_dir, pattern, top_level_dir))
  534. return 'tests'
  535. program._do_discovery(['-v'], Loader=Loader)
  536. self.assertEqual(program.verbosity, 2)
  537. self.assertEqual(program.test, 'tests')
  538. self.assertEqual(Loader.args, [('.', 'test*.py', None)])
  539. Loader.args = []
  540. program = TestableTestProgram()
  541. program._do_discovery(['--verbose'], Loader=Loader)
  542. self.assertEqual(program.test, 'tests')
  543. self.assertEqual(Loader.args, [('.', 'test*.py', None)])
  544. Loader.args = []
  545. program = TestableTestProgram()
  546. program._do_discovery([], Loader=Loader)
  547. self.assertEqual(program.test, 'tests')
  548. self.assertEqual(Loader.args, [('.', 'test*.py', None)])
  549. Loader.args = []
  550. program = TestableTestProgram()
  551. program._do_discovery(['fish'], Loader=Loader)
  552. self.assertEqual(program.test, 'tests')
  553. self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
  554. Loader.args = []
  555. program = TestableTestProgram()
  556. program._do_discovery(['fish', 'eggs'], Loader=Loader)
  557. self.assertEqual(program.test, 'tests')
  558. self.assertEqual(Loader.args, [('fish', 'eggs', None)])
  559. Loader.args = []
  560. program = TestableTestProgram()
  561. program._do_discovery(['fish', 'eggs', 'ham'], Loader=Loader)
  562. self.assertEqual(program.test, 'tests')
  563. self.assertEqual(Loader.args, [('fish', 'eggs', 'ham')])
  564. Loader.args = []
  565. program = TestableTestProgram()
  566. program._do_discovery(['-s', 'fish'], Loader=Loader)
  567. self.assertEqual(program.test, 'tests')
  568. self.assertEqual(Loader.args, [('fish', 'test*.py', None)])
  569. Loader.args = []
  570. program = TestableTestProgram()
  571. program._do_discovery(['-t', 'fish'], Loader=Loader)
  572. self.assertEqual(program.test, 'tests')
  573. self.assertEqual(Loader.args, [('.', 'test*.py', 'fish')])
  574. Loader.args = []
  575. program = TestableTestProgram()
  576. program._do_discovery(['-p', 'fish'], Loader=Loader)
  577. self.assertEqual(program.test, 'tests')
  578. self.assertEqual(Loader.args, [('.', 'fish', None)])
  579. self.assertFalse(program.failfast)
  580. self.assertFalse(program.catchbreak)
  581. Loader.args = []
  582. program = TestableTestProgram()
  583. program._do_discovery(['-p', 'eggs', '-s', 'fish', '-v', '-f', '-c'],
  584. Loader=Loader)
  585. self.assertEqual(program.test, 'tests')
  586. self.assertEqual(Loader.args, [('fish', 'eggs', None)])
  587. self.assertEqual(program.verbosity, 2)
  588. self.assertTrue(program.failfast)
  589. self.assertTrue(program.catchbreak)
  590. def setup_module_clash(self):
  591. class Module(object):
  592. __file__ = 'bar/foo.py'
  593. sys.modules['foo'] = Module
  594. full_path = os.path.abspath('foo')
  595. original_listdir = os.listdir
  596. original_isfile = os.path.isfile
  597. original_isdir = os.path.isdir
  598. original_realpath = os.path.realpath
  599. def cleanup():
  600. os.listdir = original_listdir
  601. os.path.isfile = original_isfile
  602. os.path.isdir = original_isdir
  603. os.path.realpath = original_realpath
  604. del sys.modules['foo']
  605. if full_path in sys.path:
  606. sys.path.remove(full_path)
  607. self.addCleanup(cleanup)
  608. def listdir(_):
  609. return ['foo.py']
  610. def isfile(_):
  611. return True
  612. def isdir(_):
  613. return True
  614. os.listdir = listdir
  615. os.path.isfile = isfile
  616. os.path.isdir = isdir
  617. if os.name == 'nt':
  618. # ntpath.realpath may inject path prefixes when failing to
  619. # resolve real files, so we substitute abspath() here instead.
  620. os.path.realpath = os.path.abspath
  621. return full_path
  622. def test_detect_module_clash(self):
  623. full_path = self.setup_module_clash()
  624. loader = unittest.TestLoader()
  625. mod_dir = os.path.abspath('bar')
  626. expected_dir = os.path.abspath('foo')
  627. msg = re.escape(r"'foo' module incorrectly imported from %r. Expected %r. "
  628. "Is this module globally installed?" % (mod_dir, expected_dir))
  629. self.assertRaisesRegex(
  630. ImportError, '^%s$' % msg, loader.discover,
  631. start_dir='foo', pattern='foo.py'
  632. )
  633. self.assertEqual(sys.path[0], full_path)
  634. def test_module_symlink_ok(self):
  635. full_path = self.setup_module_clash()
  636. original_realpath = os.path.realpath
  637. mod_dir = os.path.abspath('bar')
  638. expected_dir = os.path.abspath('foo')
  639. def cleanup():
  640. os.path.realpath = original_realpath
  641. self.addCleanup(cleanup)
  642. def realpath(path):
  643. if path == os.path.join(mod_dir, 'foo.py'):
  644. return os.path.join(expected_dir, 'foo.py')
  645. return path
  646. os.path.realpath = realpath
  647. loader = unittest.TestLoader()
  648. loader.discover(start_dir='foo', pattern='foo.py')
  649. def test_discovery_from_dotted_path(self):
  650. loader = unittest.TestLoader()
  651. tests = [self]
  652. expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
  653. self.wasRun = False
  654. def _find_tests(start_dir, pattern, namespace=None):
  655. self.wasRun = True
  656. self.assertEqual(start_dir, expectedPath)
  657. return tests
  658. loader._find_tests = _find_tests
  659. suite = loader.discover('unittest.test')
  660. self.assertTrue(self.wasRun)
  661. self.assertEqual(suite._tests, tests)
  662. def test_discovery_from_dotted_path_builtin_modules(self):
  663. loader = unittest.TestLoader()
  664. listdir = os.listdir
  665. os.listdir = lambda _: ['test_this_does_not_exist.py']
  666. isfile = os.path.isfile
  667. isdir = os.path.isdir
  668. os.path.isdir = lambda _: False
  669. orig_sys_path = sys.path[:]
  670. def restore():
  671. os.path.isfile = isfile
  672. os.path.isdir = isdir
  673. os.listdir = listdir
  674. sys.path[:] = orig_sys_path
  675. self.addCleanup(restore)
  676. with self.assertRaises(TypeError) as cm:
  677. loader.discover('sys')
  678. self.assertEqual(str(cm.exception),
  679. 'Can not use builtin modules '
  680. 'as dotted module names')
  681. def test_discovery_from_dotted_namespace_packages(self):
  682. loader = unittest.TestLoader()
  683. package = types.ModuleType('package')
  684. package.__path__ = ['/a', '/b']
  685. package.__spec__ = types.SimpleNamespace(
  686. loader=None,
  687. submodule_search_locations=['/a', '/b']
  688. )
  689. def _import(packagename, *args, **kwargs):
  690. sys.modules[packagename] = package
  691. return package
  692. _find_tests_args = []
  693. def _find_tests(start_dir, pattern, namespace=None):
  694. _find_tests_args.append((start_dir, pattern))
  695. return ['%s/tests' % start_dir]
  696. loader._find_tests = _find_tests
  697. loader.suiteClass = list
  698. with unittest.mock.patch('builtins.__import__', _import):
  699. # Since loader.discover() can modify sys.path, restore it when done.
  700. with support.DirsOnSysPath():
  701. # Make sure to remove 'package' from sys.modules when done.
  702. with test.test_importlib.util.uncache('package'):
  703. suite = loader.discover('package')
  704. self.assertEqual(suite, ['/a/tests', '/b/tests'])
  705. def test_discovery_failed_discovery(self):
  706. loader = unittest.TestLoader()
  707. package = types.ModuleType('package')
  708. def _import(packagename, *args, **kwargs):
  709. sys.modules[packagename] = package
  710. return package
  711. with unittest.mock.patch('builtins.__import__', _import):
  712. # Since loader.discover() can modify sys.path, restore it when done.
  713. with support.DirsOnSysPath():
  714. # Make sure to remove 'package' from sys.modules when done.
  715. with test.test_importlib.util.uncache('package'):
  716. with self.assertRaises(TypeError) as cm:
  717. loader.discover('package')
  718. self.assertEqual(str(cm.exception),
  719. 'don\'t know how to discover from {!r}'
  720. .format(package))
  721. if __name__ == '__main__':
  722. unittest.main()