Issue #22806: Add ``python -m test --list-tests`` command to list tests.

This commit is contained in:
Victor Stinner 2015-10-03 00:21:12 +02:00
parent 076fc872bc
commit 5f9d3acc5e
4 changed files with 49 additions and 26 deletions

View File

@ -1,5 +1,4 @@
import argparse import argparse
import faulthandler
import os import os
from test import support from test import support
@ -234,6 +233,9 @@ def _create_parser():
group.add_argument('-F', '--forever', action='store_true', group.add_argument('-F', '--forever', action='store_true',
help='run the specified tests in a loop, until an ' help='run the specified tests in a loop, until an '
'error happens') 'error happens')
group.add_argument('--list-tests', action='store_true',
help="only write the name of tests that will be run, "
"don't execute them")
parser.add_argument('args', nargs=argparse.REMAINDER, parser.add_argument('args', nargs=argparse.REMAINDER,
help=argparse.SUPPRESS) help=argparse.SUPPRESS)
@ -301,12 +303,7 @@ def _parse_args(args, **kwargs):
if ns.quiet: if ns.quiet:
ns.verbose = 0 ns.verbose = 0
if ns.timeout is not None: if ns.timeout is not None:
if hasattr(faulthandler, 'dump_traceback_later'): if ns.timeout <= 0:
if ns.timeout <= 0:
ns.timeout = None
else:
print("Warning: The timeout option requires "
"faulthandler.dump_traceback_later")
ns.timeout = None ns.timeout = None
if ns.use_mp is not None: if ns.use_mp is not None:
if ns.use_mp <= 0: if ns.use_mp <= 0:

View File

@ -1,3 +1,4 @@
import faulthandler
import os import os
import platform import platform
import random import random
@ -110,8 +111,13 @@ class Regrtest:
def parse_args(self, kwargs): def parse_args(self, kwargs):
ns = _parse_args(sys.argv[1:], **kwargs) ns = _parse_args(sys.argv[1:], **kwargs)
if ns.timeout and not hasattr(faulthandler, 'dump_traceback_later'):
print("Warning: The timeout option requires "
"faulthandler.dump_traceback_later", file=sys.stderr)
ns.timeout = None
if ns.threshold is not None and gc is None: if ns.threshold is not None and gc is None:
print('No GC available, ignore --threshold.') print('No GC available, ignore --threshold.', file=sys.stderr)
ns.threshold = None ns.threshold = None
if ns.findleaks: if ns.findleaks:
@ -122,7 +128,8 @@ class Regrtest:
pass pass
#gc.set_debug(gc.DEBUG_SAVEALL) #gc.set_debug(gc.DEBUG_SAVEALL)
else: else:
print('No GC available, disabling --findleaks') print('No GC available, disabling --findleaks',
file=sys.stderr)
ns.findleaks = False ns.findleaks = False
# Strip .py extensions. # Strip .py extensions.
@ -163,20 +170,6 @@ class Regrtest:
nottests.add(arg) nottests.add(arg)
self.ns.args = [] self.ns.args = []
# For a partial run, we do not need to clutter the output.
if (self.ns.verbose
or self.ns.header
or not (self.ns.quiet or self.ns.single
or self.tests or self.ns.args)):
# Print basic platform information
print("==", platform.python_implementation(), *sys.version.split())
print("== ", platform.platform(aliased=True),
"%s-endian" % sys.byteorder)
print("== ", "hash algorithm:", sys.hash_info.algorithm,
"64bit" if sys.maxsize > 2**32 else "32bit")
print("== ", os.getcwd())
print("Testing with flags:", sys.flags)
# if testdir is set, then we are not running the python tests suite, so # if testdir is set, then we are not running the python tests suite, so
# don't add default tests to be executed or skipped (pass empty values) # don't add default tests to be executed or skipped (pass empty values)
if self.ns.testdir: if self.ns.testdir:
@ -199,15 +192,18 @@ class Regrtest:
del self.selected[:self.selected.index(self.ns.start)] del self.selected[:self.selected.index(self.ns.start)]
except ValueError: except ValueError:
print("Couldn't find starting test (%s), using all tests" print("Couldn't find starting test (%s), using all tests"
% self.ns.start) % self.ns.start, file=sys.stderr)
if self.ns.randomize: if self.ns.randomize:
if self.ns.random_seed is None: if self.ns.random_seed is None:
self.ns.random_seed = random.randrange(10000000) self.ns.random_seed = random.randrange(10000000)
random.seed(self.ns.random_seed) random.seed(self.ns.random_seed)
print("Using random seed", self.ns.random_seed)
random.shuffle(self.selected) random.shuffle(self.selected)
def list_tests(self):
for name in self.selected:
print(name)
def rerun_failed_tests(self): def rerun_failed_tests(self):
self.ns.verbose = True self.ns.verbose = True
self.ns.failfast = False self.ns.failfast = False
@ -315,6 +311,23 @@ class Regrtest:
return return
def run_tests(self): def run_tests(self):
# For a partial run, we do not need to clutter the output.
if (self.ns.verbose
or self.ns.header
or not (self.ns.quiet or self.ns.single
or self.tests or self.ns.args)):
# Print basic platform information
print("==", platform.python_implementation(), *sys.version.split())
print("== ", platform.platform(aliased=True),
"%s-endian" % sys.byteorder)
print("== ", "hash algorithm:", sys.hash_info.algorithm,
"64bit" if sys.maxsize > 2**32 else "32bit")
print("== ", os.getcwd())
print("Testing with flags:", sys.flags)
if self.ns.randomize:
print("Using random seed", self.ns.random_seed)
if self.ns.forever: if self.ns.forever:
self.tests = self._test_forever(list(self.selected)) self.tests = self._test_forever(list(self.selected))
self.test_count = '' self.test_count = ''
@ -359,8 +372,12 @@ class Regrtest:
setup_tests(self.ns) setup_tests(self.ns)
self.find_tests(tests) self.find_tests(tests)
self.run_tests()
if self.ns.list_tests:
self.list_tests()
sys.exit(0)
self.run_tests()
self.display_result() self.display_result()
if self.ns.verbose2 and self.bad: if self.ns.verbose2 and self.bad:

View File

@ -672,6 +672,13 @@ class ArgsTestCase(BaseTestCase):
reflog = fp.read() reflog = fp.read()
self.assertEqual(reflog, line2) self.assertEqual(reflog, line2)
def test_list_tests(self):
# test --list-tests
tests = [self.create_test() for i in range(5)]
output = self.run_tests('--list-tests', *tests)
self.assertEqual(output.rstrip().splitlines(),
tests)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()

View File

@ -170,6 +170,8 @@ Documentation
Tests Tests
----- -----
- Issue #22806: Add ``python -m test --list-tests`` command to list tests.
- Issue #18174: ``python -m test --huntrleaks ...`` now also checks for leak of - Issue #18174: ``python -m test --huntrleaks ...`` now also checks for leak of
file descriptors. Patch written by Richard Oudkerk. file descriptors. Patch written by Richard Oudkerk.