◐ Shell
reader mode source ↗
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
40 changes: 34 additions & 6 deletions Lib/test/regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
don't execute them
--list-cases -- only write the name of test cases that will be run,
don't execute them


Additional Option Details:
Expand Down Expand Up @@ -327,7 +329,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
'runleaks', 'huntrleaks=', 'memlimit=', 'randseed=',
'multiprocess=', 'slaveargs=', 'forever', 'header', 'pgo',
'failfast', 'match=', 'testdir=', 'list-tests', 'list-cases',
'coverage', 'matchfile='])
except getopt.error, msg:
usage(2, msg)

Expand All @@ -339,6 +341,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
slaveargs = None
list_tests = False
list_cases_opt = False
for o, a in opts:
if o in ('-h', '--help'):
usage(0)
Expand Down Expand Up @@ -439,6 +442,8 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
list_tests = True
elif o == '--list-cases':
list_cases_opt = True
else:
print >>sys.stderr, ("No handler for option {}. Please "
"report this as a bug at http://bugs.python.org.").format(o)
Expand Down Expand Up @@ -558,7 +563,7 @@ def main(tests=None, testdir=None, verbose=0, quiet=False,
sys.exit(0)

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

if trace:
Expand Down Expand Up @@ -908,11 +913,19 @@ def local_runtest():
result = "FAILURE"
elif interrupted:
result = "INTERRUPTED"
else:
result = "SUCCESS"
print("Tests result: %s" % result)

sys.exit(len(bad) > 0 or interrupted)


STDTESTS = [
Expand Down @@ -1310,7 +1323,18 @@ def run_the_test():
if i >= nwarmup:
deltas.append(rc_after - rc_before)
print >> sys.stderr
if any(deltas):
msg = '%s leaked %s references, sum=%s' % (test, deltas, sum(deltas))
print >> sys.stderr, msg
with open(fname, "a") as refrep:
Expand Down Expand Up @@ -1501,9 +1525,13 @@ def _list_cases(suite):
if isinstance(test, unittest.TestSuite):
_list_cases(test)
elif isinstance(test, unittest.TestCase):
print(test.id())

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 run_unittest(*classes):
"""Run tests from unittest.TestCase-derived classes."""
valid_types = (unittest.TestSuite, unittest.TestCase)
@@ -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)
_run_suite(suite)

#=======================================================================
Expand Down
118 changes: 96 additions & 22 deletions Lib/test/test_regrtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,19 @@ def parse_executed_tests(self, output):
return list(match.group(1) for match in parser)

def check_executed_tests(self, output, tests, skipped=(), failed=(),
omitted=(), randomize=False, interrupted=False):
if isinstance(tests, str):
tests = [tests]
if isinstance(skipped, str):
skipped = [skipped]
if isinstance(failed, str):
failed = [failed]
if isinstance(omitted, str):
omitted = [omitted]
ntest = len(tests)
nskipped = len(skipped)
nfailed = len(failed)
nomitted = len(omitted)

executed = self.parse_executed_tests(output)
if randomize:
Expand All @@ -129,11 +129,17 @@ def list_regex(line_format, tests):
regex = list_regex('%s test%s failed', failed)
self.check_line(output, regex)

if omitted:
regex = list_regex('%s test%s omitted', omitted)
self.check_line(output, regex)

good = ntest - nskipped - nfailed - nomitted
if good:
regex = r'%s test%s OK\.$' % (good, plural(good))
if not skipped and not failed and good > 1:
Expand All @@ -143,10 +149,12 @@ def list_regex(line_format, tests):
if interrupted:
self.check_line(output, 'Test suite interrupted by signal SIGINT.')

if nfailed:
result = 'FAILURE'
elif interrupted:
result = 'INTERRUPTED'
else:
result = 'SUCCESS'
self.check_line(output, 'Tests result: %s' % result)
Expand Down Expand Up @@ -325,7 +333,7 @@ def test_main():
test_failing = self.create_test('failing', code=code)
tests = [test_ok, test_failing]

output = self.run_tests(*tests, exitcode=1)
self.check_executed_tests(output, tests, failed=test_failing)

def test_resources(self):
Expand Down Expand Up @@ -394,7 +402,7 @@ def test_fromfile(self):
def test_interrupted(self):
code = TEST_INTERRUPTED
test = self.create_test('sigint', code=code)
output = self.run_tests(test, exitcode=1)
self.check_executed_tests(output, test, omitted=test,
interrupted=True)

Expand Down Expand Up @@ -423,7 +431,7 @@ def test_slow_interrupted(self):
args = ("--slowest", "-j2", test)
else:
args = ("--slowest", test)
output = self.run_tests(*args, exitcode=1)
self.check_executed_tests(output, test,
omitted=test, interrupted=True)

Expand Down Expand Up @@ -461,24 +469,88 @@ def test_main():
support.run_unittest(ForeverTester)
""")
test = self.create_test('forever', code=code)
output = self.run_tests('--forever', test, exitcode=1)
self.check_executed_tests(output, [test]*3, failed=test)

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)

def test_crashed(self):
# Any code which causes a crash
code = 'import test.support; test.support._crash_python()'
crash_test = self.create_test(name="crash", code=code)
ok_test = self.create_test(name="ok")

tests = [crash_test, ok_test]
output = self.run_tests("-j2", *tests, exitcode=1)
self.check_executed_tests(output, tests, failed=crash_test,
randomize=True)

Expand Down Expand Up @@ -532,26 +604,28 @@ 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():
Toggle all file notes Toggle all file annotations