Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
bpo-30523: regrtest: Add --list-cases option
* Add --list-cases option to regrtest
* Add get_abs_module() function, use it in list_cases()

Co-Authored-By: Louie Lu <git@louie.lu>
  • Loading branch information
vstinner and mlouielu committed Jun 16, 2017
commit 2370ee947c01f784c19d78747260db9e53fda2fe
54 changes: 44 additions & 10 deletions Lib/test/regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
(instead of the Python stdlib test suite)
--list-tests -- only write the name of tests that will be run,
don't execute them
--list-cases -- only write the name of test cases that will be run,
don't execute them


Additional Option Details:
Expand Down Expand Up @@ -324,8 +326,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo',
'failfast', 'match=', 'testdir=', 'list-tests', 'coverage',
'matchfile='])
'failfast', 'match=', 'testdir=', 'list-tests', 'list-cases',
'coverage', 'matchfile='])
except getopt.error, msg:
usage(2, msg)

Expand All @@ -336,6 +338,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
use_resources = []
slaveargs = None
list_tests = False
list_cases_opt = False
for o, a in opts:
if o in ('-h', '--help'):
usage(0)
Expand Down Expand Up @@ -434,6 +437,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
testdir = a
elif o == '--list-tests':
list_tests = True
elif o == '--list-cases':
list_cases_opt = True
else:
print >>sys.stderr, ("No handler for option {}. Please "
"report this as a bug at http://bugs.python.org.").format(o)
Expand Down Expand Up @@ -552,6 +557,10 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
print(name)
sys.exit(0)

if list_cases_opt:
list_cases(testdir, selected)
sys.exit(0)

if trace:
import trace
tracer = trace.Trace(trace=False, count=True)
Expand Down Expand Up @@ -1142,11 +1151,7 @@ def runtest_inner(test, verbose, quiet, huntrleaks=False, pgo=False, testdir=Non
try:
if capture_stdout:
sys.stdout = capture_stdout
if test.startswith('test.') or testdir:
abstest = test
else:
# Always import it from the test package
abstest = 'test.' + test
abstest = get_abs_module(testdir, test)
clear_caches()
with saved_test_environment(test, verbose, quiet, pgo) as environment:
start_time = time.time()
Expand Down Expand Up @@ -1470,7 +1475,7 @@ def count(n, word):
else:
return "%d %ss" % (n, word)

def printlist(x, width=70, indent=4):
def printlist(x, width=70, indent=4, file=None):
"""Print the elements of iterable x to stdout.

Optional arg width (default 70) is the maximum line length.
Expand All @@ -1481,8 +1486,37 @@ def printlist(x, width=70, indent=4):
from textwrap import fill
blanks = ' ' * indent
# Print the sorted list: 'x' may be a '--random' list or a set()
print fill(' '.join(str(elt) for elt in sorted(x)), width,
initial_indent=blanks, subsequent_indent=blanks)
print >>file, fill(' '.join(str(elt) for elt in sorted(x)), width,
initial_indent=blanks, subsequent_indent=blanks)

def get_abs_module(testdir, test):
if test.startswith('test.') or testdir:
return test
else:
# Always import it from the test package
return 'test.' + test

def _list_cases(suite):
for test in suite:
if isinstance(test, unittest.TestSuite):
_list_cases(test)
elif isinstance(test, unittest.TestCase):
print(test.id())

def list_cases(testdir, selected):
skipped = []
for test in selected:
abstest = get_abs_module(testdir, test)
try:
suite = unittest.defaultTestLoader.loadTestsFromName(abstest)
_list_cases(suite)
except unittest.SkipTest:
skipped.append(test)

if skipped:
print >>sys.stderr
print >>sys.stderr, count(len(skipped), "test"), "skipped:"
printlist(skipped, file=sys.stderr)

# Map sys.platform to a string containing the basenames of tests
# expected to be skipped on that platform.
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,27 @@ def test_main():
subset = ['test_method1', 'test_method3']
self.assertEqual(methods, subset)

def test_list_cases(self):
# test --list-cases
code = textwrap.dedent("""
import unittest
from test import support

class Tests(unittest.TestCase):
def test_method1(self):
pass
def test_method2(self):
pass

def test_main():
support.run_unittest(Tests)
""")
testname = self.create_test(code=code)
all_methods = ['%s.Tests.test_method1' % testname,
'%s.Tests.test_method2' % testname]
output = self.run_tests('--list-cases', testname)
self.assertEqual(output.splitlines(), all_methods)


def test_main():
support.run_unittest(ProgramsTestCase, ArgsTestCase)
Expand Down