117
117
To enable all resources except one, use '-uall,-<resource>'. For
118
118
example, to run all the tests except for the gui tests, give the
119
119
option '-uall,-gui'.
120
+
121
+ --matchfile filters tests using a text file, one pattern per line.
122
+ Pattern examples:
123
+
124
+ - test method: test_stat_attributes
125
+ - test class: FileTests
126
+ - test identifier: test_os.FileTests.test_stat_attributes
120
127
"""
121
128
122
129
# We import importlib *ASAP* in order to test #15386
@@ -276,8 +283,12 @@ def _create_parser():
276
283
help = 'single step through a set of tests.' +
277
284
more_details )
278
285
group .add_argument ('-m' , '--match' , metavar = 'PAT' ,
279
- dest = 'match_tests' ,
286
+ dest = 'match_tests' , action = 'append' ,
280
287
help = 'match test cases and methods with glob pattern PAT' )
288
+ group .add_argument ('--matchfile' , metavar = 'FILENAME' ,
289
+ dest = 'match_filename' ,
290
+ help = 'similar to --match but get patterns from a '
291
+ 'text file, one pattern per line' )
281
292
group .add_argument ('-G' , '--failfast' , action = 'store_true' ,
282
293
help = 'fail as soon as a test fails (only with -v or -W)' )
283
294
group .add_argument ('-u' , '--use' , metavar = 'RES1,RES2,...' ,
@@ -323,6 +334,9 @@ def _create_parser():
323
334
group .add_argument ('-F' , '--forever' , action = 'store_true' ,
324
335
help = 'run the specified tests in a loop, until an '
325
336
'error happens' )
337
+ group .add_argument ('--list-cases' , action = 'store_true' ,
338
+ help = 'only write the name of test cases that will be run'
339
+ ' , don\' t execute them' )
326
340
group .add_argument ('-P' , '--pgo' , dest = 'pgo' , action = 'store_true' ,
327
341
help = 'enable Profile Guided Optimization training' )
328
342
@@ -361,7 +375,8 @@ def _parse_args(args, **kwargs):
361
375
findleaks = False , use_resources = None , trace = False , coverdir = 'coverage' ,
362
376
runleaks = False , huntrleaks = False , verbose2 = False , print_slow = False ,
363
377
random_seed = None , use_mp = None , verbose3 = False , forever = False ,
364
- header = False , failfast = False , match_tests = None , pgo = False )
378
+ header = False , failfast = False , match_tests = None , match_filename = None ,
379
+ pgo = False )
365
380
for k , v in kwargs .items ():
366
381
if not hasattr (ns , k ):
367
382
raise TypeError ('%r is an invalid keyword argument '
@@ -431,6 +446,13 @@ def _parse_args(args, **kwargs):
431
446
print ("WARNING: Disable --verbose3 because it's incompatible with "
432
447
"--huntrleaks: see http://bugs.python.org/issue27103" ,
433
448
file = sys .stderr )
449
+ if ns .match_filename :
450
+ if ns .match_tests is None :
451
+ ns .match_tests = []
452
+ filename = os .path .join (support .SAVEDCWD , ns .match_filename )
453
+ with open (filename ) as fp :
454
+ for line in fp :
455
+ ns .match_tests .append (line .strip ())
434
456
435
457
return ns
436
458
@@ -555,7 +577,7 @@ def main(tests=None, **kwargs):
555
577
unittest .BaseTestSuite ._cleanup = False
556
578
557
579
try :
558
- result = runtest (testname , ns .verbose , ns .quiet ,
580
+ result = runtest (ns , testname , ns .verbose , ns .quiet ,
559
581
ns .huntrleaks ,
560
582
output_on_failure = ns .verbose3 ,
561
583
timeout = ns .timeout , failfast = ns .failfast ,
@@ -632,18 +654,6 @@ def main(tests=None, **kwargs):
632
654
nottests .add (arg )
633
655
ns .args = []
634
656
635
- # For a partial run, we do not need to clutter the output.
636
- if (ns .verbose or ns .header or
637
- not (ns .pgo or ns .quiet or ns .single or tests or ns .args )):
638
- # Print basic platform information
639
- print ("==" , platform .python_implementation (), * sys .version .split ())
640
- print ("== " , platform .platform (aliased = True ),
641
- "%s-endian" % sys .byteorder )
642
- print ("== " , "hash algorithm:" , sys .hash_info .algorithm ,
643
- "64bit" if sys .maxsize > 2 ** 32 else "32bit" )
644
- print ("== " , os .getcwd ())
645
- print ("Testing with flags:" , sys .flags )
646
-
647
657
# if testdir is set, then we are not running the python tests suite, so
648
658
# don't add default tests to be executed or skipped (pass empty values)
649
659
if ns .testdir :
@@ -697,6 +707,10 @@ def accumulate_result(test, result):
697
707
skipped .append (test )
698
708
resource_denieds .append (test )
699
709
710
+ if ns .list_cases :
711
+ list_cases (ns , selected )
712
+ sys .exit (0 )
713
+
700
714
if ns .forever :
701
715
def test_forever (tests = list (selected )):
702
716
while True :
@@ -712,6 +726,18 @@ def test_forever(tests=list(selected)):
712
726
test_count = '/{}' .format (len (selected ))
713
727
test_count_width = len (test_count ) - 1
714
728
729
+ # For a partial run, we do not need to clutter the output.
730
+ if (ns .verbose or ns .header or
731
+ not (ns .pgo or ns .quiet or ns .single or tests or ns .args )):
732
+ # Print basic platform information
733
+ print ("==" , platform .python_implementation (), * sys .version .split ())
734
+ print ("== " , platform .platform (aliased = True ),
735
+ "%s-endian" % sys .byteorder )
736
+ print ("== " , "hash algorithm:" , sys .hash_info .algorithm ,
737
+ "64bit" if sys .maxsize > 2 ** 32 else "32bit" )
738
+ print ("== " , os .getcwd ())
739
+ print ("Testing with flags:" , sys .flags )
740
+
715
741
if ns .use_mp :
716
742
try :
717
743
from threading import Thread
@@ -797,11 +823,11 @@ def work():
797
823
if ns .trace :
798
824
# If we're tracing code coverage, then we don't exit with status
799
825
# if on a false return value from main.
800
- tracer .runctx ('runtest(test, ns.verbose, ns.quiet, timeout=ns.timeout)' ,
826
+ tracer .runctx ('runtest(ns, test, ns.verbose, ns.quiet, timeout=ns.timeout)' ,
801
827
globals = globals (), locals = vars ())
802
828
else :
803
829
try :
804
- result = runtest (test , ns .verbose , ns .quiet ,
830
+ result = runtest (ns , test , ns .verbose , ns .quiet ,
805
831
ns .huntrleaks ,
806
832
output_on_failure = ns .verbose3 ,
807
833
timeout = ns .timeout , failfast = ns .failfast ,
@@ -859,7 +885,7 @@ def work():
859
885
sys .stdout .flush ()
860
886
try :
861
887
ns .verbose = True
862
- ok = runtest (test , True , ns .quiet , ns .huntrleaks ,
888
+ ok = runtest (ns , test , True , ns .quiet , ns .huntrleaks ,
863
889
timeout = ns .timeout , pgo = ns .pgo )
864
890
except KeyboardInterrupt :
865
891
# print a newline separate from the ^C
@@ -956,7 +982,7 @@ def restore_stdout():
956
982
sys .stdout = stdout
957
983
atexit .register (restore_stdout )
958
984
959
- def runtest (test , verbose , quiet ,
985
+ def runtest (ns , test , verbose , quiet ,
960
986
huntrleaks = False , use_resources = None ,
961
987
output_on_failure = False , failfast = False , match_tests = None ,
962
988
timeout = None , * , pgo = False ):
@@ -1011,8 +1037,9 @@ def runtest(test, verbose, quiet,
1011
1037
try :
1012
1038
sys .stdout = stream
1013
1039
sys .stderr = stream
1014
- result = runtest_inner (test , verbose , quiet , huntrleaks ,
1015
- display_failure = False , pgo = pgo )
1040
+ result = runtest_inner (ns , test , verbose , quiet , huntrleaks ,
1041
+ display_failure = False ,
1042
+ pgo = pgo )
1016
1043
if result [0 ] != PASSED and not pgo :
1017
1044
output = stream .getvalue ()
1018
1045
orig_stderr .write (output )
@@ -1022,8 +1049,9 @@ def runtest(test, verbose, quiet,
1022
1049
sys .stderr = orig_stderr
1023
1050
else :
1024
1051
support .verbose = verbose # Tell tests to be moderately quiet
1025
- result = runtest_inner (test , verbose , quiet , huntrleaks ,
1026
- display_failure = not verbose , pgo = pgo )
1052
+ result = runtest_inner (ns , test , verbose , quiet , huntrleaks ,
1053
+ display_failure = not verbose ,
1054
+ pgo = pgo )
1027
1055
return result
1028
1056
finally :
1029
1057
if use_timeout :
@@ -1294,18 +1322,14 @@ def __exit__(self, exc_type, exc_val, exc_tb):
1294
1322
return False
1295
1323
1296
1324
1297
- def runtest_inner (test , verbose , quiet ,
1325
+ def runtest_inner (ns , test , verbose , quiet ,
1298
1326
huntrleaks = False , display_failure = True , pgo = False ):
1299
1327
support .unload (test )
1300
1328
1301
1329
test_time = 0.0
1302
1330
refleak = False # True if the test leaked references.
1303
1331
try :
1304
- if test .startswith ('test.' ):
1305
- abstest = test
1306
- else :
1307
- # Always import it from the test package
1308
- abstest = 'test.' + test
1332
+ abstest = get_abs_module (ns , test )
1309
1333
clear_caches ()
1310
1334
with saved_test_environment (test , verbose , quiet , pgo = pgo ) as environment :
1311
1335
start_time = time .time ()
@@ -1646,7 +1670,7 @@ def count(n, word):
1646
1670
else :
1647
1671
return "%d %ss" % (n , word )
1648
1672
1649
- def printlist (x , width = 70 , indent = 4 ):
1673
+ def printlist (x , width = 70 , indent = 4 , file = None ):
1650
1674
"""Print the elements of iterable x to stdout.
1651
1675
1652
1676
Optional arg width (default 70) is the maximum line length.
@@ -1658,7 +1682,41 @@ def printlist(x, width=70, indent=4):
1658
1682
blanks = ' ' * indent
1659
1683
# Print the sorted list: 'x' may be a '--random' list or a set()
1660
1684
print (fill (' ' .join (str (elt ) for elt in sorted (x )), width ,
1661
- initial_indent = blanks , subsequent_indent = blanks ))
1685
+ initial_indent = blanks , subsequent_indent = blanks ), file = file )
1686
+
1687
+
1688
+ def get_abs_module (ns , test ):
1689
+ if test .startswith ('test.' ) or ns .testdir :
1690
+ return test
1691
+ else :
1692
+ # Always import it from the test package
1693
+ return 'test.' + test
1694
+
1695
+
1696
+ def _list_cases (suite ):
1697
+ for test in suite :
1698
+ if isinstance (test , unittest .loader ._FailedTest ):
1699
+ continue
1700
+ if isinstance (test , unittest .TestSuite ):
1701
+ _list_cases (test )
1702
+ elif isinstance (test , unittest .TestCase ):
1703
+ print (test .id ())
1704
+
1705
+
1706
+ def list_cases (ns , selected ):
1707
+ skipped = []
1708
+ for test in selected :
1709
+ abstest = get_abs_module (ns , test )
1710
+ try :
1711
+ suite = unittest .defaultTestLoader .loadTestsFromName (abstest )
1712
+ _list_cases (suite )
1713
+ except unittest .SkipTest :
1714
+ skipped .append (test )
1715
+
1716
+ if skipped :
1717
+ print (file = sys .stderr )
1718
+ print (count (len (skipped ), "test" ), "skipped:" , file = sys .stderr )
1719
+ printlist (skipped , file = sys .stderr )
1662
1720
1663
1721
1664
1722
def main_in_temp_cwd ():
0 commit comments