Skip to content

Commit 9724348

Browse files
pablogsalvstinner
authored andcommitted
bpo-34279, regrtest: Issue a warning if no tests have been executed (GH-10150)
1 parent b2774c8 commit 9724348

File tree

5 files changed

+107
-7
lines changed

5 files changed

+107
-7
lines changed

Lib/test/libregrtest/main.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from test.libregrtest.runtest import (
1515
findtests, runtest, get_abs_module,
1616
STDTESTS, NOTTESTS, PASSED, FAILED, ENV_CHANGED, SKIPPED, RESOURCE_DENIED,
17-
INTERRUPTED, CHILD_ERROR,
17+
INTERRUPTED, CHILD_ERROR, TEST_DID_NOT_RUN,
1818
PROGRESS_MIN_TIME, format_test_result)
1919
from test.libregrtest.setup import setup_tests
2020
from test.libregrtest.utils import removepy, count, format_duration, printlist
@@ -79,6 +79,7 @@ def __init__(self):
7979
self.resource_denieds = []
8080
self.environment_changed = []
8181
self.rerun = []
82+
self.run_no_tests = []
8283
self.first_result = None
8384
self.interrupted = False
8485

@@ -118,6 +119,8 @@ def accumulate_result(self, test, result):
118119
elif ok == RESOURCE_DENIED:
119120
self.skipped.append(test)
120121
self.resource_denieds.append(test)
122+
elif ok == TEST_DID_NOT_RUN:
123+
self.run_no_tests.append(test)
121124
elif ok != INTERRUPTED:
122125
raise ValueError("invalid test result: %r" % ok)
123126

@@ -368,6 +371,11 @@ def display_result(self):
368371
print("%s:" % count(len(self.rerun), "re-run test"))
369372
printlist(self.rerun)
370373

374+
if self.run_no_tests:
375+
print()
376+
print(count(len(self.run_no_tests), "test"), "run no tests:")
377+
printlist(self.run_no_tests)
378+
371379
def run_tests_sequential(self):
372380
if self.ns.trace:
373381
import trace
@@ -458,6 +466,9 @@ def get_tests_result(self):
458466
result.append("FAILURE")
459467
elif self.ns.fail_env_changed and self.environment_changed:
460468
result.append("ENV CHANGED")
469+
elif not any((self.good, self.bad, self.skipped, self.interrupted,
470+
self.environment_changed)):
471+
result.append("NO TEST RUN")
461472

462473
if self.interrupted:
463474
result.append("INTERRUPTED")

Lib/test/libregrtest/runtest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
RESOURCE_DENIED = -3
2020
INTERRUPTED = -4
2121
CHILD_ERROR = -5 # error in a child process
22+
TEST_DID_NOT_RUN = -6 # error in a child process
2223

2324
_FORMAT_TEST_RESULT = {
2425
PASSED: '%s passed',
@@ -28,6 +29,7 @@
2829
RESOURCE_DENIED: '%s skipped (resource denied)',
2930
INTERRUPTED: '%s interrupted',
3031
CHILD_ERROR: '%s crashed',
32+
TEST_DID_NOT_RUN: '%s run no tests',
3133
}
3234

3335
# Minimum duration of a test to display its duration or to mention that
@@ -94,6 +96,7 @@ def runtest(ns, test):
9496
ENV_CHANGED test failed because it changed the execution environment
9597
FAILED test failed
9698
PASSED test passed
99+
EMPTY_TEST_SUITE test ran no subtests.
97100
98101
If ns.xmlpath is not None, xml_data is a list containing each
99102
generated testsuite element.
@@ -197,6 +200,8 @@ def test_runner():
197200
else:
198201
print("test", test, "failed", file=sys.stderr, flush=True)
199202
return FAILED, test_time
203+
except support.TestDidNotRun:
204+
return TEST_DID_NOT_RUN, test_time
200205
except:
201206
msg = traceback.format_exc()
202207
if not ns.pgo:

Lib/test/support/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
# globals
7373
"PIPE_MAX_SIZE", "verbose", "max_memuse", "use_resources", "failfast",
7474
# exceptions
75-
"Error", "TestFailed", "ResourceDenied",
75+
"Error", "TestFailed", "TestDidNotRun", "ResourceDenied",
7676
# imports
7777
"import_module", "import_fresh_module", "CleanImport",
7878
# modules
@@ -120,6 +120,9 @@ class Error(Exception):
120120
class TestFailed(Error):
121121
"""Test failed."""
122122

123+
class TestDidNotRun(Error):
124+
"""Test did not run any subtests."""
125+
123126
class ResourceDenied(unittest.SkipTest):
124127
"""Test skipped because it requested a disallowed resource.
125128
@@ -1930,6 +1933,8 @@ def _run_suite(suite):
19301933
if junit_xml_list is not None:
19311934
junit_xml_list.append(result.get_xml_element())
19321935

1936+
if not result.testsRun:
1937+
raise TestDidNotRun
19331938
if not result.wasSuccessful():
19341939
if len(result.errors) == 1 and not result.failures:
19351940
err = result.errors[0][1]

Lib/test/test_regrtest.py

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,20 @@ def setUp(self):
351351
self.tmptestdir = tempfile.mkdtemp()
352352
self.addCleanup(support.rmtree, self.tmptestdir)
353353

354-
def create_test(self, name=None, code=''):
354+
def create_test(self, name=None, code=None):
355355
if not name:
356356
name = 'noop%s' % BaseTestCase.TEST_UNIQUE_ID
357357
BaseTestCase.TEST_UNIQUE_ID += 1
358358

359+
if code is None:
360+
code = textwrap.dedent("""
361+
import unittest
362+
363+
class Tests(unittest.TestCase):
364+
def test_empty_test(self):
365+
pass
366+
""")
367+
359368
# test_regrtest cannot be run twice in parallel because
360369
# of setUp() and create_test()
361370
name = self.TESTNAME_PREFIX + name
@@ -390,7 +399,7 @@ def parse_executed_tests(self, output):
390399

391400
def check_executed_tests(self, output, tests, skipped=(), failed=(),
392401
env_changed=(), omitted=(),
393-
rerun=(),
402+
rerun=(), no_test_ran=(),
394403
randomize=False, interrupted=False,
395404
fail_env_changed=False):
396405
if isinstance(tests, str):
@@ -405,6 +414,8 @@ def check_executed_tests(self, output, tests, skipped=(), failed=(),
405414
omitted = [omitted]
406415
if isinstance(rerun, str):
407416
rerun = [rerun]
417+
if isinstance(no_test_ran, str):
418+
no_test_ran = [no_test_ran]
408419

409420
executed = self.parse_executed_tests(output)
410421
if randomize:
@@ -447,8 +458,12 @@ def list_regex(line_format, tests):
447458
regex = "Re-running test %r in verbose mode" % name
448459
self.check_line(output, regex)
449460

461+
if no_test_ran:
462+
regex = list_regex('%s test%s run no tests', no_test_ran)
463+
self.check_line(output, regex)
464+
450465
good = (len(tests) - len(skipped) - len(failed)
451-
- len(omitted) - len(env_changed))
466+
- len(omitted) - len(env_changed) - len(no_test_ran))
452467
if good:
453468
regex = r'%s test%s OK\.$' % (good, plural(good))
454469
if not skipped and not failed and good > 1:
@@ -465,12 +480,16 @@ def list_regex(line_format, tests):
465480
result.append('ENV CHANGED')
466481
if interrupted:
467482
result.append('INTERRUPTED')
468-
if not result:
483+
if not any((good, result, failed, interrupted, skipped,
484+
env_changed, fail_env_changed)):
485+
result.append("NO TEST RUN")
486+
elif not result:
469487
result.append('SUCCESS')
470488
result = ', '.join(result)
471489
if rerun:
472490
self.check_line(output, 'Tests result: %s' % result)
473491
result = 'FAILURE then %s' % result
492+
474493
self.check_line(output, 'Tests result: %s' % result)
475494

476495
def parse_random_seed(self, output):
@@ -649,7 +668,14 @@ def test_resources(self):
649668
# test -u command line option
650669
tests = {}
651670
for resource in ('audio', 'network'):
652-
code = 'from test import support\nsupport.requires(%r)' % resource
671+
code = textwrap.dedent("""
672+
from test import support; support.requires(%r)
673+
import unittest
674+
class PassingTest(unittest.TestCase):
675+
def test_pass(self):
676+
pass
677+
""" % resource)
678+
653679
tests[resource] = self.create_test(resource, code)
654680
test_names = sorted(tests.values())
655681

@@ -978,6 +1004,56 @@ def test_bug(self):
9781004
output = self.run_tests("-w", testname, exitcode=2)
9791005
self.check_executed_tests(output, [testname],
9801006
failed=testname, rerun=testname)
1007+
def test_no_tests_ran(self):
1008+
code = textwrap.dedent("""
1009+
import unittest
1010+
1011+
class Tests(unittest.TestCase):
1012+
def test_bug(self):
1013+
pass
1014+
""")
1015+
testname = self.create_test(code=code)
1016+
1017+
output = self.run_tests(testname, "-m", "nosuchtest", exitcode=0)
1018+
self.check_executed_tests(output, [testname], no_test_ran=testname)
1019+
1020+
def test_no_tests_ran_multiple_tests_nonexistent(self):
1021+
code = textwrap.dedent("""
1022+
import unittest
1023+
1024+
class Tests(unittest.TestCase):
1025+
def test_bug(self):
1026+
pass
1027+
""")
1028+
testname = self.create_test(code=code)
1029+
testname2 = self.create_test(code=code)
1030+
1031+
output = self.run_tests(testname, testname2, "-m", "nosuchtest", exitcode=0)
1032+
self.check_executed_tests(output, [testname, testname2],
1033+
no_test_ran=[testname, testname2])
1034+
1035+
def test_no_test_ran_some_test_exist_some_not(self):
1036+
code = textwrap.dedent("""
1037+
import unittest
1038+
1039+
class Tests(unittest.TestCase):
1040+
def test_bug(self):
1041+
pass
1042+
""")
1043+
testname = self.create_test(code=code)
1044+
other_code = textwrap.dedent("""
1045+
import unittest
1046+
1047+
class Tests(unittest.TestCase):
1048+
def test_other_bug(self):
1049+
pass
1050+
""")
1051+
testname2 = self.create_test(code=other_code)
1052+
1053+
output = self.run_tests(testname, testname2, "-m", "nosuchtest",
1054+
"-m", "test_other_bug", exitcode=0)
1055+
self.check_executed_tests(output, [testname, testname2],
1056+
no_test_ran=[testname])
9811057

9821058

9831059
class TestUtils(unittest.TestCase):
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
regrtest issue a warning when no tests have been executed in a particular
2+
test file. Also, a new final result state is issued if no test have been
3+
executed across all test files. Patch by Pablo Galindo.

0 commit comments

Comments
 (0)