Skip to content

Commit d8af327

Browse files
[3.15] gh-108828: Support selecting tests by labels (GH-108829) (GH-162584)
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. (cherry picked from commit bc63806) Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent aa9b2c4 commit d8af327

12 files changed

Lines changed: 201 additions & 25 deletions

File tree

‎Doc/library/test.rst‎

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

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

520522
.. function:: sortdict(dict)
521523

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

534536

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

537562
Get size of a page in bytes.
@@ -776,26 +801,31 @@ The :mod:`!test.support` module defines the following functions:
776801
.. decorator:: requires_zlib
777802

778803
Decorator for skipping tests if :mod:`zlib` doesn't exist.
804+
Adds label "requires_zlib".
779805

780806

781807
.. decorator:: requires_gzip
782808

783809
Decorator for skipping tests if :mod:`gzip` doesn't exist.
810+
Adds label "requires_gzip".
784811

785812

786813
.. decorator:: requires_bz2
787814

788815
Decorator for skipping tests if :mod:`bz2` doesn't exist.
816+
Adds label "requires_bz2".
789817

790818

791819
.. decorator:: requires_lzma
792820

793821
Decorator for skipping tests if :mod:`lzma` doesn't exist.
822+
Adds label "requires_lzma".
794823

795824

796825
.. decorator:: requires_resource(resource)
797826

798827
Decorator for skipping tests if *resource* is not available.
828+
Adds label "requires\_\ *resource*".
799829

800830

801831
.. decorator:: requires_docstrings
@@ -812,12 +842,17 @@ The :mod:`!test.support` module defines the following functions:
812842
.. decorator:: cpython_only
813843

814844
Decorator for tests only applicable to CPython.
845+
Adds label "impl_detail_cpython".
815846

816847

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

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

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

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

890+
Adds label "bigmemtest".
891+
855892

856893
.. decorator:: bigaddrspacetest
857894

858895
Decorator for tests that fill the address space.
896+
Adds label "bigaddrspacetest".
859897

860898

861899
.. function:: linked_to_musl()
@@ -1791,6 +1829,8 @@ Importing modules
17911829
optional for others, set *required_on* to an iterable of platform prefixes
17921830
which will be compared against :data:`sys.platform`.
17931831

1832+
If called at the top level, sets label "requires\_\ *name*" on the module.
1833+
17941834
.. versionadded:: 3.1
17951835

17961836
.. 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
@@ -299,6 +300,14 @@ def _create_parser():
299300
group.add_argument('-i', '--ignore', metavar='PAT',
300301
dest='match_tests', action=FilterAction, const=False,
301302
help='ignore test cases and methods with glob pattern `PAT`')
303+
group.add_argument('--label', metavar='NAME[=VALUE]',
304+
dest='match_labels', action=FilterAction, const=True,
305+
help='match test cases and methods with label `NAME` '
306+
'(optionally only if its value is `VALUE`)')
307+
group.add_argument('--no-label', metavar='NAME[=VALUE]',
308+
dest='match_labels', action=FilterAction, const=False,
309+
help='ignore test cases and methods with label `NAME` '
310+
'(optionally only if its value is `VALUE`)')
302311
group.add_argument('--matchfile', metavar='FILENAME',
303312
dest='match_tests',
304313
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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,10 @@ 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
97-
set_match_tests(match_tests)
98+
set_match_tests(match_tests, match_labels)
9899

99100
skipped = []
100101
for test_name in tests:

‎Lib/test/libregrtest/main.py‎

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

8686
# Select tests
8787
self.match_tests: TestFilter = ns.match_tests
88+
self.match_labels: TestFilter = ns.match_labels
8889
self.exclude: bool = ns.exclude
8990
self.fromfile: StrPath | None = ns.fromfile
9091
self.starting_test: TestName | None = ns.start
@@ -498,6 +499,7 @@ def create_run_tests(self, tests: TestTuple) -> RunTests:
498499
fail_fast=self.fail_fast,
499500
fail_env_changed=self.fail_env_changed,
500501
match_tests=self.match_tests,
502+
match_labels=self.match_labels,
501503
match_tests_dict=None,
502504
rerun=False,
503505
forever=self.forever,
@@ -785,6 +787,7 @@ def main(self, tests: TestList | None = None) -> NoReturn:
785787
elif self.want_list_cases:
786788
list_cases(selected,
787789
match_tests=self.match_tests,
790+
match_labels=self.match_labels,
788791
test_dir=self.test_dir)
789792
else:
790793
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
@@ -110,7 +110,7 @@ def setup_tests(runtests: RunTests) -> None:
110110
support.PGO = runtests.pgo
111111
support.PGO_EXTENDED = runtests.pgo_extended
112112

113-
set_match_tests(runtests.match_tests)
113+
set_match_tests(runtests.match_tests, runtests.match_labels)
114114

115115
if runtests.use_junit:
116116
support.junit_xml_list = []

0 commit comments

Comments
 (0)