Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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)

(cherry picked from commit 36946c0)
  • Loading branch information
vstinner committed Jun 27, 2017
commit 9e5aee764bcb67c6fd3722de908151aed616666b
10 changes: 7 additions & 3 deletions Lib/test/regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
sys.exit(0)

if list_cases_opt:
list_cases(testdir, selected)
list_cases(testdir, selected, match_tests)
sys.exit(0)

if trace:
Expand Down Expand Up @@ -1501,9 +1501,13 @@ def _list_cases(suite):
if isinstance(test, unittest.TestSuite):
_list_cases(test)
elif isinstance(test, unittest.TestCase):
print(test.id())
if test_support._match_test(test):
print(test.id())

def list_cases(testdir, selected, match_tests):
test_support.verbose = False
test_support.match_tests = match_tests

def list_cases(testdir, selected):
skipped = []
for test in selected:
abstest = get_abs_module(testdir, test)
Expand Down
32 changes: 18 additions & 14 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,6 +1542,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)
Expand All @@ -1556,20 +1573,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)

#=======================================================================
Expand Down
47 changes: 26 additions & 21 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,32 @@ def test_list_tests(self):
self.assertEqual(output.rstrip().splitlines(),
tests)

def test_list_cases(self):
# test --list-cases
code = textwrap.dedent("""
import unittest

class Tests(unittest.TestCase):
def test_method1(self):
pass
def test_method2(self):
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 test.support; test.support._crash_python()'
Expand Down Expand Up @@ -532,27 +558,6 @@ def test_main():
subset = ['test_method1', 'test_method3']
self.assertEqual(methods, subset)

def test_list_cases(self):
# test --list-cases
code = textwrap.dedent("""
import unittest
from test import support

class Tests(unittest.TestCase):
def test_method1(self):
pass
def test_method2(self):
pass

def test_main():
support.run_unittest(Tests)
""")
testname = self.create_test(code=code)
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)


def test_main():
support.run_unittest(ProgramsTestCase, ArgsTestCase)
Expand Down