test_program.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. import io
  2. import os
  3. import sys
  4. import subprocess
  5. from test import support
  6. import unittest
  7. import unittest.test
  8. from unittest.test.test_result import BufferedWriter
  9. class Test_TestProgram(unittest.TestCase):
  10. def test_discovery_from_dotted_path(self):
  11. loader = unittest.TestLoader()
  12. tests = [self]
  13. expectedPath = os.path.abspath(os.path.dirname(unittest.test.__file__))
  14. self.wasRun = False
  15. def _find_tests(start_dir, pattern):
  16. self.wasRun = True
  17. self.assertEqual(start_dir, expectedPath)
  18. return tests
  19. loader._find_tests = _find_tests
  20. suite = loader.discover('unittest.test')
  21. self.assertTrue(self.wasRun)
  22. self.assertEqual(suite._tests, tests)
  23. # Horrible white box test
  24. def testNoExit(self):
  25. result = object()
  26. test = object()
  27. class FakeRunner(object):
  28. def run(self, test):
  29. self.test = test
  30. return result
  31. runner = FakeRunner()
  32. oldParseArgs = unittest.TestProgram.parseArgs
  33. def restoreParseArgs():
  34. unittest.TestProgram.parseArgs = oldParseArgs
  35. unittest.TestProgram.parseArgs = lambda *args: None
  36. self.addCleanup(restoreParseArgs)
  37. def removeTest():
  38. del unittest.TestProgram.test
  39. unittest.TestProgram.test = test
  40. self.addCleanup(removeTest)
  41. program = unittest.TestProgram(testRunner=runner, exit=False, verbosity=2)
  42. self.assertEqual(program.result, result)
  43. self.assertEqual(runner.test, test)
  44. self.assertEqual(program.verbosity, 2)
  45. class FooBar(unittest.TestCase):
  46. def testPass(self):
  47. pass
  48. def testFail(self):
  49. raise AssertionError
  50. class FooBarLoader(unittest.TestLoader):
  51. """Test loader that returns a suite containing FooBar."""
  52. def loadTestsFromModule(self, module):
  53. return self.suiteClass(
  54. [self.loadTestsFromTestCase(Test_TestProgram.FooBar)])
  55. def loadTestsFromNames(self, names, module):
  56. return self.suiteClass(
  57. [self.loadTestsFromTestCase(Test_TestProgram.FooBar)])
  58. def test_defaultTest_with_string(self):
  59. class FakeRunner(object):
  60. def run(self, test):
  61. self.test = test
  62. return True
  63. old_argv = sys.argv
  64. sys.argv = ['faketest']
  65. runner = FakeRunner()
  66. program = unittest.TestProgram(testRunner=runner, exit=False,
  67. defaultTest='unittest.test',
  68. testLoader=self.FooBarLoader())
  69. sys.argv = old_argv
  70. self.assertEqual(('unittest.test',), program.testNames)
  71. def test_defaultTest_with_iterable(self):
  72. class FakeRunner(object):
  73. def run(self, test):
  74. self.test = test
  75. return True
  76. old_argv = sys.argv
  77. sys.argv = ['faketest']
  78. runner = FakeRunner()
  79. program = unittest.TestProgram(
  80. testRunner=runner, exit=False,
  81. defaultTest=['unittest.test', 'unittest.test2'],
  82. testLoader=self.FooBarLoader())
  83. sys.argv = old_argv
  84. self.assertEqual(['unittest.test', 'unittest.test2'],
  85. program.testNames)
  86. def test_NonExit(self):
  87. stream = BufferedWriter()
  88. program = unittest.main(exit=False,
  89. argv=["foobar"],
  90. testRunner=unittest.TextTestRunner(stream=stream),
  91. testLoader=self.FooBarLoader())
  92. self.assertTrue(hasattr(program, 'result'))
  93. self.assertIn('\nFAIL: testFail ', stream.getvalue())
  94. self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
  95. def test_Exit(self):
  96. stream = BufferedWriter()
  97. self.assertRaises(
  98. SystemExit,
  99. unittest.main,
  100. argv=["foobar"],
  101. testRunner=unittest.TextTestRunner(stream=stream),
  102. exit=True,
  103. testLoader=self.FooBarLoader())
  104. self.assertIn('\nFAIL: testFail ', stream.getvalue())
  105. self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
  106. def test_ExitAsDefault(self):
  107. stream = BufferedWriter()
  108. self.assertRaises(
  109. SystemExit,
  110. unittest.main,
  111. argv=["foobar"],
  112. testRunner=unittest.TextTestRunner(stream=stream),
  113. testLoader=self.FooBarLoader())
  114. self.assertIn('\nFAIL: testFail ', stream.getvalue())
  115. self.assertTrue(stream.getvalue().endswith('\n\nFAILED (failures=1)\n'))
  116. class InitialisableProgram(unittest.TestProgram):
  117. exit = False
  118. result = None
  119. verbosity = 1
  120. defaultTest = None
  121. tb_locals = False
  122. testRunner = None
  123. testLoader = unittest.defaultTestLoader
  124. module = '__main__'
  125. progName = 'test'
  126. test = 'test'
  127. def __init__(self, *args):
  128. pass
  129. RESULT = object()
  130. class FakeRunner(object):
  131. initArgs = None
  132. test = None
  133. raiseError = 0
  134. def __init__(self, **kwargs):
  135. FakeRunner.initArgs = kwargs
  136. if FakeRunner.raiseError:
  137. FakeRunner.raiseError -= 1
  138. raise TypeError
  139. def run(self, test):
  140. FakeRunner.test = test
  141. return RESULT
  142. class TestCommandLineArgs(unittest.TestCase):
  143. def setUp(self):
  144. self.program = InitialisableProgram()
  145. self.program.createTests = lambda: None
  146. FakeRunner.initArgs = None
  147. FakeRunner.test = None
  148. FakeRunner.raiseError = 0
  149. def testVerbosity(self):
  150. program = self.program
  151. for opt in '-q', '--quiet':
  152. program.verbosity = 1
  153. program.parseArgs([None, opt])
  154. self.assertEqual(program.verbosity, 0)
  155. for opt in '-v', '--verbose':
  156. program.verbosity = 1
  157. program.parseArgs([None, opt])
  158. self.assertEqual(program.verbosity, 2)
  159. def testBufferCatchFailfast(self):
  160. program = self.program
  161. for arg, attr in (('buffer', 'buffer'), ('failfast', 'failfast'),
  162. ('catch', 'catchbreak')):
  163. setattr(program, attr, None)
  164. program.parseArgs([None])
  165. self.assertIs(getattr(program, attr), False)
  166. false = []
  167. setattr(program, attr, false)
  168. program.parseArgs([None])
  169. self.assertIs(getattr(program, attr), false)
  170. true = [42]
  171. setattr(program, attr, true)
  172. program.parseArgs([None])
  173. self.assertIs(getattr(program, attr), true)
  174. short_opt = '-%s' % arg[0]
  175. long_opt = '--%s' % arg
  176. for opt in short_opt, long_opt:
  177. setattr(program, attr, None)
  178. program.parseArgs([None, opt])
  179. self.assertIs(getattr(program, attr), True)
  180. setattr(program, attr, False)
  181. with support.captured_stderr() as stderr, \
  182. self.assertRaises(SystemExit) as cm:
  183. program.parseArgs([None, opt])
  184. self.assertEqual(cm.exception.args, (2,))
  185. setattr(program, attr, True)
  186. with support.captured_stderr() as stderr, \
  187. self.assertRaises(SystemExit) as cm:
  188. program.parseArgs([None, opt])
  189. self.assertEqual(cm.exception.args, (2,))
  190. def testWarning(self):
  191. """Test the warnings argument"""
  192. # see #10535
  193. class FakeTP(unittest.TestProgram):
  194. def parseArgs(self, *args, **kw): pass
  195. def runTests(self, *args, **kw): pass
  196. warnoptions = sys.warnoptions[:]
  197. try:
  198. sys.warnoptions[:] = []
  199. # no warn options, no arg -> default
  200. self.assertEqual(FakeTP().warnings, 'default')
  201. # no warn options, w/ arg -> arg value
  202. self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
  203. sys.warnoptions[:] = ['somevalue']
  204. # warn options, no arg -> None
  205. # warn options, w/ arg -> arg value
  206. self.assertEqual(FakeTP().warnings, None)
  207. self.assertEqual(FakeTP(warnings='ignore').warnings, 'ignore')
  208. finally:
  209. sys.warnoptions[:] = warnoptions
  210. def testRunTestsRunnerClass(self):
  211. program = self.program
  212. program.testRunner = FakeRunner
  213. program.verbosity = 'verbosity'
  214. program.failfast = 'failfast'
  215. program.buffer = 'buffer'
  216. program.warnings = 'warnings'
  217. program.runTests()
  218. self.assertEqual(FakeRunner.initArgs, {'verbosity': 'verbosity',
  219. 'failfast': 'failfast',
  220. 'buffer': 'buffer',
  221. 'tb_locals': False,
  222. 'warnings': 'warnings'})
  223. self.assertEqual(FakeRunner.test, 'test')
  224. self.assertIs(program.result, RESULT)
  225. def testRunTestsRunnerInstance(self):
  226. program = self.program
  227. program.testRunner = FakeRunner()
  228. FakeRunner.initArgs = None
  229. program.runTests()
  230. # A new FakeRunner should not have been instantiated
  231. self.assertIsNone(FakeRunner.initArgs)
  232. self.assertEqual(FakeRunner.test, 'test')
  233. self.assertIs(program.result, RESULT)
  234. def test_locals(self):
  235. program = self.program
  236. program.testRunner = FakeRunner
  237. program.parseArgs([None, '--locals'])
  238. self.assertEqual(True, program.tb_locals)
  239. program.runTests()
  240. self.assertEqual(FakeRunner.initArgs, {'buffer': False,
  241. 'failfast': False,
  242. 'tb_locals': True,
  243. 'verbosity': 1,
  244. 'warnings': None})
  245. def testRunTestsOldRunnerClass(self):
  246. program = self.program
  247. # Two TypeErrors are needed to fall all the way back to old-style
  248. # runners - one to fail tb_locals, one to fail buffer etc.
  249. FakeRunner.raiseError = 2
  250. program.testRunner = FakeRunner
  251. program.verbosity = 'verbosity'
  252. program.failfast = 'failfast'
  253. program.buffer = 'buffer'
  254. program.test = 'test'
  255. program.runTests()
  256. # If initialising raises a type error it should be retried
  257. # without the new keyword arguments
  258. self.assertEqual(FakeRunner.initArgs, {})
  259. self.assertEqual(FakeRunner.test, 'test')
  260. self.assertIs(program.result, RESULT)
  261. def testCatchBreakInstallsHandler(self):
  262. module = sys.modules['unittest.main']
  263. original = module.installHandler
  264. def restore():
  265. module.installHandler = original
  266. self.addCleanup(restore)
  267. self.installed = False
  268. def fakeInstallHandler():
  269. self.installed = True
  270. module.installHandler = fakeInstallHandler
  271. program = self.program
  272. program.catchbreak = True
  273. program.testRunner = FakeRunner
  274. program.runTests()
  275. self.assertTrue(self.installed)
  276. def _patch_isfile(self, names, exists=True):
  277. def isfile(path):
  278. return path in names
  279. original = os.path.isfile
  280. os.path.isfile = isfile
  281. def restore():
  282. os.path.isfile = original
  283. self.addCleanup(restore)
  284. def testParseArgsFileNames(self):
  285. # running tests with filenames instead of module names
  286. program = self.program
  287. argv = ['progname', 'foo.py', 'bar.Py', 'baz.PY', 'wing.txt']
  288. self._patch_isfile(argv)
  289. program.createTests = lambda: None
  290. program.parseArgs(argv)
  291. # note that 'wing.txt' is not a Python file so the name should
  292. # *not* be converted to a module name
  293. expected = ['foo', 'bar', 'baz', 'wing.txt']
  294. self.assertEqual(program.testNames, expected)
  295. def testParseArgsFilePaths(self):
  296. program = self.program
  297. argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
  298. self._patch_isfile(argv)
  299. program.createTests = lambda: None
  300. program.parseArgs(argv)
  301. expected = ['foo.bar.baz', 'green.red']
  302. self.assertEqual(program.testNames, expected)
  303. def testParseArgsNonExistentFiles(self):
  304. program = self.program
  305. argv = ['progname', 'foo/bar/baz.py', 'green\\red.py']
  306. self._patch_isfile([])
  307. program.createTests = lambda: None
  308. program.parseArgs(argv)
  309. self.assertEqual(program.testNames, argv[1:])
  310. def testParseArgsAbsolutePathsThatCanBeConverted(self):
  311. cur_dir = os.getcwd()
  312. program = self.program
  313. def _join(name):
  314. return os.path.join(cur_dir, name)
  315. argv = ['progname', _join('foo/bar/baz.py'), _join('green\\red.py')]
  316. self._patch_isfile(argv)
  317. program.createTests = lambda: None
  318. program.parseArgs(argv)
  319. expected = ['foo.bar.baz', 'green.red']
  320. self.assertEqual(program.testNames, expected)
  321. def testParseArgsAbsolutePathsThatCannotBeConverted(self):
  322. program = self.program
  323. # even on Windows '/...' is considered absolute by os.path.abspath
  324. argv = ['progname', '/foo/bar/baz.py', '/green/red.py']
  325. self._patch_isfile(argv)
  326. program.createTests = lambda: None
  327. program.parseArgs(argv)
  328. self.assertEqual(program.testNames, argv[1:])
  329. # it may be better to use platform specific functions to normalise paths
  330. # rather than accepting '.PY' and '\' as file separator on Linux / Mac
  331. # it would also be better to check that a filename is a valid module
  332. # identifier (we have a regex for this in loader.py)
  333. # for invalid filenames should we raise a useful error rather than
  334. # leaving the current error message (import of filename fails) in place?
  335. def testParseArgsSelectedTestNames(self):
  336. program = self.program
  337. argv = ['progname', '-k', 'foo', '-k', 'bar', '-k', '*pat*']
  338. program.createTests = lambda: None
  339. program.parseArgs(argv)
  340. self.assertEqual(program.testNamePatterns, ['*foo*', '*bar*', '*pat*'])
  341. def testSelectedTestNamesFunctionalTest(self):
  342. def run_unittest(args):
  343. p = subprocess.Popen([sys.executable, '-m', 'unittest'] + args,
  344. stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, cwd=os.path.dirname(__file__))
  345. with p:
  346. _, stderr = p.communicate()
  347. return stderr.decode()
  348. t = '_test_warnings'
  349. self.assertIn('Ran 7 tests', run_unittest([t]))
  350. self.assertIn('Ran 7 tests', run_unittest(['-k', 'TestWarnings', t]))
  351. self.assertIn('Ran 7 tests', run_unittest(['discover', '-p', '*_test*', '-k', 'TestWarnings']))
  352. self.assertIn('Ran 2 tests', run_unittest(['-k', 'f', t]))
  353. self.assertIn('Ran 7 tests', run_unittest(['-k', 't', t]))
  354. self.assertIn('Ran 3 tests', run_unittest(['-k', '*t', t]))
  355. self.assertIn('Ran 7 tests', run_unittest(['-k', '*test_warnings.*Warning*', t]))
  356. self.assertIn('Ran 1 test', run_unittest(['-k', '*test_warnings.*warning*', t]))
  357. if __name__ == '__main__':
  358. unittest.main()