main.py 11 KB

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