bpo-30540, bpo-30523: Add --matchfile and --list-cases options to reg… · python/cpython@24c2c20 (original) (raw)
`@@ -38,6 +38,7 @@
`
38
38
`-x/--exclude -- arguments are tests to exclude
`
39
39
`-s/--single -- single step through a set of tests (see below)
`
40
40
`-m/--match PAT -- match test cases and methods with glob pattern PAT
`
``
41
`+
--matchfile FILENAME -- filters tests using a text file, one pattern per line
`
41
42
`-G/--failfast -- fail as soon as a test fails (only with -v or -W)
`
42
43
`-u/--use RES1,RES2,...
`
43
44
` -- specify which special resource intensive tests to run
`
`@@ -64,6 +65,8 @@
`
64
65
` (instead of the Python stdlib test suite)
`
65
66
`--list-tests -- only write the name of tests that will be run,
`
66
67
` don't execute them
`
``
68
`+
--list-cases -- only write the name of test cases that will be run,
`
``
69
`+
don't execute them
`
67
70
``
68
71
``
69
72
`Additional Option Details:
`
157
160
`To enable all resources except one, use '-uall,-'. For
`
158
161
`example, to run all the tests except for the bsddb tests, give the
`
159
162
`option '-uall,-bsddb'.
`
``
163
+
``
164
`+
--matchfile filters tests using a text file, one pattern per line.
`
``
165
`+
Pattern examples:
`
``
166
+
``
167
`+
- test method: test_stat_attributes
`
``
168
`+
- test class: FileTests
`
``
169
`+
- test identifier: test_os.FileTests.test_stat_attributes
`
160
170
`"""
`
161
171
``
162
172
`import StringIO
`
`@@ -316,7 +326,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
`
316
326
`'use=', 'threshold=', 'trace', 'coverdir=', 'nocoverdir',
`
317
327
`'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
`
318
328
`'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo',
`
319
``
`-
'failfast', 'match=', 'testdir=', 'list-tests', 'coverage'])
`
``
329
`+
'failfast', 'match=', 'testdir=', 'list-tests', 'list-cases',
`
``
330
`+
'coverage', 'matchfile='])
`
320
331
`except getopt.error, msg:
`
321
332
`usage(2, msg)
`
322
333
``
`@@ -327,6 +338,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
`
327
338
`use_resources = []
`
328
339
`slaveargs = None
`
329
340
`list_tests = False
`
``
341
`+
list_cases_opt = False
`
330
342
`for o, a in opts:
`
331
343
`if o in ('-h', '--help'):
`
332
344
`usage(0)
`
`@@ -354,7 +366,16 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
`
354
366
`elif o in ('-f', '--fromfile'):
`
355
367
`fromfile = a
`
356
368
`elif o in ('-m', '--match'):
`
357
``
`-
match_tests = a
`
``
369
`+
if match_tests is None:
`
``
370
`+
match_tests = []
`
``
371
`+
match_tests.append(a)
`
``
372
`+
elif o == '--matchfile':
`
``
373
`+
if match_tests is None:
`
``
374
`+
match_tests = []
`
``
375
`+
filename = os.path.join(test_support.SAVEDCWD, a)
`
``
376
`+
with open(filename) as fp:
`
``
377
`+
for line in fp:
`
``
378
`+
match_tests.append(line.strip())
`
358
379
`elif o in ('-l', '--findleaks'):
`
359
380
`findleaks = True
`
360
381
`elif o in ('-L', '--runleaks'):
`
`@@ -416,6 +437,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
`
416
437
`testdir = a
`
417
438
`elif o == '--list-tests':
`
418
439
`list_tests = True
`
``
440
`+
elif o == '--list-cases':
`
``
441
`+
list_cases_opt = True
`
419
442
`else:
`
420
443
`print >>sys.stderr, ("No handler for option {}. Please "
`
421
444
`"report this as a bug at http://bugs.python.org.").format(o)
`
`@@ -534,6 +557,10 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
`
534
557
`print(name)
`
535
558
`sys.exit(0)
`
536
559
``
``
560
`+
if list_cases_opt:
`
``
561
`+
list_cases(testdir, selected)
`
``
562
`+
sys.exit(0)
`
``
563
+
537
564
`if trace:
`
538
565
`import trace
`
539
566
`tracer = trace.Trace(trace=False, count=True)
`
`@@ -1124,11 +1151,7 @@ def runtest_inner(test, verbose, quiet, huntrleaks=False, pgo=False, testdir=Non
`
1124
1151
`try:
`
1125
1152
`if capture_stdout:
`
1126
1153
`sys.stdout = capture_stdout
`
1127
``
`-
if test.startswith('test.') or testdir:
`
1128
``
`-
abstest = test
`
1129
``
`-
else:
`
1130
``
`-
Always import it from the test package
`
1131
``
`-
abstest = 'test.' + test
`
``
1154
`+
abstest = get_abs_module(testdir, test)
`
1132
1155
`clear_caches()
`
1133
1156
`with saved_test_environment(test, verbose, quiet, pgo) as environment:
`
1134
1157
`start_time = time.time()
`
`@@ -1452,7 +1475,7 @@ def count(n, word):
`
1452
1475
`else:
`
1453
1476
`return "%d %ss" % (n, word)
`
1454
1477
``
1455
``
`-
def printlist(x, width=70, indent=4):
`
``
1478
`+
def printlist(x, width=70, indent=4, file=None):
`
1456
1479
`"""Print the elements of iterable x to stdout.
`
1457
1480
``
1458
1481
` Optional arg width (default 70) is the maximum line length.
`
`@@ -1463,8 +1486,37 @@ def printlist(x, width=70, indent=4):
`
1463
1486
`from textwrap import fill
`
1464
1487
`blanks = ' ' * indent
`
1465
1488
`# Print the sorted list: 'x' may be a '--random' list or a set()
`
1466
``
`-
print fill(' '.join(str(elt) for elt in sorted(x)), width,
`
1467
``
`-
initial_indent=blanks, subsequent_indent=blanks)
`
``
1489
`+
print >>file, fill(' '.join(str(elt) for elt in sorted(x)), width,
`
``
1490
`+
initial_indent=blanks, subsequent_indent=blanks)
`
``
1491
+
``
1492
`+
def get_abs_module(testdir, test):
`
``
1493
`+
if test.startswith('test.') or testdir:
`
``
1494
`+
return test
`
``
1495
`+
else:
`
``
1496
`+
Always import it from the test package
`
``
1497
`+
return 'test.' + test
`
``
1498
+
``
1499
`+
def _list_cases(suite):
`
``
1500
`+
for test in suite:
`
``
1501
`+
if isinstance(test, unittest.TestSuite):
`
``
1502
`+
_list_cases(test)
`
``
1503
`+
elif isinstance(test, unittest.TestCase):
`
``
1504
`+
print(test.id())
`
``
1505
+
``
1506
`+
def list_cases(testdir, selected):
`
``
1507
`+
skipped = []
`
``
1508
`+
for test in selected:
`
``
1509
`+
abstest = get_abs_module(testdir, test)
`
``
1510
`+
try:
`
``
1511
`+
suite = unittest.defaultTestLoader.loadTestsFromName(abstest)
`
``
1512
`+
_list_cases(suite)
`
``
1513
`+
except unittest.SkipTest:
`
``
1514
`+
skipped.append(test)
`
``
1515
+
``
1516
`+
if skipped:
`
``
1517
`+
print >>sys.stderr
`
``
1518
`+
print >>sys.stderr, count(len(skipped), "test"), "skipped:"
`
``
1519
`+
printlist(skipped, file=sys.stderr)
`
1468
1520
``
1469
1521
`# Map sys.platform to a string containing the basenames of tests
`
1470
1522
`# expected to be skipped on that platform.
`