main.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. """Unittest main program"""
  2. import sys
  3. import argparse
  4. import os
  5. import warnings
  6. from . import loader, runner
  7. from .signals import installHandler
  8. __unittest = True
  9. _NO_TESTS_EXITCODE = 5
  10. MAIN_EXAMPLES = """\
  11. Examples:
  12. %(prog)s test_module - run tests from test_module
  13. %(prog)s module.TestClass - run tests from module.TestClass
  14. %(prog)s module.Class.test_method - run specified test method
  15. %(prog)s path/to/test_file.py - run tests from test_file.py
  16. """
  17. MODULE_EXAMPLES = """\
  18. Examples:
  19. %(prog)s - run default set of tests
  20. %(prog)s MyTestSuite - run suite 'MyTestSuite'
  21. %(prog)s MyTestCase.testSomething - run MyTestCase.testSomething
  22. %(prog)s MyTestCase - run all 'test*' test methods
  23. in MyTestCase
  24. """
  25. def _convert_name(name):
  26. # on Linux / Mac OS X 'foo.PY' is not importable, but on
  27. # Windows it is. Simpler to do a case insensitive match
  28. # a better check would be to check that the name is a
  29. # valid Python module name.
  30. if os.path.isfile(name) and name.lower().endswith('.py'):
  31. if os.path.isabs(name):
  32. rel_path = os.path.relpath(name, os.getcwd())
  33. if os.path.isabs(rel_path) or rel_path.startswith(os.pardir):
  34. return name
  35. name = rel_path
  36. # on Windows both '\' and '/' are used as path
  37. # separators. Better to replace both than rely on os.path.sep
  38. return os.path.normpath(name)[:-3].replace('\\', '.').replace('/', '.')
  39. return name
  40. def _convert_names(names):
  41. return [_convert_name(name) for name in names]
  42. def _convert_select_pattern(pattern):
  43. if not '*' in pattern:
  44. pattern = '*%s*' % pattern
  45. return pattern
  46. class TestProgram(object):
  47. """A command-line program that runs a set of tests; this is primarily
  48. for making test modules conveniently executable.
  49. """
  50. # defaults for testing
  51. module=None
  52. verbosity = 1
  53. failfast = catchbreak = buffer = progName = warnings = testNamePatterns = None
  54. _discovery_parser = None
  55. def __init__(self, module='__main__', defaultTest=None, argv=None,
  56. testRunner=None, testLoader=loader.defaultTestLoader,
  57. exit=True, verbosity=1, failfast=None, catchbreak=None,
  58. buffer=None, warnings=None, *, tb_locals=False,
  59. durations=None):
  60. if isinstance(module, str):
  61. self.module = __import__(module)
  62. for part in module.split('.')[1:]:
  63. self.module = getattr(self.module, part)
  64. else:
  65. self.module = module
  66. if argv is None:
  67. argv = sys.argv
  68. self.exit = exit
  69. self.failfast = failfast
  70. self.catchbreak = catchbreak
  71. self.verbosity = verbosity
  72. self.buffer = buffer
  73. self.tb_locals = tb_locals
  74. self.durations = durations
  75. if warnings is None and not sys.warnoptions:
  76. # even if DeprecationWarnings are ignored by default
  77. # print them anyway unless other warnings settings are
  78. # specified by the warnings arg or the -W python flag
  79. self.warnings = 'default'
  80. else:
  81. # here self.warnings is set either to the value passed
  82. # to the warnings args or to None.
  83. # If the user didn't pass a value self.warnings will
  84. # be None. This means that the behavior is unchanged
  85. # and depends on the values passed to -W.
  86. self.warnings = warnings
  87. self.defaultTest = defaultTest
  88. self.testRunner = testRunner
  89. self.testLoader = testLoader
  90. self.progName = os.path.basename(argv[0])
  91. self.parseArgs(argv)
  92. self.runTests()
  93. def usageExit(self, msg=None):
  94. warnings.warn("TestProgram.usageExit() is deprecated and will be"
  95. " removed in Python 3.13", DeprecationWarning)
  96. if msg:
  97. print(msg)
  98. if self._discovery_parser is None:
  99. self._initArgParsers()
  100. self._print_help()
  101. sys.exit(2)
  102. def _print_help(self, *args, **kwargs):
  103. if self.module is None:
  104. print(self._main_parser.format_help())
  105. print(MAIN_EXAMPLES % {'prog': self.progName})
  106. self._discovery_parser.print_help()
  107. else:
  108. print(self._main_parser.format_help())
  109. print(MODULE_EXAMPLES % {'prog': self.progName})
  110. def parseArgs(self, argv):
  111. self._initArgParsers()
  112. if self.module is None:
  113. if len(argv) > 1 and argv[1].lower() == 'discover':
  114. self._do_discovery(argv[2:])
  115. return
  116. self._main_parser.parse_args(argv[1:], self)
  117. if not self.tests:
  118. # this allows "python -m unittest -v" to still work for
  119. # test discovery.
  120. self._do_discovery([])
  121. return
  122. else:
  123. self._main_parser.parse_args(argv[1:], self)
  124. if self.tests:
  125. self.testNames = _convert_names(self.tests)
  126. if __name__ == '__main__':
  127. # to support python -m unittest ...
  128. self.module = None
  129. elif self.defaultTest is None:
  130. # createTests will load tests from self.module
  131. self.testNames = None
  132. elif isinstance(self.defaultTest, str):
  133. self.testNames = (self.defaultTest,)
  134. else:
  135. self.testNames = list(self.defaultTest)
  136. self.createTests()
  137. def createTests(self, from_discovery=False, Loader=None):
  138. if self.testNamePatterns:
  139. self.testLoader.testNamePatterns = self.testNamePatterns
  140. if from_discovery:
  141. loader = self.testLoader if Loader is None else Loader()
  142. self.test = loader.discover(self.start, self.pattern, self.top)
  143. elif self.testNames is None:
  144. self.test = self.testLoader.loadTestsFromModule(self.module)
  145. else:
  146. self.test = self.testLoader.loadTestsFromNames(self.testNames,
  147. self.module)
  148. def _initArgParsers(self):
  149. parent_parser = self._getParentArgParser()
  150. self._main_parser = self._getMainArgParser(parent_parser)
  151. self._discovery_parser = self._getDiscoveryArgParser(parent_parser)
  152. def _getParentArgParser(self):
  153. parser = argparse.ArgumentParser(add_help=False)
  154. parser.add_argument('-v', '--verbose', dest='verbosity',
  155. action='store_const', const=2,
  156. help='Verbose output')
  157. parser.add_argument('-q', '--quiet', dest='verbosity',
  158. action='store_const', const=0,
  159. help='Quiet output')
  160. parser.add_argument('--locals', dest='tb_locals',
  161. action='store_true',
  162. help='Show local variables in tracebacks')
  163. parser.add_argument('--durations', dest='durations', type=int,
  164. default=None, metavar="N",
  165. help='Show the N slowest test cases (N=0 for all)')
  166. if self.failfast is None:
  167. parser.add_argument('-f', '--failfast', dest='failfast',
  168. action='store_true',
  169. help='Stop on first fail or error')
  170. self.failfast = False
  171. if self.catchbreak is None:
  172. parser.add_argument('-c', '--catch', dest='catchbreak',
  173. action='store_true',
  174. help='Catch Ctrl-C and display results so far')
  175. self.catchbreak = False
  176. if self.buffer is None:
  177. parser.add_argument('-b', '--buffer', dest='buffer',
  178. action='store_true',
  179. help='Buffer stdout and stderr during tests')
  180. self.buffer = False
  181. if self.testNamePatterns is None:
  182. parser.add_argument('-k', dest='testNamePatterns',
  183. action='append', type=_convert_select_pattern,
  184. help='Only run tests which match the given substring')
  185. self.testNamePatterns = []
  186. return parser
  187. def _getMainArgParser(self, parent):
  188. parser = argparse.ArgumentParser(parents=[parent])
  189. parser.prog = self.progName
  190. parser.print_help = self._print_help
  191. parser.add_argument('tests', nargs='*',
  192. help='a list of any number of test modules, '
  193. 'classes and test methods.')
  194. return parser
  195. def _getDiscoveryArgParser(self, parent):
  196. parser = argparse.ArgumentParser(parents=[parent])
  197. parser.prog = '%s discover' % self.progName
  198. parser.epilog = ('For test discovery all test modules must be '
  199. 'importable from the top level directory of the '
  200. 'project.')
  201. parser.add_argument('-s', '--start-directory', dest='start',
  202. help="Directory to start discovery ('.' default)")
  203. parser.add_argument('-p', '--pattern', dest='pattern',
  204. help="Pattern to match tests ('test*.py' default)")
  205. parser.add_argument('-t', '--top-level-directory', dest='top',
  206. help='Top level directory of project (defaults to '
  207. 'start directory)')
  208. for arg in ('start', 'pattern', 'top'):
  209. parser.add_argument(arg, nargs='?',
  210. default=argparse.SUPPRESS,
  211. help=argparse.SUPPRESS)
  212. return parser
  213. def _do_discovery(self, argv, Loader=None):
  214. self.start = '.'
  215. self.pattern = 'test*.py'
  216. self.top = None
  217. if argv is not None:
  218. # handle command line args for test discovery
  219. if self._discovery_parser is None:
  220. # for testing
  221. self._initArgParsers()
  222. self._discovery_parser.parse_args(argv, self)
  223. self.createTests(from_discovery=True, Loader=Loader)
  224. def runTests(self):
  225. if self.catchbreak:
  226. installHandler()
  227. if self.testRunner is None:
  228. self.testRunner = runner.TextTestRunner
  229. if isinstance(self.testRunner, type):
  230. try:
  231. try:
  232. testRunner = self.testRunner(verbosity=self.verbosity,
  233. failfast=self.failfast,
  234. buffer=self.buffer,
  235. warnings=self.warnings,
  236. tb_locals=self.tb_locals,
  237. durations=self.durations)
  238. except TypeError:
  239. # didn't accept the tb_locals or durations argument
  240. testRunner = self.testRunner(verbosity=self.verbosity,
  241. failfast=self.failfast,
  242. buffer=self.buffer,
  243. warnings=self.warnings)
  244. except TypeError:
  245. # didn't accept the verbosity, buffer or failfast arguments
  246. testRunner = self.testRunner()
  247. else:
  248. # it is assumed to be a TestRunner instance
  249. testRunner = self.testRunner
  250. self.result = testRunner.run(self.test)
  251. if self.exit:
  252. if self.result.testsRun == 0 and len(self.result.skipped) == 0:
  253. sys.exit(_NO_TESTS_EXITCODE)
  254. elif self.result.wasSuccessful():
  255. sys.exit(0)
  256. else:
  257. sys.exit(1)
  258. main = TestProgram