From 99c556909e517f0ab8d8ab5c20f29c912d459cd3 Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Fri, 16 Jun 2017 10:50:05 +0800 Subject: [PATCH 1/3] bpo-30523: regrtest: Add --list-cases option --- Lib/test/libregrtest/cmdline.py | 3 +++ Lib/test/libregrtest/main.py | 25 +++++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/Lib/test/libregrtest/cmdline.py b/Lib/test/libregrtest/cmdline.py index 7f669975259fed..bf64062ef964b8 100644 --- a/Lib/test/libregrtest/cmdline.py +++ b/Lib/test/libregrtest/cmdline.py @@ -250,6 +250,9 @@ def _create_parser(): group.add_argument('--list-tests', action='store_true', help="only write the name of tests that will be run, " "don't execute them") + group.add_argument('--list-cases', action='store_true', + help='only write the name of test cases that will be run' + ' , don\'t execute them') group.add_argument('-P', '--pgo', dest='pgo', action='store_true', help='enable Profile Guided Optimization training') diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 7b11fe951b2325..56fc44bd2ef269 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -1,4 +1,5 @@ import datetime +import doctest import faulthandler import locale import os @@ -10,6 +11,7 @@ import tempfile import textwrap import time +import unittest from test.libregrtest.cmdline import _parse_args from test.libregrtest.runtest import ( findtests, runtest, @@ -248,6 +250,25 @@ def list_tests(self): for name in self.selected: print(name) + def _list_cases(self, suite): + for test in suite: + if isinstance(test, unittest.loader._FailedTest): + continue + elif isinstance(test, doctest.DocTestCase): + continue + if isinstance(test, unittest.TestSuite): + self._list_cases(test) + elif isinstance(test, unittest.TestCase): + print(test.id()) + + def list_cases(self): + for name in self.selected: + try: + suite = unittest.defaultTestLoader.loadTestsFromName(name) + self._list_cases(suite) + except unittest.SkipTest: + pass + def rerun_failed_tests(self): self.ns.verbose = True self.ns.failfast = False @@ -499,6 +520,10 @@ def _main(self, tests, kwargs): self.list_tests() sys.exit(0) + if self.ns.list_cases: + self.list_cases() + sys.exit(0) + self.run_tests() self.display_result() From 7b99e2af8de3813c6ab5d83a8f91cad297fc3f23 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 16 Jun 2017 11:05:11 +0200 Subject: [PATCH 2/3] bpo-30523: Enhance --list-cases * Add get_abs_module() function, use it in list_cases() * list_cases() now logs skipped tests into stderr --- Lib/test/libregrtest/main.py | 19 +++++++++++++------ Lib/test/libregrtest/runtest.py | 14 +++++++++----- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index 56fc44bd2ef269..a1c2fd943093e4 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -14,7 +14,7 @@ import unittest from test.libregrtest.cmdline import _parse_args from test.libregrtest.runtest import ( - findtests, runtest, + findtests, runtest, get_abs_module, STDTESTS, NOTTESTS, PASSED, FAILED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED, INTERRUPTED, CHILD_ERROR, PROGRESS_MIN_TIME, format_test_result) @@ -262,12 +262,18 @@ def _list_cases(self, suite): print(test.id()) def list_cases(self): - for name in self.selected: + for test in self.selected: + abstest = get_abs_module(self.ns, test) try: - suite = unittest.defaultTestLoader.loadTestsFromName(name) + suite = unittest.defaultTestLoader.loadTestsFromName(abstest) self._list_cases(suite) except unittest.SkipTest: - pass + self.skipped.append(test) + + if self.skipped: + print(file=sys.stderr) + print(count(len(self.skipped), "test"), "skipped:", file=sys.stderr) + printlist(self.skipped, file=sys.stderr) def rerun_failed_tests(self): self.ns.verbose = True @@ -550,7 +556,7 @@ def count(n, word): return "%d %ss" % (n, word) -def printlist(x, width=70, indent=4): +def printlist(x, width=70, indent=4, file=None): """Print the elements of iterable x to stdout. Optional arg width (default 70) is the maximum line length. @@ -561,7 +567,8 @@ def printlist(x, width=70, indent=4): blanks = ' ' * indent # Print the sorted list: 'x' may be a '--random' list or a set() print(textwrap.fill(' '.join(str(elt) for elt in sorted(x)), width, - initial_indent=blanks, subsequent_indent=blanks)) + initial_indent=blanks, subsequent_indent=blanks), + file=file) def main(tests=None, **kwargs): diff --git a/Lib/test/libregrtest/runtest.py b/Lib/test/libregrtest/runtest.py index ba0df0a317a500..fda4ca1ebca094 100644 --- a/Lib/test/libregrtest/runtest.py +++ b/Lib/test/libregrtest/runtest.py @@ -71,6 +71,14 @@ def findtests(testdir=None, stdtests=STDTESTS, nottests=NOTTESTS): return stdtests + sorted(tests) +def get_abs_module(ns, test): + if test.startswith('test.') or ns.testdir: + return test + else: + # Always import it from the test package + return 'test.' + test + + def runtest(ns, test): """Run a single test. @@ -141,11 +149,7 @@ def runtest_inner(ns, test, display_failure=True): test_time = 0.0 refleak = False # True if the test leaked references. try: - if test.startswith('test.') or ns.testdir: - abstest = test - else: - # Always import it from the test package - abstest = 'test.' + test + abstest = get_abs_module(ns, test) clear_caches() with saved_test_environment(test, ns.verbose, ns.quiet, pgo=ns.pgo) as environment: start_time = time.time() From 1c280a339ebb5a6a0d4875033637325ec1956dcc Mon Sep 17 00:00:00 2001 From: Louie Lu Date: Fri, 16 Jun 2017 17:25:14 +0800 Subject: [PATCH 3/3] Remove unused doctest --- Lib/test/libregrtest/main.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/Lib/test/libregrtest/main.py b/Lib/test/libregrtest/main.py index a1c2fd943093e4..527de177792ce5 100644 --- a/Lib/test/libregrtest/main.py +++ b/Lib/test/libregrtest/main.py @@ -1,5 +1,4 @@ import datetime -import doctest import faulthandler import locale import os @@ -254,8 +253,6 @@ def _list_cases(self, suite): for test in suite: if isinstance(test, unittest.loader._FailedTest): continue - elif isinstance(test, doctest.DocTestCase): - continue if isinstance(test, unittest.TestSuite): self._list_cases(test) elif isinstance(test, unittest.TestCase):