[Python-checkins] cpython: Issue #22806: Add ``python -m test --list-tests`` command to list tests.

victor.stinner python-checkins at python.org
Fri Oct 2 18:47:42 EDT 2015


https://hg.python.org/cpython/rev/1005573e6a74
changeset:   98493:1005573e6a74
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Oct 03 00:21:12 2015 +0200
summary:
  Issue #22806: Add ``python -m test --list-tests`` command to list tests.

files:
  Lib/test/libregrtest/cmdline.py |  11 +--
  Lib/test/libregrtest/main.py    |  55 +++++++++++++-------
  Lib/test/test_regrtest.py       |   7 ++
  Misc/NEWS                       |   2 +
  4 files changed, 49 insertions(+), 26 deletions(-)


diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py
--- a/Lib/test/libregrtest/cmdline.py
+++ b/Lib/test/libregrtest/cmdline.py
@@ -1,5 +1,4 @@
 import argparse
-import faulthandler
 import os
 from test import support
 
@@ -234,6 +233,9 @@
     group.add_argument('-F', '--forever', action='store_true',
                        help='run the specified tests in a loop, until an '
                             'error happens')
+    group.add_argument('--list-tests', action='store_true',
+                       help="only write the name of tests that will be run, "
+                            "don't execute them")
 
     parser.add_argument('args', nargs=argparse.REMAINDER,
                         help=argparse.SUPPRESS)
@@ -301,12 +303,7 @@
     if ns.quiet:
         ns.verbose = 0
     if ns.timeout is not None:
-        if hasattr(faulthandler, 'dump_traceback_later'):
-            if ns.timeout <= 0:
-                ns.timeout = None
-        else:
-            print("Warning: The timeout option requires "
-                  "faulthandler.dump_traceback_later")
+        if ns.timeout <= 0:
             ns.timeout = None
     if ns.use_mp is not None:
         if ns.use_mp <= 0:
diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py
--- a/Lib/test/libregrtest/main.py
+++ b/Lib/test/libregrtest/main.py
@@ -1,3 +1,4 @@
+import faulthandler
 import os
 import platform
 import random
@@ -110,8 +111,13 @@
     def parse_args(self, kwargs):
         ns = _parse_args(sys.argv[1:], **kwargs)
 
+        if ns.timeout and not hasattr(faulthandler, 'dump_traceback_later'):
+            print("Warning: The timeout option requires "
+                  "faulthandler.dump_traceback_later", file=sys.stderr)
+            ns.timeout = None
+
         if ns.threshold is not None and gc is None:
-            print('No GC available, ignore --threshold.')
+            print('No GC available, ignore --threshold.', file=sys.stderr)
             ns.threshold = None
 
         if ns.findleaks:
@@ -122,7 +128,8 @@
                 pass
                 #gc.set_debug(gc.DEBUG_SAVEALL)
             else:
-                print('No GC available, disabling --findleaks')
+                print('No GC available, disabling --findleaks',
+                      file=sys.stderr)
                 ns.findleaks = False
 
         # Strip .py extensions.
@@ -163,20 +170,6 @@
                 nottests.add(arg)
             self.ns.args = []
 
-        # For a partial run, we do not need to clutter the output.
-        if (self.ns.verbose
-            or self.ns.header
-            or not (self.ns.quiet or self.ns.single
-                    or self.tests or self.ns.args)):
-            # Print basic platform information
-            print("==", platform.python_implementation(), *sys.version.split())
-            print("==  ", platform.platform(aliased=True),
-                          "%s-endian" % sys.byteorder)
-            print("==  ", "hash algorithm:", sys.hash_info.algorithm,
-                  "64bit" if sys.maxsize > 2**32 else "32bit")
-            print("==  ", os.getcwd())
-            print("Testing with flags:", sys.flags)
-
         # if testdir is set, then we are not running the python tests suite, so
         # don't add default tests to be executed or skipped (pass empty values)
         if self.ns.testdir:
@@ -199,15 +192,18 @@
                 del self.selected[:self.selected.index(self.ns.start)]
             except ValueError:
                 print("Couldn't find starting test (%s), using all tests"
-                      % self.ns.start)
+                      % self.ns.start, file=sys.stderr)
 
         if self.ns.randomize:
             if self.ns.random_seed is None:
                 self.ns.random_seed = random.randrange(10000000)
             random.seed(self.ns.random_seed)
-            print("Using random seed", self.ns.random_seed)
             random.shuffle(self.selected)
 
+    def list_tests(self):
+        for name in self.selected:
+            print(name)
+
     def rerun_failed_tests(self):
         self.ns.verbose = True
         self.ns.failfast = False
@@ -315,6 +311,23 @@
                     return
 
     def run_tests(self):
+        # For a partial run, we do not need to clutter the output.
+        if (self.ns.verbose
+            or self.ns.header
+            or not (self.ns.quiet or self.ns.single
+                    or self.tests or self.ns.args)):
+            # Print basic platform information
+            print("==", platform.python_implementation(), *sys.version.split())
+            print("==  ", platform.platform(aliased=True),
+                          "%s-endian" % sys.byteorder)
+            print("==  ", "hash algorithm:", sys.hash_info.algorithm,
+                  "64bit" if sys.maxsize > 2**32 else "32bit")
+            print("==  ", os.getcwd())
+            print("Testing with flags:", sys.flags)
+
+        if self.ns.randomize:
+            print("Using random seed", self.ns.random_seed)
+
         if self.ns.forever:
             self.tests = self._test_forever(list(self.selected))
             self.test_count = ''
@@ -359,8 +372,12 @@
         setup_tests(self.ns)
 
         self.find_tests(tests)
+
+        if self.ns.list_tests:
+            self.list_tests()
+            sys.exit(0)
+
         self.run_tests()
-
         self.display_result()
 
         if self.ns.verbose2 and self.bad:
diff --git a/Lib/test/test_regrtest.py b/Lib/test/test_regrtest.py
--- a/Lib/test/test_regrtest.py
+++ b/Lib/test/test_regrtest.py
@@ -672,6 +672,13 @@
             reflog = fp.read()
             self.assertEqual(reflog, line2)
 
+    def test_list_tests(self):
+        # test --list-tests
+        tests = [self.create_test() for i in range(5)]
+        output = self.run_tests('--list-tests', *tests)
+        self.assertEqual(output.rstrip().splitlines(),
+                         tests)
+
 
 if __name__ == '__main__':
     unittest.main()
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -170,6 +170,8 @@
 Tests
 -----
 
+- Issue #22806: Add ``python -m test --list-tests`` command to list tests.
+
 - Issue #18174: ``python -m test --huntrleaks ...`` now also checks for leak of
   file descriptors. Patch written by Richard Oudkerk.
 

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list