77# By default, don't filter tests
88_test_matchers = ()
99_test_patterns = ()
10- _match_test_func2 = None
10+ _match_labels = ()
1111
1212
13- def match_test1 (test ):
13+ def match_test (test ):
1414 # Function used by support.run_unittest() and regrtest --list-cases
15+ return match_test_id (test ) and match_test_label (test )
16+
17+ def match_test_id (test ):
1518 result = False
1619 for matcher , result in reversed (_test_matchers ):
1720 if matcher (test .id ()):
1821 return result
1922 return not result
2023
21- def match_test (test ):
22- # Function used by support.run_unittest() and regrtest --list-cases
23- return (match_test1 (test ) and
24- (_match_test_func2 is None or _match_test_func2 (test )))
24+ def match_test_label (test ):
25+ result = False
26+ for label , result in reversed (_match_labels ):
27+ if _has_label (test , label ):
28+ return result
29+ return not result
30+
31+ def _has_label (test , label ):
32+ attrname = f'_label_{ label } '
33+ if hasattr (test , attrname ):
34+ return True
35+ testMethod = getattr (test , test ._testMethodName )
36+ while testMethod is not None :
37+ if hasattr (testMethod , attrname ):
38+ return True
39+ testMethod = getattr (testMethod , '__wrapped__' , None )
40+ try :
41+ module = sys .modules [test .__class__ .__module__ ]
42+ if hasattr (module , attrname ):
43+ return True
44+ except KeyError :
45+ pass
46+ return False
47+
2548
2649def _is_full_match_test (pattern ):
2750 # If a pattern contains at least one dot, it's considered
@@ -33,7 +56,7 @@ def _is_full_match_test(pattern):
3356 return ('.' in pattern ) and (not re .search (r'[?*\[\]]' , pattern ))
3457
3558
36- def set_match_tests (patterns ):
59+ def set_match_tests (patterns = None , match_labels = None ):
3760 global _test_matchers , _test_patterns
3861
3962 if not patterns :
@@ -50,51 +73,6 @@ def set_match_tests(patterns):
5073 _test_patterns = patterns
5174
5275
53- def _check_obj_labels (obj , labels ):
54- for label in labels :
55- if hasattr (obj , f'_label_{ label } ' ):
56- return True
57- return False
58-
59- def _check_test_labels (test , labels ):
60- if _check_obj_labels (test , labels ):
61- return True
62- testMethod = getattr (test , test ._testMethodName )
63- while testMethod is not None :
64- if _check_obj_labels (testMethod , labels ):
65- return True
66- testMethod = getattr (testMethod , '__wrapped__' , None )
67- try :
68- module = sys .modules [test .__class__ .__module__ ]
69- if _check_obj_labels (module , labels ):
70- return True
71- except KeyError :
72- pass
73- return False
74-
75- def set_match_tests2 (accept_labels = None , ignore_labels = None ):
76- global _match_test_func2
77-
78- if accept_labels is None :
79- accept_labels = ()
80- if ignore_labels is None :
81- ignore_labels = ()
82- # Create a copy since label lists can be mutable and so modified later
83- accept_labels = tuple (accept_labels )
84- ignore_labels = tuple (ignore_labels )
85-
86- def match_function (test ):
87- accept = True
88- ignore = False
89- if accept_labels :
90- accept = _check_test_labels (test , accept_labels )
91- if ignore_labels :
92- ignore = _check_test_labels (test , ignore_labels )
93- return accept and not ignore
94-
95- _match_test_func2 = match_function
96-
97-
9876def _compile_match_function (patterns ):
9977 patterns = list (patterns )
10078
0 commit comments