Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b9c73e8

Browse files
committed
Issue #12626: In regrtest, allow to filter tests using a glob filter
with the `-m` (or `--match`) option. This works with all test cases using the unittest module. This is useful with long test suites such as test_io or test_subprocess.
1 parent def3543 commit b9c73e8

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

Lib/test/regrtest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
-f/--fromfile -- read names of tests to run from a file (see below)
3939
-x/--exclude -- arguments are tests to *exclude*
4040
-s/--single -- single step through a set of tests (see below)
41+
-m/--match PAT -- match test cases and methods with glob pattern PAT
4142
-G/--failfast -- fail as soon as a test fails (only with -v or -W)
4243
-u/--use RES1,RES2,...
4344
-- specify which special resource intensive tests to run
@@ -242,7 +243,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
242243
findleaks=False, use_resources=None, trace=False, coverdir='coverage',
243244
runleaks=False, huntrleaks=False, verbose2=False, print_slow=False,
244245
random_seed=None, use_mp=None, verbose3=False, forever=False,
245-
header=False, failfast=False):
246+
header=False, failfast=False, match_tests=None):
246247
"""Execute a test suite.
247248
248249
This also parses command-line options and modifies its behavior
@@ -270,13 +271,13 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
270271

271272
support.record_original_stdout(sys.stdout)
272273
try:
273-
opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:nj:G',
274+
opts, args = getopt.getopt(sys.argv[1:], 'hvqxsSrf:lu:t:TD:NLR:FwWM:nj:Gm:',
274275
['help', 'verbose', 'verbose2', 'verbose3', 'quiet',
275276
'exclude', 'single', 'slow', 'random', 'fromfile', 'findleaks',
276277
'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
277278
'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
278279
'multiprocess=', 'coverage', 'slaveargs=', 'forever', 'debug',
279-
'start=', 'nowindows', 'header', 'failfast'])
280+
'start=', 'nowindows', 'header', 'failfast', 'match'])
280281
except getopt.error as msg:
281282
usage(msg)
282283

@@ -318,6 +319,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
318319
random_seed = int(a)
319320
elif o in ('-f', '--fromfile'):
320321
fromfile = a
322+
elif o in ('-m', '--match'):
323+
match_tests = a
321324
elif o in ('-l', '--findleaks'):
322325
findleaks = True
323326
elif o in ('-L', '--runleaks'):
@@ -551,7 +554,7 @@ def tests_and_args():
551554
(test, verbose, quiet),
552555
dict(huntrleaks=huntrleaks, use_resources=use_resources,
553556
debug=debug, output_on_failure=verbose3,
554-
failfast=failfast)
557+
failfast=failfast, match_tests=match_tests)
555558
)
556559
yield (test, args_tuple)
557560
pending = tests_and_args()
@@ -630,7 +633,7 @@ def work():
630633
try:
631634
result = runtest(test, verbose, quiet, huntrleaks, debug,
632635
output_on_failure=verbose3,
633-
failfast=failfast)
636+
failfast=failfast, match_tests=match_tests)
634637
accumulate_result(test, result)
635638
except KeyboardInterrupt:
636639
interrupted = True
@@ -777,7 +780,7 @@ def restore_stdout():
777780

778781
def runtest(test, verbose, quiet,
779782
huntrleaks=False, debug=False, use_resources=None,
780-
output_on_failure=False, failfast=False):
783+
output_on_failure=False, failfast=False, match_tests=None):
781784
"""Run a single test.
782785
783786
test -- the name of the test
@@ -800,6 +803,7 @@ def runtest(test, verbose, quiet,
800803
if use_resources is not None:
801804
support.use_resources = use_resources
802805
try:
806+
support.match_tests = match_tests
803807
if failfast:
804808
support.failfast = True
805809
if output_on_failure:

Lib/test/support.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import imp
2222
import time
2323
import sysconfig
24+
import fnmatch
2425
import logging.handlers
2526

2627
try:
@@ -177,6 +178,7 @@ def get_attribute(obj, name):
177178
# small sizes, to make sure they work.)
178179
real_max_memuse = 0
179180
failfast = False
181+
match_tests = None
180182

181183
# _original_stdout is meant to hold stdout at the time regrtest began.
182184
# This may be "the real" stdout, or IDLE's emulation of stdout, or whatever.
@@ -1183,6 +1185,18 @@ def check_impl_detail(**guards):
11831185
return guards.get(platform.python_implementation().lower(), default)
11841186

11851187

1188+
def _filter_suite(suite, pred):
1189+
"""Recursively filter test cases in a suite based on a predicate."""
1190+
newtests = []
1191+
for test in suite._tests:
1192+
if isinstance(test, unittest.TestSuite):
1193+
_filter_suite(test, pred)
1194+
newtests.append(test)
1195+
else:
1196+
if pred(test):
1197+
newtests.append(test)
1198+
suite._tests = newtests
1199+
11861200

11871201
def _run_suite(suite):
11881202
"""Run tests from a unittest.TestSuite-derived class."""
@@ -1218,6 +1232,14 @@ def run_unittest(*classes):
12181232
suite.addTest(cls)
12191233
else:
12201234
suite.addTest(unittest.makeSuite(cls))
1235+
def case_pred(test):
1236+
if match_tests is None:
1237+
return True
1238+
for name in test.id().split("."):
1239+
if fnmatch.fnmatchcase(name, match_tests):
1240+
return True
1241+
return False
1242+
_filter_suite(suite, case_pred)
12211243
_run_suite(suite)
12221244

12231245

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ Tools/Demos
137137
Tests
138138
-----
139139

140+
- Issue #12626: In regrtest, allow to filter tests using a glob filter
141+
with the ``-m`` (or ``--match``) option. This works with all test cases
142+
using the unittest module. This is useful with long test suites
143+
such as test_io or test_subprocess.
144+
140145
- Issue #12624: It is now possible to fail after the first failure when
141146
running in verbose mode (``-v`` or ``-W``), by using the ``--failfast``
142147
(or ``-G``) option to regrtest. This is useful with long test suites

0 commit comments

Comments
 (0)