| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | """Unittest main program""" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | import os | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | from . import loader, runner | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  | from .signals import installHandler | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-22 00:15:53 +00:00
										 |  |  | __unittest = True | 
					
						
							| 
									
										
										
										
											2023-04-26 18:28:46 -07:00
										 |  |  | _NO_TESTS_EXITCODE = 5 | 
					
						
							| 
									
										
										
										
											2010-03-22 00:15:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  | MAIN_EXAMPLES = """\
 | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | Examples: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |   %(prog)s test_module               - run tests from test_module | 
					
						
							|  |  |  |   %(prog)s module.TestClass          - run tests from module.TestClass | 
					
						
							|  |  |  |   %(prog)s module.Class.test_method  - run specified test method | 
					
						
							| 
									
										
										
										
											2017-04-20 11:46:59 +08:00
										 |  |  |   %(prog)s path/to/test_file.py      - run tests from test_file.py | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  | MODULE_EXAMPLES = """\
 | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | Examples: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |   %(prog)s                           - run default set of tests | 
					
						
							|  |  |  |   %(prog)s MyTestSuite               - run suite 'MyTestSuite' | 
					
						
							|  |  |  |   %(prog)s MyTestCase.testSomething  - run MyTestCase.testSomething | 
					
						
							|  |  |  |   %(prog)s MyTestCase                - run all 'test*' test methods | 
					
						
							|  |  |  |                                        in MyTestCase | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | """
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-12-04 01:11:21 +00:00
										 |  |  | def _convert_name(name): | 
					
						
							|  |  |  |     # on Linux / Mac OS X 'foo.PY' is not importable, but on | 
					
						
							|  |  |  |     # Windows it is. Simpler to do a case insensitive match | 
					
						
							|  |  |  |     # a better check would be to check that the name is a | 
					
						
							|  |  |  |     # valid Python module name. | 
					
						
							|  |  |  |     if os.path.isfile(name) and name.lower().endswith('.py'): | 
					
						
							|  |  |  |         if os.path.isabs(name): | 
					
						
							|  |  |  |             rel_path = os.path.relpath(name, os.getcwd()) | 
					
						
							|  |  |  |             if os.path.isabs(rel_path) or rel_path.startswith(os.pardir): | 
					
						
							|  |  |  |                 return name | 
					
						
							|  |  |  |             name = rel_path | 
					
						
							|  |  |  |         # on Windows both '\' and '/' are used as path | 
					
						
							|  |  |  |         # separators. Better to replace both than rely on os.path.sep | 
					
						
							| 
									
										
										
										
											2022-03-18 00:37:52 +01:00
										 |  |  |         return os.path.normpath(name)[:-3].replace('\\', '.').replace('/', '.') | 
					
						
							| 
									
										
										
										
											2010-12-04 01:11:21 +00:00
										 |  |  |     return name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def _convert_names(names): | 
					
						
							|  |  |  |     return [_convert_name(name) for name in names] | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 16:23:52 +01:00
										 |  |  | def _convert_select_pattern(pattern): | 
					
						
							|  |  |  |     if not '*' in pattern: | 
					
						
							|  |  |  |         pattern = '*%s*' % pattern | 
					
						
							|  |  |  |     return pattern | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | class TestProgram(object): | 
					
						
							|  |  |  |     """A command-line program that runs a set of tests; this is primarily
 | 
					
						
							|  |  |  |        for making test modules conveniently executable. | 
					
						
							|  |  |  |     """
 | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  |     # defaults for testing | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     module=None | 
					
						
							|  |  |  |     verbosity = 1 | 
					
						
							| 
									
										
										
										
											2017-11-25 16:23:52 +01:00
										 |  |  |     failfast = catchbreak = buffer = progName = warnings = testNamePatterns = None | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     _discovery_parser = None | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-07 15:35:24 +00:00
										 |  |  |     def __init__(self, module='__main__', defaultTest=None, argv=None, | 
					
						
							|  |  |  |                     testRunner=None, testLoader=loader.defaultTestLoader, | 
					
						
							|  |  |  |                     exit=True, verbosity=1, failfast=None, catchbreak=None, | 
					
						
							| 
									
										
										
										
											2023-04-03 00:12:51 +02:00
										 |  |  |                     buffer=None, warnings=None, *, tb_locals=False, | 
					
						
							|  |  |  |                     durations=None): | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |         if isinstance(module, str): | 
					
						
							|  |  |  |             self.module = __import__(module) | 
					
						
							|  |  |  |             for part in module.split('.')[1:]: | 
					
						
							|  |  |  |                 self.module = getattr(self.module, part) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.module = module | 
					
						
							|  |  |  |         if argv is None: | 
					
						
							|  |  |  |             argv = sys.argv | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.exit = exit | 
					
						
							| 
									
										
										
										
											2010-03-22 01:13:48 +00:00
										 |  |  |         self.failfast = failfast | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  |         self.catchbreak = catchbreak | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |         self.verbosity = verbosity | 
					
						
							| 
									
										
											  
											
												Merged revisions 79464,79471,79623,79626,79630,79632,79643,79648-79649,79679,79685,79711,79761,79774,79777,79792-79794,79877,79898-79900 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line
  A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython)
........
  r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines
  Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
  This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
........
  r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line
  Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
........
  r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line
  TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option
........
  r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line
  unittest tests no longer replace the sys.stdout put in place by regrtest
........
  r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line
  Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
........
  r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line
  Support dotted module names for test discovery paths in unittest. Issue 8038.
........
  r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line
  Cross platform unittest.TestResult newline handling when buffering stdout / stderr.
........
  r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line
  Another attempt at a fix for unittest.test.test_result for windows line endings
........
  r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line
  Adding -b command line option to the unittest usage message.
........
  r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line
  Minor tweak to unittest command line usage message
........
  r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line
  Documenting new features in unittest
........
  r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line
  unittest documentation formatting changes
........
  r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line
  Adding documentation for new unittest.main() parameters
........
  r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line
  Document signal handling functions in unittest.rst
........
  r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line
  Documentation fixes for unittest
........
  r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line
  Furterh documentation fix for unittest.rst
........
  r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line
  Further documentation fix for unittest.rst
........
  r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line
  Fix module directory finding logic for dotted paths in unittest test discovery.
........
  r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line
  unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.
........
  r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line
  Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333.
........
  r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line
  Correction of unittest documentation typos and omissions
........
											
										 
											2010-04-11 20:43:16 +00:00
										 |  |  |         self.buffer = buffer | 
					
						
							| 
									
										
										
										
											2015-03-06 13:46:35 +13:00
										 |  |  |         self.tb_locals = tb_locals | 
					
						
							| 
									
										
										
										
											2023-04-03 00:12:51 +02:00
										 |  |  |         self.durations = durations | 
					
						
							| 
									
										
										
										
											2010-12-01 00:56:10 +00:00
										 |  |  |         if warnings is None and not sys.warnoptions: | 
					
						
							| 
									
										
										
										
											2015-03-06 11:35:29 +13:00
										 |  |  |             # even if DeprecationWarnings are ignored by default | 
					
						
							| 
									
										
										
										
											2010-12-01 00:56:10 +00:00
										 |  |  |             # print them anyway unless other warnings settings are | 
					
						
							|  |  |  |             # specified by the warnings arg or the -W python flag | 
					
						
							|  |  |  |             self.warnings = 'default' | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # here self.warnings is set either to the value passed | 
					
						
							|  |  |  |             # to the warnings args or to None. | 
					
						
							|  |  |  |             # If the user didn't pass a value self.warnings will | 
					
						
							|  |  |  |             # be None. This means that the behavior is unchanged | 
					
						
							|  |  |  |             # and depends on the values passed to -W. | 
					
						
							|  |  |  |             self.warnings = warnings | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |         self.defaultTest = defaultTest | 
					
						
							|  |  |  |         self.testRunner = testRunner | 
					
						
							|  |  |  |         self.testLoader = testLoader | 
					
						
							|  |  |  |         self.progName = os.path.basename(argv[0]) | 
					
						
							|  |  |  |         self.parseArgs(argv) | 
					
						
							|  |  |  |         self.runTests() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _print_help(self, *args, **kwargs): | 
					
						
							|  |  |  |         if self.module is None: | 
					
						
							|  |  |  |             print(self._main_parser.format_help()) | 
					
						
							|  |  |  |             print(MAIN_EXAMPLES % {'prog': self.progName}) | 
					
						
							|  |  |  |             self._discovery_parser.print_help() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             print(self._main_parser.format_help()) | 
					
						
							|  |  |  |             print(MODULE_EXAMPLES % {'prog': self.progName}) | 
					
						
							| 
									
										
										
										
											2010-12-19 03:59:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def parseArgs(self, argv): | 
					
						
							|  |  |  |         self._initArgParsers() | 
					
						
							|  |  |  |         if self.module is None: | 
					
						
							|  |  |  |             if len(argv) > 1 and argv[1].lower() == 'discover': | 
					
						
							|  |  |  |                 self._do_discovery(argv[2:]) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |             self._main_parser.parse_args(argv[1:], self) | 
					
						
							|  |  |  |             if not self.tests: | 
					
						
							|  |  |  |                 # this allows "python -m unittest -v" to still work for | 
					
						
							|  |  |  |                 # test discovery. | 
					
						
							|  |  |  |                 self._do_discovery([]) | 
					
						
							|  |  |  |                 return | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self._main_parser.parse_args(argv[1:], self) | 
					
						
							| 
									
										
										
										
											2010-12-19 03:59:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         if self.tests: | 
					
						
							|  |  |  |             self.testNames = _convert_names(self.tests) | 
					
						
							| 
									
										
										
										
											2010-12-19 03:59:10 +00:00
										 |  |  |             if __name__ == '__main__': | 
					
						
							|  |  |  |                 # to support python -m unittest ... | 
					
						
							|  |  |  |                 self.module = None | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         elif self.defaultTest is None: | 
					
						
							|  |  |  |             # createTests will load tests from self.module | 
					
						
							|  |  |  |             self.testNames = None | 
					
						
							|  |  |  |         elif isinstance(self.defaultTest, str): | 
					
						
							|  |  |  |             self.testNames = (self.defaultTest,) | 
					
						
							| 
									
										
										
										
											2010-12-19 03:59:10 +00:00
										 |  |  |         else: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |             self.testNames = list(self.defaultTest) | 
					
						
							| 
									
										
										
										
											2010-12-19 03:59:10 +00:00
										 |  |  |         self.createTests() | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 16:23:52 +01:00
										 |  |  |     def createTests(self, from_discovery=False, Loader=None): | 
					
						
							|  |  |  |         if self.testNamePatterns: | 
					
						
							|  |  |  |             self.testLoader.testNamePatterns = self.testNamePatterns | 
					
						
							|  |  |  |         if from_discovery: | 
					
						
							|  |  |  |             loader = self.testLoader if Loader is None else Loader() | 
					
						
							|  |  |  |             self.test = loader.discover(self.start, self.pattern, self.top) | 
					
						
							|  |  |  |         elif self.testNames is None: | 
					
						
							| 
									
										
											  
											
												Merged revisions 74779-74786,74793,74795,74811,74860-74861,74863,74876,74886,74896,74901,74903,74908,74912,74930,74933,74943,74946,74952-74955,75015,75019,75032,75068,75076,75095,75098,75102,75129,75139,75230 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r74779 | michael.foord | 2009-09-13 11:13:36 -0500 (Sun, 13 Sep 2009) | 1 line
  Change to tutorial wording for reading text / binary files on Windows. Issue #6301.
........
  r74780 | michael.foord | 2009-09-13 11:40:02 -0500 (Sun, 13 Sep 2009) | 1 line
  Objects that compare equal automatically pass or fail assertAlmostEqual and assertNotAlmostEqual tests on unittest.TestCase. Issue 6567.
........
  r74781 | michael.foord | 2009-09-13 11:46:19 -0500 (Sun, 13 Sep 2009) | 1 line
  Note that sys._getframe is not guaranteed to exist in all implementations of Python, and a corresponding note in inspect.currentframe. Issue 6712.
........
  r74782 | michael.foord | 2009-09-13 12:07:46 -0500 (Sun, 13 Sep 2009) | 1 line
  Tutorial tweaks. Issue 6849.
........
  r74783 | michael.foord | 2009-09-13 12:28:35 -0500 (Sun, 13 Sep 2009) | 1 line
  unittest.TestLoader.loadTestsFromName honors the loader suiteClass attribute. Issue 6866.
........
  r74784 | georg.brandl | 2009-09-13 13:15:07 -0500 (Sun, 13 Sep 2009) | 1 line
  Typo fix.
........
  r74785 | michael.foord | 2009-09-13 14:07:03 -0500 (Sun, 13 Sep 2009) | 1 line
  Test discovery in unittest will only attempt to import modules that are importable; i.e. their names are valid Python identifiers. If an import fails during discovery this will be recorded as an error and test discovery will continue. Issue 6568.
........
  r74786 | michael.foord | 2009-09-13 14:08:18 -0500 (Sun, 13 Sep 2009) | 1 line
  Remove an extraneous space in unittest documentation.
........
  r74793 | georg.brandl | 2009-09-14 09:50:47 -0500 (Mon, 14 Sep 2009) | 1 line
  #6908: fix association of hashlib hash attributes.
........
  r74795 | benjamin.peterson | 2009-09-14 22:36:26 -0500 (Mon, 14 Sep 2009) | 1 line
  Py_SetPythonHome uses static storage #6913
........
  r74811 | georg.brandl | 2009-09-15 15:26:59 -0500 (Tue, 15 Sep 2009) | 1 line
  Add Armin Ronacher.
........
  r74860 | benjamin.peterson | 2009-09-16 21:46:54 -0500 (Wed, 16 Sep 2009) | 1 line
  kill bare except
........
  r74861 | benjamin.peterson | 2009-09-16 22:18:28 -0500 (Wed, 16 Sep 2009) | 1 line
  pep 8 defaults
........
  r74863 | benjamin.peterson | 2009-09-16 22:27:33 -0500 (Wed, 16 Sep 2009) | 1 line
  rationalize a bit
........
  r74876 | georg.brandl | 2009-09-17 11:15:53 -0500 (Thu, 17 Sep 2009) | 1 line
  #6932: remove paragraph that advises relying on __del__ being called.
........
  r74886 | benjamin.peterson | 2009-09-17 16:33:46 -0500 (Thu, 17 Sep 2009) | 1 line
  use macros
........
  r74896 | georg.brandl | 2009-09-18 02:22:41 -0500 (Fri, 18 Sep 2009) | 1 line
  #6936: for interactive use, quit() is just fine.
........
  r74901 | georg.brandl | 2009-09-18 04:14:52 -0500 (Fri, 18 Sep 2009) | 1 line
  #6905: use better exception messages in inspect when the argument is of the wrong type.
........
  r74903 | georg.brandl | 2009-09-18 04:18:27 -0500 (Fri, 18 Sep 2009) | 1 line
  #6938: "ident" is always a string, so use a format code which works.
........
  r74908 | georg.brandl | 2009-09-18 08:57:11 -0500 (Fri, 18 Sep 2009) | 1 line
  Use str.format() to fix beginner's mistake with %-style string formatting.
........
  r74912 | georg.brandl | 2009-09-18 11:19:56 -0500 (Fri, 18 Sep 2009) | 1 line
  Optimize optimization and fix method name in docstring.
........
  r74930 | georg.brandl | 2009-09-18 16:21:41 -0500 (Fri, 18 Sep 2009) | 1 line
  #6925: rewrite docs for locals() and vars() a bit.
........
  r74933 | georg.brandl | 2009-09-18 16:35:59 -0500 (Fri, 18 Sep 2009) | 1 line
  #6930: clarify description about byteorder handling in UTF decoder routines.
........
  r74943 | georg.brandl | 2009-09-19 02:35:07 -0500 (Sat, 19 Sep 2009) | 1 line
  #6944: the argument to PyArg_ParseTuple should be a tuple, otherwise a SystemError is set.  Also clean up another usage of PyArg_ParseTuple.
........
  r74946 | georg.brandl | 2009-09-19 03:43:16 -0500 (Sat, 19 Sep 2009) | 1 line
  Update bug tracker reference.
........
  r74952 | georg.brandl | 2009-09-19 05:42:34 -0500 (Sat, 19 Sep 2009) | 1 line
  #6946: fix duplicate index entries for datetime classes.
........
  r74953 | georg.brandl | 2009-09-19 07:04:16 -0500 (Sat, 19 Sep 2009) | 1 line
  Fix references to threading.enumerate().
........
  r74954 | georg.brandl | 2009-09-19 08:13:56 -0500 (Sat, 19 Sep 2009) | 1 line
  Add Doug.
........
  r74955 | georg.brandl | 2009-09-19 08:20:49 -0500 (Sat, 19 Sep 2009) | 1 line
  Add Mark Summerfield.
........
  r75015 | georg.brandl | 2009-09-22 05:55:08 -0500 (Tue, 22 Sep 2009) | 1 line
  Fix encoding name.
........
  r75019 | vinay.sajip | 2009-09-22 12:23:41 -0500 (Tue, 22 Sep 2009) | 1 line
  Fixed a typo, and added sections on optimization and using arbitrary objects as messages.
........
  r75032 | benjamin.peterson | 2009-09-22 17:15:28 -0500 (Tue, 22 Sep 2009) | 1 line
  fix typos/rephrase
........
  r75068 | benjamin.peterson | 2009-09-25 21:57:59 -0500 (Fri, 25 Sep 2009) | 1 line
  comment out ugly xxx
........
  r75076 | vinay.sajip | 2009-09-26 09:53:32 -0500 (Sat, 26 Sep 2009) | 1 line
  Tidied up name of parameter in StreamHandler
........
  r75095 | michael.foord | 2009-09-27 14:15:41 -0500 (Sun, 27 Sep 2009) | 1 line
  Test creation moved from TestProgram.parseArgs to TestProgram.createTests exclusively. Issue 6956.
........
  r75098 | michael.foord | 2009-09-27 15:08:23 -0500 (Sun, 27 Sep 2009) | 1 line
  Documentation improvement for load_tests protocol in unittest. Issue 6515.
........
  r75102 | skip.montanaro | 2009-09-27 21:12:27 -0500 (Sun, 27 Sep 2009) | 3 lines
  Patch from Thomas Barr so that csv.Sniffer will set doublequote property.
  Closes issue 6606.
........
  r75129 | vinay.sajip | 2009-09-29 02:08:54 -0500 (Tue, 29 Sep 2009) | 1 line
  Issue #7014: logging: Improved IronPython 2.6 compatibility.
........
  r75139 | raymond.hettinger | 2009-09-29 13:53:24 -0500 (Tue, 29 Sep 2009) | 3 lines
  Issue 7008: Better document str.title and show how to work around the apostrophe problem.
........
  r75230 | benjamin.peterson | 2009-10-04 08:38:38 -0500 (Sun, 04 Oct 2009) | 1 line
  test logging
........
											
										 
											2009-10-04 14:49:41 +00:00
										 |  |  |             self.test = self.testLoader.loadTestsFromModule(self.module) | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             self.test = self.testLoader.loadTestsFromNames(self.testNames, | 
					
						
							|  |  |  |                                                            self.module) | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _initArgParsers(self): | 
					
						
							|  |  |  |         parent_parser = self._getParentArgParser() | 
					
						
							|  |  |  |         self._main_parser = self._getMainArgParser(parent_parser) | 
					
						
							|  |  |  |         self._discovery_parser = self._getDiscoveryArgParser(parent_parser) | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _getParentArgParser(self): | 
					
						
							|  |  |  |         parser = argparse.ArgumentParser(add_help=False) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser.add_argument('-v', '--verbose', dest='verbosity', | 
					
						
							|  |  |  |                             action='store_const', const=2, | 
					
						
							|  |  |  |                             help='Verbose output') | 
					
						
							|  |  |  |         parser.add_argument('-q', '--quiet', dest='verbosity', | 
					
						
							|  |  |  |                             action='store_const', const=0, | 
					
						
							|  |  |  |                             help='Quiet output') | 
					
						
							| 
									
										
										
										
											2015-03-06 13:46:35 +13:00
										 |  |  |         parser.add_argument('--locals', dest='tb_locals', | 
					
						
							|  |  |  |                             action='store_true', | 
					
						
							|  |  |  |                             help='Show local variables in tracebacks') | 
					
						
							| 
									
										
										
										
											2023-04-03 00:12:51 +02:00
										 |  |  |         parser.add_argument('--durations', dest='durations', type=int, | 
					
						
							|  |  |  |                             default=None, metavar="N", | 
					
						
							|  |  |  |                             help='Show the N slowest test cases (N=0 for all)') | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  |         if self.failfast is None: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |             parser.add_argument('-f', '--failfast', dest='failfast', | 
					
						
							|  |  |  |                                 action='store_true', | 
					
						
							|  |  |  |                                 help='Stop on first fail or error') | 
					
						
							|  |  |  |             self.failfast = False | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  |         if self.catchbreak is None: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |             parser.add_argument('-c', '--catch', dest='catchbreak', | 
					
						
							|  |  |  |                                 action='store_true', | 
					
						
							| 
									
										
										
										
											2015-09-12 17:45:25 +03:00
										 |  |  |                                 help='Catch Ctrl-C and display results so far') | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |             self.catchbreak = False | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  |         if self.buffer is None: | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |             parser.add_argument('-b', '--buffer', dest='buffer', | 
					
						
							|  |  |  |                                 action='store_true', | 
					
						
							|  |  |  |                                 help='Buffer stdout and stderr during tests') | 
					
						
							|  |  |  |             self.buffer = False | 
					
						
							| 
									
										
										
										
											2017-11-25 16:23:52 +01:00
										 |  |  |         if self.testNamePatterns is None: | 
					
						
							|  |  |  |             parser.add_argument('-k', dest='testNamePatterns', | 
					
						
							|  |  |  |                                 action='append', type=_convert_select_pattern, | 
					
						
							|  |  |  |                                 help='Only run tests which match the given substring') | 
					
						
							|  |  |  |             self.testNamePatterns = [] | 
					
						
							| 
									
										
										
										
											2012-03-12 13:53:04 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         return parser | 
					
						
							| 
									
										
										
										
											2013-02-11 00:18:07 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _getMainArgParser(self, parent): | 
					
						
							| 
									
										
										
										
											2025-05-05 20:46:46 +03:00
										 |  |  |         parser = argparse.ArgumentParser(parents=[parent], color=True) | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         parser.prog = self.progName | 
					
						
							|  |  |  |         parser.print_help = self._print_help | 
					
						
							| 
									
										
										
										
											2012-03-12 15:24:46 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         parser.add_argument('tests', nargs='*', | 
					
						
							|  |  |  |                             help='a list of any number of test modules, ' | 
					
						
							|  |  |  |                             'classes and test methods.') | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         return parser | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _getDiscoveryArgParser(self, parent): | 
					
						
							| 
									
										
										
										
											2025-05-05 20:46:46 +03:00
										 |  |  |         parser = argparse.ArgumentParser(parents=[parent], color=True) | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         parser.prog = '%s discover' % self.progName | 
					
						
							|  |  |  |         parser.epilog = ('For test discovery all test modules must be ' | 
					
						
							|  |  |  |                          'importable from the top level directory of the ' | 
					
						
							|  |  |  |                          'project.') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         parser.add_argument('-s', '--start-directory', dest='start', | 
					
						
							|  |  |  |                             help="Directory to start discovery ('.' default)") | 
					
						
							|  |  |  |         parser.add_argument('-p', '--pattern', dest='pattern', | 
					
						
							|  |  |  |                             help="Pattern to match tests ('test*.py' default)") | 
					
						
							|  |  |  |         parser.add_argument('-t', '--top-level-directory', dest='top', | 
					
						
							|  |  |  |                             help='Top level directory of project (defaults to ' | 
					
						
							|  |  |  |                                  'start directory)') | 
					
						
							|  |  |  |         for arg in ('start', 'pattern', 'top'): | 
					
						
							|  |  |  |             parser.add_argument(arg, nargs='?', | 
					
						
							|  |  |  |                                 default=argparse.SUPPRESS, | 
					
						
							|  |  |  |                                 help=argparse.SUPPRESS) | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |         return parser | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 12:37:28 +03:00
										 |  |  |     def _do_discovery(self, argv, Loader=None): | 
					
						
							|  |  |  |         self.start = '.' | 
					
						
							|  |  |  |         self.pattern = 'test*.py' | 
					
						
							|  |  |  |         self.top = None | 
					
						
							|  |  |  |         if argv is not None: | 
					
						
							|  |  |  |             # handle command line args for test discovery | 
					
						
							|  |  |  |             if self._discovery_parser is None: | 
					
						
							|  |  |  |                 # for testing | 
					
						
							|  |  |  |                 self._initArgParsers() | 
					
						
							|  |  |  |             self._discovery_parser.parse_args(argv, self) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-25 16:23:52 +01:00
										 |  |  |         self.createTests(from_discovery=True, Loader=Loader) | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def runTests(self): | 
					
						
							| 
									
										
										
										
											2010-03-27 13:25:41 +00:00
										 |  |  |         if self.catchbreak: | 
					
						
							|  |  |  |             installHandler() | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |         if self.testRunner is None: | 
					
						
							|  |  |  |             self.testRunner = runner.TextTestRunner | 
					
						
							|  |  |  |         if isinstance(self.testRunner, type): | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2015-03-06 13:46:35 +13:00
										 |  |  |                 try: | 
					
						
							|  |  |  |                     testRunner = self.testRunner(verbosity=self.verbosity, | 
					
						
							|  |  |  |                                                  failfast=self.failfast, | 
					
						
							|  |  |  |                                                  buffer=self.buffer, | 
					
						
							|  |  |  |                                                  warnings=self.warnings, | 
					
						
							| 
									
										
										
										
											2023-04-03 00:12:51 +02:00
										 |  |  |                                                  tb_locals=self.tb_locals, | 
					
						
							|  |  |  |                                                  durations=self.durations) | 
					
						
							| 
									
										
										
										
											2015-03-06 13:46:35 +13:00
										 |  |  |                 except TypeError: | 
					
						
							| 
									
										
										
										
											2023-04-03 00:12:51 +02:00
										 |  |  |                     # didn't accept the tb_locals or durations argument | 
					
						
							| 
									
										
										
										
											2015-03-06 13:46:35 +13:00
										 |  |  |                     testRunner = self.testRunner(verbosity=self.verbosity, | 
					
						
							|  |  |  |                                                  failfast=self.failfast, | 
					
						
							|  |  |  |                                                  buffer=self.buffer, | 
					
						
							|  |  |  |                                                  warnings=self.warnings) | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |             except TypeError: | 
					
						
							| 
									
										
											  
											
												Merged revisions 79464,79471,79623,79626,79630,79632,79643,79648-79649,79679,79685,79711,79761,79774,79777,79792-79794,79877,79898-79900 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
  r79464 | michael.foord | 2010-03-27 07:55:19 -0500 (Sat, 27 Mar 2010) | 1 line
  A fix for running unittest tests on platforms without the audioop module (e.g. jython and IronPython)
........
  r79471 | michael.foord | 2010-03-27 14:10:11 -0500 (Sat, 27 Mar 2010) | 4 lines
  Addition of delta keyword argument to unittest.TestCase.assertAlmostEquals and assertNotAlmostEquals
  This allows the comparison of objects by specifying a maximum difference; this includes the comparing of non-numeric objects that don't support rounding.
........
  r79623 | michael.foord | 2010-04-02 16:42:47 -0500 (Fri, 02 Apr 2010) | 1 line
  Addition of -b command line option to unittest for buffering stdout and stderr during test runs.
........
  r79626 | michael.foord | 2010-04-02 17:08:29 -0500 (Fri, 02 Apr 2010) | 1 line
  TestResult stores original sys.stdout and tests no longer use sys.__stdout__ (etc) in tests for unittest -b command line option
........
  r79630 | michael.foord | 2010-04-02 17:30:56 -0500 (Fri, 02 Apr 2010) | 1 line
  unittest tests no longer replace the sys.stdout put in place by regrtest
........
  r79632 | michael.foord | 2010-04-02 17:55:59 -0500 (Fri, 02 Apr 2010) | 1 line
  Issue #8038: Addition of unittest.TestCase.assertNotRegexpMatches
........
  r79643 | michael.foord | 2010-04-02 20:15:21 -0500 (Fri, 02 Apr 2010) | 1 line
  Support dotted module names for test discovery paths in unittest. Issue 8038.
........
  r79648 | michael.foord | 2010-04-02 21:21:39 -0500 (Fri, 02 Apr 2010) | 1 line
  Cross platform unittest.TestResult newline handling when buffering stdout / stderr.
........
  r79649 | michael.foord | 2010-04-02 21:33:55 -0500 (Fri, 02 Apr 2010) | 1 line
  Another attempt at a fix for unittest.test.test_result for windows line endings
........
  r79679 | michael.foord | 2010-04-03 09:52:18 -0500 (Sat, 03 Apr 2010) | 1 line
  Adding -b command line option to the unittest usage message.
........
  r79685 | michael.foord | 2010-04-03 10:20:00 -0500 (Sat, 03 Apr 2010) | 1 line
  Minor tweak to unittest command line usage message
........
  r79711 | michael.foord | 2010-04-03 12:03:11 -0500 (Sat, 03 Apr 2010) | 1 line
  Documenting new features in unittest
........
  r79761 | michael.foord | 2010-04-04 17:41:54 -0500 (Sun, 04 Apr 2010) | 1 line
  unittest documentation formatting changes
........
  r79774 | michael.foord | 2010-04-04 18:28:44 -0500 (Sun, 04 Apr 2010) | 1 line
  Adding documentation for new unittest.main() parameters
........
  r79777 | michael.foord | 2010-04-04 19:39:50 -0500 (Sun, 04 Apr 2010) | 1 line
  Document signal handling functions in unittest.rst
........
  r79792 | michael.foord | 2010-04-05 05:26:26 -0500 (Mon, 05 Apr 2010) | 1 line
  Documentation fixes for unittest
........
  r79793 | michael.foord | 2010-04-05 05:28:27 -0500 (Mon, 05 Apr 2010) | 1 line
  Furterh documentation fix for unittest.rst
........
  r79794 | michael.foord | 2010-04-05 05:30:14 -0500 (Mon, 05 Apr 2010) | 1 line
  Further documentation fix for unittest.rst
........
  r79877 | michael.foord | 2010-04-06 18:18:16 -0500 (Tue, 06 Apr 2010) | 1 line
  Fix module directory finding logic for dotted paths in unittest test discovery.
........
  r79898 | michael.foord | 2010-04-07 18:04:22 -0500 (Wed, 07 Apr 2010) | 1 line
  unittest.result.TestResult does not create its buffers until they're used. It uses StringIO not cStringIO. Issue 8333.
........
  r79899 | michael.foord | 2010-04-07 19:04:24 -0500 (Wed, 07 Apr 2010) | 1 line
  Switch regrtest to use StringIO instead of cStringIO for test_multiprocessing on Windows. Issue 8333.
........
  r79900 | michael.foord | 2010-04-07 23:33:20 -0500 (Wed, 07 Apr 2010) | 1 line
  Correction of unittest documentation typos and omissions
........
											
										 
											2010-04-11 20:43:16 +00:00
										 |  |  |                 # didn't accept the verbosity, buffer or failfast arguments | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  |                 testRunner = self.testRunner() | 
					
						
							|  |  |  |         else: | 
					
						
							|  |  |  |             # it is assumed to be a TestRunner instance | 
					
						
							|  |  |  |             testRunner = self.testRunner | 
					
						
							|  |  |  |         self.result = testRunner.run(self.test) | 
					
						
							|  |  |  |         if self.exit: | 
					
						
							| 
									
										
										
										
											2024-01-09 11:50:01 -08:00
										 |  |  |             if self.result.testsRun == 0 and len(self.result.skipped) == 0: | 
					
						
							| 
									
										
										
										
											2023-04-26 18:28:46 -07:00
										 |  |  |                 sys.exit(_NO_TESTS_EXITCODE) | 
					
						
							|  |  |  |             elif self.result.wasSuccessful(): | 
					
						
							|  |  |  |                 sys.exit(0) | 
					
						
							|  |  |  |             else: | 
					
						
							|  |  |  |                 sys.exit(1) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-19 21:01:52 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | main = TestProgram |