Skip to content

Commit bc63806

Browse files
gh-108828: Support selecting tests by labels (GH-108829)
Add the --label and --no-label regrtest options to run or skip tests that have a label. "--label name" matches any value of the label, "--label name=value" matches only that value. Add test.support.mark() to label a test method or class, and test.support.mark_module() to label every test of a module. Many test.support helpers, such as requires_resource(), requires() called at the module level, cpython_only, bigmemtest() and import_module() called at the module level, add labels automatically. Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 4743be9 commit bc63806

12 files changed

Lines changed: 203 additions & 25 deletions

File tree

‎Doc/library/test.rst‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,8 @@ The :mod:`!test.support` module defines the following functions:
517517
``True`` if called by a function whose ``__name__`` is ``'__main__'``.
518518
Used when tests are executed by :mod:`test.regrtest`.
519519

520+
If called at the top level, sets label "requires\_\ *resource*" on the module.
521+
520522

521523
.. function:: sortdict(dict)
522524

@@ -533,6 +535,29 @@ The :mod:`!test.support` module defines the following functions:
533535
rather than looking directly in the path directories.
534536

535537

538+
.. function:: mark(label, value=True, /)
539+
540+
Add a label to a test. Use ``@mark('label')`` as a decorator of a test
541+
method or class.
542+
543+
The optional *value* (``True`` by default) is matched on the command line
544+
by ``--label label=value``, whereas ``--label label`` matches any value.
545+
546+
Many :mod:`test.support` decorators like :func:`requires_resource`,
547+
:func:`~test.support.cpython_only` or :func:`bigmemtest` add labels
548+
automatically.
549+
550+
551+
.. function:: mark_module(label, value=True, /, *, globals=None)
552+
553+
Add a label to every test of a module. Call it at the module level::
554+
555+
test.support.mark_module('pickletest')
556+
557+
The module is the caller, unless its :func:`globals` dict is passed as the
558+
*globals* argument.
559+
560+
536561
.. function:: get_pagesize()
537562

538563
Get size of a page in bytes.
@@ -777,26 +802,31 @@ The :mod:`!test.support` module defines the following functions:
777802
.. decorator:: requires_zlib
778803

779804
Decorator for skipping tests if :mod:`zlib` doesn't exist.
805+
Adds label "requires_zlib".
780806

781807

782808
.. decorator:: requires_gzip
783809

784810
Decorator for skipping tests if :mod:`gzip` doesn't exist.
811+
Adds label "requires_gzip".
785812

786813

787814
.. decorator:: requires_bz2
788815

789816
Decorator for skipping tests if :mod:`bz2` doesn't exist.
817+
Adds label "requires_bz2".
790818

791819

792820
.. decorator:: requires_lzma
793821

794822
Decorator for skipping tests if :mod:`lzma` doesn't exist.
823+
Adds label "requires_lzma".
795824

796825

797826
.. decorator:: requires_resource(resource)
798827

799828
Decorator for skipping tests if *resource* is not available.
829+
Adds label "requires\_\ *resource*".
800830

801831

802832
.. decorator:: requires_docstrings
@@ -813,12 +843,17 @@ The :mod:`!test.support` module defines the following functions:
813843
.. decorator:: cpython_only
814844

815845
Decorator for tests only applicable to CPython.
846+
Adds label "impl_detail_cpython".
816847

817848

818849
.. decorator:: impl_detail(msg=None, **guards)
819850

820851
Decorator for invoking :func:`check_impl_detail` on *guards*. If that
821852
returns ``False``, then uses *msg* as the reason for skipping the test.
853+
For every keyword argument *name* adds a label
854+
"impl_detail\_\ *name*" if its value is true or
855+
"impl_detail_no\_\ *name*" otherwise.
856+
822857

823858
.. decorator:: thread_unsafe(reason=None)
824859

@@ -853,10 +888,13 @@ The :mod:`!test.support` module defines the following functions:
853888
method may be less than the requested value. If *dry_run* is ``False``, it
854889
means the test doesn't support dummy runs when ``-M`` is not specified.
855890

891+
Adds label "bigmemtest".
892+
856893

857894
.. decorator:: bigaddrspacetest
858895

859896
Decorator for tests that fill the address space.
897+
Adds label "bigaddrspacetest".
860898

861899

862900
.. function:: linked_to_musl()
@@ -1800,6 +1838,8 @@ Importing modules
18001838
optional for others, set *required_on* to an iterable of platform prefixes
18011839
which will be compared against :data:`sys.platform`.
18021840

1841+
If called at the top level, sets label "requires\_\ *name*" on the module.
1842+
18031843
.. versionadded:: 3.1
18041844

18051845
.. function:: import_fresh_module(name, fresh=(), blocked=(), *, deprecated=False, usefrozen=False)

‎Lib/test/libregrtest/cmdline.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ def __init__(self, **kwargs) -> None:
180180
self.header = False
181181
self.failfast = False
182182
self.match_tests: TestFilter = []
183+
self.match_labels: TestFilter = []
183184
self.pgo = False
184185
self.pgo_extended = False
185186
self.tsan = False
@@ -300,6 +301,14 @@ def _create_parser():
300301
group.add_argument('-i', '--ignore', metavar='PAT',
301302
dest='match_tests', action=FilterAction, const=False,
302303
help='ignore test cases and methods with glob pattern `PAT`')
304+
group.add_argument('--label', metavar='NAME[=VALUE]',
305+
dest='match_labels', action=FilterAction, const=True,
306+
help='match test cases and methods with label `NAME` '
307+
'(optionally only if its value is `VALUE`)')
308+
group.add_argument('--no-label', metavar='NAME[=VALUE]',
309+
dest='match_labels', action=FilterAction, const=False,
310+
help='ignore test cases and methods with label `NAME` '
311+
'(optionally only if its value is `VALUE`)')
303312
group.add_argument('--matchfile', metavar='FILENAME',
304313
dest='match_tests',
305314
action=FromFileFilterAction, const=True,

‎Lib/test/libregrtest/filter.py‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
import itertools
22
import operator
33
import re
4+
import sys
45

56

67
# By default, don't filter tests
78
_test_matchers = ()
89
_test_patterns = ()
10+
_match_labels = ()
11+
12+
# Sentinel returned by _get_label() when the test has no such label.
13+
_no_label = object()
914

1015

1116
def match_test(test):
1217
# Function used by support.run_unittest() and regrtest --list-cases
18+
return match_test_id(test) and match_test_label(test)
19+
20+
def match_test_id(test):
1321
result = False
1422
for matcher, result in reversed(_test_matchers):
1523
if matcher(test.id()):
1624
return result
1725
return not result
1826

27+
def match_test_label(test):
28+
result = False
29+
for name, value, result in reversed(_match_labels):
30+
actual = _get_label(test, name)
31+
if actual is _no_label:
32+
continue
33+
# value is None for a plain "--label name" (match any value).
34+
if value is None or value == str(actual):
35+
return result
36+
return not result
37+
38+
def _get_label(test, label):
39+
attrname = f'_label_{label}'
40+
value = getattr(test, attrname, _no_label)
41+
if value is not _no_label:
42+
return value
43+
testMethod = getattr(test, test._testMethodName)
44+
while testMethod is not None:
45+
value = getattr(testMethod, attrname, _no_label)
46+
if value is not _no_label:
47+
return value
48+
testMethod = getattr(testMethod, '__wrapped__', None)
49+
try:
50+
module = sys.modules[test.__class__.__module__]
51+
except KeyError:
52+
pass
53+
else:
54+
value = getattr(module, attrname, _no_label)
55+
if value is not _no_label:
56+
return value
57+
return _no_label
58+
1959

2060
def _is_full_match_test(pattern):
2161
# If a pattern contains at least one dot, it's considered
@@ -32,8 +72,8 @@ def get_match_tests():
3272
return _test_patterns
3373

3474

35-
def set_match_tests(patterns):
36-
global _test_matchers, _test_patterns
75+
def set_match_tests(patterns=None, match_labels=None):
76+
global _test_matchers, _test_patterns, _match_labels
3777

3878
if not patterns:
3979
_test_matchers = ()
@@ -48,6 +88,17 @@ def set_match_tests(patterns):
4888
]
4989
_test_patterns = patterns
5090

91+
if not match_labels:
92+
_match_labels = ()
93+
else:
94+
# "name" matches a label with any value, "name=value" matches only
95+
# the specified value.
96+
_match_labels = tuple(
97+
(name, value if sep else None, result)
98+
for label, result in match_labels
99+
for name, sep, value in [label.partition('=')]
100+
)
101+
51102

52103
def _compile_match_function(patterns):
53104
patterns = list(patterns)

‎Lib/test/libregrtest/findtests.py‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ def _list_cases(suite: unittest.TestSuite) -> None:
9292

9393
def list_cases(tests: TestTuple, *,
9494
match_tests: TestFilter | None = None,
95+
match_labels: TestFilter | None = None,
9596
test_dir: StrPath | None = None) -> None:
9697
support.verbose = False
9798
cases_by_module, skipped = collect_cases(tests, match_tests=match_tests,
99+
match_labels=match_labels,
98100
test_dir=test_dir)
99101
for cases in cases_by_module.values():
100102
for case_id in cases:
@@ -112,12 +114,13 @@ class _ModuleLoadFailed(Exception):
112114

113115
def collect_cases(tests: TestTuple, *,
114116
match_tests: TestFilter | None = None,
117+
match_labels: TestFilter | None = None,
115118
test_dir: StrPath | None = None
116119
) -> tuple[dict[TestName, list[str]], list[TestName]]:
117120
# Install the filter unconditionally: passing None clears any
118121
# previously installed global filter, so collection is not
119122
# affected by unrelated state in this process.
120-
set_match_tests(match_tests)
123+
set_match_tests(match_tests, match_labels)
121124
result: dict[TestName, list[str]] = {}
122125
skipped: list[TestName] = []
123126
for test_name in tests:

‎Lib/test/libregrtest/main.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
8888

8989
# Select tests
9090
self.match_tests: TestFilter = ns.match_tests
91+
self.match_labels: TestFilter = ns.match_labels
9192
self.exclude: bool = ns.exclude
9293
self.fromfile: StrPath | None = ns.fromfile
9394
self.starting_test: TestName | None = ns.start
@@ -515,6 +516,7 @@ def create_run_tests(self, tests: TestTuple) -> RunTests:
515516
fail_fast=self.fail_fast,
516517
fail_env_changed=self.fail_env_changed,
517518
match_tests=self.match_tests,
519+
match_labels=self.match_labels,
518520
match_tests_dict=None,
519521
rerun=False,
520522
forever=self.forever,
@@ -820,6 +822,7 @@ def main(self, tests: TestList | None = None) -> NoReturn:
820822
elif self.want_list_cases:
821823
list_cases(selected,
822824
match_tests=self.match_tests,
825+
match_labels=self.match_labels,
823826
test_dir=self.test_dir)
824827
else:
825828
exitcode = self.run_tests(selected, tests)

‎Lib/test/libregrtest/runtests.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ class RunTests:
8181
fail_fast: bool
8282
fail_env_changed: bool
8383
match_tests: TestFilter
84+
match_labels: TestFilter
8485
match_tests_dict: FilterDict | None
8586
rerun: bool
8687
forever: bool

‎Lib/test/libregrtest/setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def setup_tests(runtests: RunTests) -> None:
112112
support.PGO = runtests.pgo
113113
support.PGO_EXTENDED = runtests.pgo_extended
114114

115-
set_match_tests(runtests.match_tests)
115+
set_match_tests(runtests.match_tests, runtests.match_labels)
116116

117117
reporter = get_reporter()
118118
reporter.run_started(runtests)

0 commit comments

Comments
 (0)