mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 22:18:37 +00:00
unittest discover loads TestCase classes defined in a package's __init__.py regardless of the filename pattern. The parallel runner only selected test*.py files, so a failing package initializer could be skipped while -j 1 reported it. Include package initializers in the parallel module list; an empty initializer with no tests is not a failure. Add regressions for failing initializers and for a pattern that matches no test filenames. AI-assisted.
213 lines
11 KiB
Python
213 lines
11 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"
|
|
|
|
|
|
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_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_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()
|