]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30523: regrtest --list-cases --match (#2401)
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 26 Jun 2017 12:18:51 +0000 (14:18 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 27 Jun 2017 14:06:37 +0000 (16:06 +0200)
* 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 ace56d583664f855d89d1219ece7c21c2fddcf30)

Lib/test/regrtest.py
Lib/test/support/__init__.py
Lib/test/test_regrtest.py

index 299416cf24c8d45ac0feb4a99f93740fca937191..d0f92d5c497a7eef3d2867e5f5ffa69a63418ccd 100755 (executable)
@@ -1735,10 +1735,14 @@ def _list_cases(suite):
         if isinstance(test, unittest.TestSuite):
             _list_cases(test)
         elif isinstance(test, unittest.TestCase):
-            print(test.id())
+            if support._match_test(test):
+                print(test.id())
 
 
 def list_cases(ns, selected):
+    support.verbose = False
+    support.match_tests = ns.match_tests
+
     skipped = []
     for test in selected:
         abstest = get_abs_module(ns, test)
index 92599b31e208fd48d14004bf1ca49e5d555b488a..85878eb6ef2f20a43ec4248fdc9defc857118fa7 100644 (file)
@@ -1866,6 +1866,23 @@ def _run_suite(suite):
         raise TestFailed(err)
 
 
+def _match_test(test):
+    global match_tests
+
+    if match_tests is None:
+        return True
+    test_id = test.id()
+
+    for match_test in match_tests:
+        if fnmatch.fnmatchcase(test_id, match_test):
+            return True
+
+        for name in test_id.split("."):
+            if fnmatch.fnmatchcase(name, match_test):
+                return True
+    return False
+
+
 def run_unittest(*classes):
     """Run tests from unittest.TestCase-derived classes."""
     valid_types = (unittest.TestSuite, unittest.TestCase)
@@ -1880,20 +1897,7 @@ def run_unittest(*classes):
             suite.addTest(cls)
         else:
             suite.addTest(unittest.makeSuite(cls))
-    def case_pred(test):
-        if match_tests is None:
-            return True
-        test_id = test.id()
-
-        for match_test in match_tests:
-            if fnmatch.fnmatchcase(test_id, match_test):
-                return True
-
-            for name in test_id.split("."):
-                if fnmatch.fnmatchcase(name, match_test):
-                    return True
-        return False
-    _filter_suite(suite, case_pred)
+    _filter_suite(suite, _match_test)
     _run_suite(suite)
 
 #=======================================================================
index 3568004b176b17eb4bcf0535a6900e3a481ecdd3..bd74de0819fb3cd5d9caeca44b8ed6fe7fbe3024 100644 (file)
@@ -794,11 +794,20 @@ class ArgsTestCase(BaseTestCase):
                     pass
         """)
         testname = self.create_test(code=code)
+
+        # Test --list-cases
         all_methods = ['%s.Tests.test_method1' % testname,
                        '%s.Tests.test_method2' % testname]
         output = self.run_tests('--list-cases', testname)
         self.assertEqual(output.splitlines(), all_methods)
 
+        # Test --list-cases with --match
+        all_methods = ['%s.Tests.test_method1' % testname]
+        output = self.run_tests('--list-cases',
+                                '-m', 'test_method1',
+                                testname)
+        self.assertEqual(output.splitlines(), all_methods)
+
     def test_crashed(self):
         # Any code which causes a crash
         code = 'import faulthandler; faulthandler._sigsegv()'