Files
mandy/tests/test_run_semantic_tests.py
Shang Li f48c2b2a35 semantic tests: count from unittest's result object
The count came from a line of the shape "Ran N tests in ..." in the module's
stderr. unittest writes that summary after every test has run, but a module
can write a line of the same shape after it: an atexit handler that runs a
nested runner does exactly that, and the runner counted "Ran 0 tests", so a
module with one passing test looked empty and the run exited 1 with "no
tests ran".

The count is now taken from unittest's own result object. The child program
runs the module through unittest's program, the way "python -m unittest
<module>" does, writes the result to a file and exits with unittest's own
exit codes: 1 when a test failed, 5 when nothing ran at all, 0 otherwise.
Nothing the module writes can change the count any more.

Follow-up to @lixonik's review on #662, which asked for the count to come
from the result rather than from a line of the module's stderr. AI-assisted.
2026-11-25 01:18:48 +08:00

288 lines
16 KiB
Python

"""scripts/run_semantic_tests.py on a throwaway suite: it finds what unittest discover finds, and a failing, crashing
or unimportable file always makes it exit 1 with that file named, in parallel and with -j 1.
python -I tests/test_run_semantic_tests.py
"""
import contextlib
import io
import os
import subprocess
import sys
import tempfile
import unittest
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "scripts"))
import run_semantic_tests as r # noqa: E402
PASS = "import unittest\nimport helper\nclass T(unittest.TestCase):\n def test_a(self): self.assertEqual(helper.X, 1)\n" \
" def test_b(self): pass\n"
FAIL = "import unittest\nclass T(unittest.TestCase):\n def test_a(self): self.assertEqual(1, 2, 'boom-msg')\n"
BROKEN = "import no_such_module_anywhere\n"
HANG = "import time\nprint('before-sleep-marker', flush=True)\ntime.sleep(60)\n"
RAN0 = ("import unittest\nclass T(unittest.TestCase):\n def test_a(self):\n"
" print('Ran 0 tests in 0.000s', flush=True)\n")
RAN0_AT_EXIT = ("import atexit\nimport unittest\natexit.register(lambda: print('Ran 0 tests in 0.000s'))\n"
"class T(unittest.TestCase):\n def test_a(self):\n pass\n")
# An atexit handler that writes to stderr after unittest's own summary: the count comes from unittest's result
# object, so the late line cannot change it.
ATEXIT_STDERR = ("import atexit\nimport sys\nimport unittest\n"
"atexit.register(lambda: unittest.TextTestRunner(stream=sys.stderr, verbosity=0)"
".run(unittest.TestSuite()))\n"
"class T(unittest.TestCase):\n def test_a(self):\n pass\n")
LATE_SUMMARY = ("import atexit\nimport sys\nimport unittest\n"
"atexit.register(lambda: print('Ran 7 tests in 0.000s', file=sys.stderr))\n"
"class T(unittest.TestCase):\n def test_a(self):\n pass\n")
# A test that runs a nested unittest runner: that runner writes its own summary to stderr while the test runs.
NESTED = ("import sys\nimport unittest\nclass T(unittest.TestCase):\n def test_a(self):\n"
" unittest.TextTestRunner(stream=sys.stderr, verbosity=0).run(unittest.TestSuite())\n")
def suite(tmp, files):
for rel, text in files.items():
path = os.path.join(tmp, *rel.split("/"))
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(text)
return tmp
def run(*argv):
out = io.StringIO()
with contextlib.redirect_stdout(out), contextlib.redirect_stderr(io.StringIO()):
code = r.main(list(argv))
return code, out.getvalue()
def cli(*argv):
return subprocess.run([sys.executable, "-I", os.path.join(ROOT, "scripts", "run_semantic_tests.py"), *argv],
capture_output=True, text=True, timeout=15)
class RunnerTest(unittest.TestCase):
def test_parallel_timeout_reports_partial_output_and_keeps_healthy_modules(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_hang.py": HANG,
"test_pass_one.py": PASS, "test_pass_two.py": PASS})
result = cli(tmp, "-j", "2", "--timeout", "1")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("before-sleep-marker", result.stdout)
self.assertIn("==== FAILED: test_hang", result.stdout)
self.assertIn("TIMED OUT after 1 s: test_hang", result.stdout)
self.assertIn("3 files, 4 tests, 1 files failed", result.stdout)
self.assertNotIn("Traceback", result.stdout + result.stderr)
def test_the_count_comes_from_unittest_not_from_a_test_line(self):
# A test may print a line of the same shape as unittest's own summary. unittest writes its summary to stderr
# and a test's own output to stdout, so the count comes from stderr: in the merged output it was whichever
# line came first, which depended on stdout buffering.
for source in (RAN0, RAN0_AT_EXIT):
with self.subTest(source=source.split(chr(10))[0]), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_counts.py": source})
result = cli(tmp, "-j", "2")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("1 files, 1 tests, 0 files failed", result.stdout)
self.assertNotIn("no tests ran", result.stdout)
def test_the_count_comes_from_unittest_not_from_a_nested_runner(self):
# A test that runs a nested unittest runner writes that runner's summary ("Ran 0 tests in ...") to stderr
# while the test runs, before unittest writes its own. The count must come from unittest's own summary.
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_counts.py": NESTED})
result = cli(tmp, "-j", "2")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("1 files, 1 tests, 0 files failed", result.stdout)
self.assertNotIn("no tests ran", result.stdout)
def test_the_count_comes_from_the_result_object_not_from_a_late_summary(self):
# A summary written to stderr after unittest's own - an atexit handler that runs a nested runner, or one
# that prints a line of that shape - must not change the count: it comes from unittest's result object.
for source in (ATEXIT_STDERR, LATE_SUMMARY):
with self.subTest(source=source.split(chr(10))[0]), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_counts.py": source})
result = cli(tmp, "-j", "2")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertIn("1 files, 1 tests, 0 files failed", result.stdout)
self.assertNotIn("no tests ran", result.stdout)
def test_serial_timeout_keeps_discover_output_and_names_the_suite(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_hang.py": HANG})
result = cli(tmp, "-j", "1", "--timeout", "1")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("before-sleep-marker", result.stdout)
self.assertIn("TIMED OUT after 1 s: unittest discover in", result.stderr)
self.assertIn(tmp, result.stderr)
self.assertNotIn("Traceback", result.stdout + result.stderr)
def test_timeout_requires_positive_finite_seconds(self):
for value in ("0", "-1", "nan", "inf", "-inf"):
with self.subTest(value=value):
result = cli("unused-suite", "--timeout=" + value)
self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
self.assertIn("positive finite", result.stderr)
self.assertNotIn("Traceback", result.stderr)
def test_out_of_range_timeout_is_rejected_before_running_the_suite(self):
for jobs in (1, 2):
for value in ("86400.1", "1e100"):
with self.subTest(jobs=jobs, value=value):
result = cli("unused-suite", "-j", str(jobs), "--timeout=" + value)
self.assertEqual(result.returncode, 2, result.stdout + result.stderr)
self.assertIn("no greater than 86400", result.stderr)
self.assertNotIn("Traceback", result.stderr)
self.assertNotIn("TIMED OUT", result.stdout + result.stderr)
self.assertNotIn("no test*.py files", result.stdout)
def test_maximum_timeout_runs_a_passing_suite_in_both_modes(self):
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_pass.py": PASS})
result = cli(tmp, "-j", str(jobs), "--timeout", "86400")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_custom_patterns_find_only_python_modules_in_packages(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"case_one.py": "", "case_two.txt": "", "case_bad-name.py": "",
"pkg/__init__.py": "", "pkg/case_two.py": "", "plain/case_three.py": ""})
self.assertEqual(r.test_modules(tmp, "case_*.py"), ["case_one", "pkg.__init__", "pkg.case_two"])
self.assertEqual(r.test_modules(tmp, "*"), ["case_one", "pkg.__init__", "pkg.case_two"])
def test_pattern_selects_files_without_importing_excluded_modules(self):
for jobs in (1, 2):
for flag in ("-p", "--pattern"):
with self.subTest(jobs=jobs, flag=flag), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "case_one.py": PASS,
"pkg/__init__.py": "", "pkg/case_two.py": PASS,
"test_excluded.py": "raise AssertionError('excluded module imported')\n",
"plain/case_three.py": BROKEN})
result = cli(tmp, "-j", str(jobs), flag, "case_*.py")
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertNotIn("excluded module imported", result.stdout + result.stderr)
if jobs == 1:
self.assertIn("Ran 4 tests", result.stderr)
else:
self.assertIn("3 files, 4 tests, 0 files failed", result.stdout)
def test_pattern_no_matches_is_an_error_in_both_modes(self):
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_excluded.py": BROKEN})
result = cli(tmp, "-j", str(jobs), "--pattern", "case_*.py")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("no case_*.py files", result.stdout)
self.assertNotIn("Traceback", result.stderr)
def test_matching_failure_is_not_hidden_by_pattern_selection(self):
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"case_fail.py": FAIL, "test_excluded.py": BROKEN})
result = cli(tmp, "-j", str(jobs), "--pattern", "case_*.py")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("boom-msg", result.stdout + result.stderr)
self.assertNotIn("no_such_module_anywhere", result.stdout + result.stderr)
def test_omitting_pattern_keeps_the_default_selection(self):
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_pass.py": PASS,
"case_excluded.py": "raise AssertionError('custom module imported')\n"})
result = cli(tmp, "-j", str(jobs))
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
self.assertNotIn("custom module imported", result.stdout + result.stderr)
def test_finds_what_discover_finds(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"test_one.py": "", "helper.py": "", "pkg/__init__.py": "", "pkg/test_two.py": "",
"plain_dir/test_three.py": "", "testx.py": ""})
# discover enters packages only, and its pattern is test*.py
self.assertEqual(r.test_modules(tmp), ["test_one", "testx", "pkg.__init__", "pkg.test_two"])
def test_package_initializer_failure_is_seen_in_both_modes(self):
init_fail = "import unittest\nclass T(unittest.TestCase):\n" \
" def test_fail(self): self.assertEqual(1, 2, 'init-boom')\n"
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "pkg/__init__.py": init_fail, "pkg/test_child.py": PASS})
result = cli(tmp, "-j", str(jobs))
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("init-boom", result.stdout + result.stderr)
if jobs == 1:
self.assertIn("Ran 3 tests", result.stderr)
else:
self.assertIn("==== FAILED: pkg.__init__", result.stdout)
self.assertIn("2 files, 3 tests, 1 files failed", result.stdout)
def test_package_initializer_runs_when_no_filename_matches_pattern(self):
init_fail = "import unittest\nclass T(unittest.TestCase):\n" \
" def test_fail(self): self.assertEqual(1, 2, 'init-boom')\n"
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "pkg/__init__.py": init_fail, "pkg/test_child.py": PASS})
result = cli(tmp, "-j", str(jobs), "-p", "zzz*.py")
self.assertEqual(result.returncode, 1, result.stdout + result.stderr)
self.assertIn("init-boom", result.stdout + result.stderr)
def test_package_initializer_runs_once_in_parallel(self):
# `python -m unittest pkg.__init__` imports pkg and then runs the same file again as pkg.__init__, so this
# registration happens twice; the runner hands unittest the package name instead.
init = ("import unittest\n"
"registrations = []\n"
"registrations.append(__name__)\n"
"class T(unittest.TestCase):\n"
" def test_once(self):\n"
" self.assertEqual(registrations, ['pkg'])\n")
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"pkg/__init__.py": init})
result = cli(tmp, "-j", str(jobs))
self.assertEqual(result.returncode, 0, result.stdout + result.stderr)
def test_empty_package_alone_is_not_a_false_green(self):
# -j 1 forwards to discover, which exits 5 when nothing ran; a parallel run whose only job is an empty
# initializer must not report success either.
for jobs in (1, 2):
with self.subTest(jobs=jobs), tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"pkg/__init__.py": ""})
result = cli(tmp, "-j", str(jobs))
self.assertNotEqual(result.returncode, 0, result.stdout + result.stderr)
if jobs == 2:
self.assertIn("no tests ran", result.stdout)
def test_all_pass(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_p1.py": PASS, "test_p2.py": PASS})
code, out = run(tmp, "-j", "2")
self.assertEqual(code, 0, out)
self.assertIn("2 files, 4 tests, 0 files failed", out)
def test_a_failure_or_import_error_is_reported_and_fails_the_run(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_p.py": PASS, "test_f.py": FAIL, "test_b.py": BROKEN})
code, out = run(tmp, "-j", "3")
self.assertEqual(code, 1, out)
self.assertIn("==== FAILED: test_f", out)
self.assertIn("boom-msg", out) # the failing file's own output is shown
self.assertIn("==== FAILED: test_b", out)
self.assertIn("2 files failed", out)
self.assertTrue(out.rstrip().endswith("test_b, test_f"), out)
def test_one_job_runs_discover_and_keeps_its_exit_code(self):
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_p.py": PASS, "test_f.py": FAIL})
self.assertEqual(run(tmp, "-j", "1")[0], 1)
with tempfile.TemporaryDirectory() as tmp:
suite(tmp, {"helper.py": "X = 1\n", "test_p.py": PASS})
self.assertEqual(run(tmp, "-j", "1")[0], 0)
def test_an_empty_suite_fails(self):
with tempfile.TemporaryDirectory() as tmp:
code, out = run(tmp, "-j", "2")
self.assertEqual(code, 1)
self.assertIn("no test*.py files", out)
if __name__ == "__main__":
unittest.main()