Skip to content

Commit a594008

Browse files
authored
gh-132678: Add --prioritize to regrtest (GH-132679)
This is an option that allows the user to specify, which selected tests should execute first, even if the order is otherwise randomized. This is particularly useful for tests that run the longest.
1 parent d134bd2 commit a594008

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

Lib/test/libregrtest/cmdline.py

+27-5
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,19 @@
4444
doing memory analysis on the Python interpreter, which process tends to
4545
consume too many resources to run the full regression test non-stop.
4646
47-
-S is used to continue running tests after an aborted run. It will
48-
maintain the order a standard run (ie, this assumes -r is not used).
47+
-S is used to resume running tests after an interrupted run. It will
48+
maintain the order a standard run (i.e. it assumes -r is not used).
4949
This is useful after the tests have prematurely stopped for some external
50-
reason and you want to start running from where you left off rather
51-
than starting from the beginning.
50+
reason and you want to resume the run from where you left off rather
51+
than starting from the beginning. Note: this is different from --prioritize.
52+
53+
--prioritize is used to influence the order of selected tests, such that
54+
the tests listed as an argument are executed first. This is especially
55+
useful when combined with -j and -r to pin the longest-running tests
56+
to start at the beginning of a test run. Pass --prioritize=test_a,test_b
57+
to make test_a run first, followed by test_b, and then the other tests.
58+
If test_a wasn't selected for execution by regular means, --prioritize will
59+
not make it execute.
5260
5361
-f reads the names of tests from the file given as f's argument, one
5462
or more test names per line. Whitespace is ignored. Blank lines and
@@ -236,7 +244,7 @@ def _create_parser():
236244
help='wait for user input, e.g., allow a debugger '
237245
'to be attached')
238246
group.add_argument('-S', '--start', metavar='START',
239-
help='the name of the test at which to start.' +
247+
help='resume an interrupted run at the following test.' +
240248
more_details)
241249
group.add_argument('-p', '--python', metavar='PYTHON',
242250
help='Command to run Python test subprocesses with.')
@@ -263,6 +271,10 @@ def _create_parser():
263271
group = parser.add_argument_group('Selecting tests')
264272
group.add_argument('-r', '--randomize', action='store_true',
265273
help='randomize test execution order.' + more_details)
274+
group.add_argument('--prioritize', metavar='TEST1,TEST2,...',
275+
action='append', type=priority_list,
276+
help='select these tests first, even if the order is'
277+
' randomized.' + more_details)
266278
group.add_argument('-f', '--fromfile', metavar='FILE',
267279
help='read names of tests to run from a file.' +
268280
more_details)
@@ -406,6 +418,10 @@ def resources_list(string):
406418
return u
407419

408420

421+
def priority_list(string):
422+
return string.split(",")
423+
424+
409425
def _parse_args(args, **kwargs):
410426
# Defaults
411427
ns = Namespace()
@@ -551,4 +567,10 @@ def _parse_args(args, **kwargs):
551567
print(msg, file=sys.stderr, flush=True)
552568
sys.exit(2)
553569

570+
ns.prioritize = [
571+
test
572+
for test_list in (ns.prioritize or ())
573+
for test in test_list
574+
]
575+
554576
return ns

Lib/test/libregrtest/main.py

+11
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
142142
self.random_seed = random.getrandbits(32)
143143
else:
144144
self.random_seed = ns.random_seed
145+
self.prioritize_tests: tuple[str, ...] = tuple(ns.prioritize)
145146

146147
self.parallel_threads = ns.parallel_threads
147148

@@ -237,6 +238,16 @@ def find_tests(self, tests: TestList | None = None) -> tuple[TestTuple, TestList
237238
if self.randomize:
238239
random.shuffle(selected)
239240

241+
for priority_test in reversed(self.prioritize_tests):
242+
try:
243+
selected.remove(priority_test)
244+
except ValueError:
245+
print(f"warning: --prioritize={priority_test} used"
246+
f" but test not actually selected")
247+
continue
248+
else:
249+
selected.insert(0, priority_test)
250+
240251
return (tuple(selected), tests)
241252

242253
@staticmethod
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add ``--prioritize`` to ``-m test``. This option allows the user to specify
2+
which selected tests should execute first, even if the order is otherwise
3+
randomized. This is particularly useful for tests that run the longest.

0 commit comments

Comments
 (0)