Skip to content

Commit 36946c0

Browse files
committed
bpo-30523: regrtest --list-cases --match (#2401)
* regrtest --list-cases now supports --match and --match-file options. Example: ./python -m test --list-cases -m FileTests test_os * --list-cases now also sets support.verbose to False to prevent messages to stdout when loading test modules. * Add support._match_test() private function. (cherry picked from commit ace56d5)
1 parent eef254d commit 36946c0

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

Lib/test/regrtest.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1735,10 +1735,14 @@ def _list_cases(suite):
17351735
if isinstance(test, unittest.TestSuite):
17361736
_list_cases(test)
17371737
elif isinstance(test, unittest.TestCase):
1738-
print(test.id())
1738+
if support._match_test(test):
1739+
print(test.id())
17391740

17401741

17411742
def list_cases(ns, selected):
1743+
support.verbose = False
1744+
support.match_tests = ns.match_tests
1745+
17421746
skipped = []
17431747
for test in selected:
17441748
abstest = get_abs_module(ns, test)

Lib/test/support/__init__.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1866,6 +1866,23 @@ def _run_suite(suite):
18661866
raise TestFailed(err)
18671867

18681868

1869+
def _match_test(test):
1870+
global match_tests
1871+
1872+
if match_tests is None:
1873+
return True
1874+
test_id = test.id()
1875+
1876+
for match_test in match_tests:
1877+
if fnmatch.fnmatchcase(test_id, match_test):
1878+
return True
1879+
1880+
for name in test_id.split("."):
1881+
if fnmatch.fnmatchcase(name, match_test):
1882+
return True
1883+
return False
1884+
1885+
18691886
def run_unittest(*classes):
18701887
"""Run tests from unittest.TestCase-derived classes."""
18711888
valid_types = (unittest.TestSuite, unittest.TestCase)
@@ -1880,20 +1897,7 @@ def run_unittest(*classes):
18801897
suite.addTest(cls)
18811898
else:
18821899
suite.addTest(unittest.makeSuite(cls))
1883-
def case_pred(test):
1884-
if match_tests is None:
1885-
return True
1886-
test_id = test.id()
1887-
1888-
for match_test in match_tests:
1889-
if fnmatch.fnmatchcase(test_id, match_test):
1890-
return True
1891-
1892-
for name in test_id.split("."):
1893-
if fnmatch.fnmatchcase(name, match_test):
1894-
return True
1895-
return False
1896-
_filter_suite(suite, case_pred)
1900+
_filter_suite(suite, _match_test)
18971901
_run_suite(suite)
18981902

18991903
#=======================================================================

Lib/test/test_regrtest.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,11 +794,20 @@ def test_method2(self):
794794
pass
795795
""")
796796
testname = self.create_test(code=code)
797+
798+
# Test --list-cases
797799
all_methods = ['%s.Tests.test_method1' % testname,
798800
'%s.Tests.test_method2' % testname]
799801
output = self.run_tests('--list-cases', testname)
800802
self.assertEqual(output.splitlines(), all_methods)
801803

804+
# Test --list-cases with --match
805+
all_methods = ['%s.Tests.test_method1' % testname]
806+
output = self.run_tests('--list-cases',
807+
'-m', 'test_method1',
808+
testname)
809+
self.assertEqual(output.splitlines(), all_methods)
810+
802811
def test_crashed(self):
803812
# Any code which causes a crash
804813
code = 'import faulthandler; faulthandler._sigsegv()'

0 commit comments

Comments
 (0)