Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
33 changes: 23 additions & 10 deletions tests/test_semantic_mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,11 +263,14 @@ def umax_line(self):
self.skipTest("v_sad_u8's UMax line moved")

def run_tool(self, extra, main_block=True):
line = self.umax_line()
source = SAD_TEST.format(semantic=os.path.join(ROOT, "tests", "semantic"), extra=extra)
if not main_block:
source = source.replace(MAIN_BLOCK, "")
self.assertNotIn("__main__", source)
return self.run_source(source)

def run_source(self, source):
line = self.umax_line()
with tempfile.TemporaryDirectory() as tmp:
test = os.path.join(tmp, "test_sad_probe.py")
with open(test, "w", encoding="utf-8", newline="\n") as f:
Expand All @@ -293,15 +296,25 @@ def test_file_without_a_main_block_still_runs_its_tests(self):
self.assertRegex(out, r"caught\s+line \d+: last operand \{ya\} -> \{xa\}")

def test_file_with_no_tests_is_a_baseline_error(self):
line = self.umax_line()
with tempfile.TemporaryDirectory() as tmp:
test = os.path.join(tmp, "test_empty_probe.py")
with open(test, "w", encoding="utf-8", newline="\n") as f:
f.write("import unittest\n")
p = subprocess.run([sys.executable, "-I", TOOL, test, "--lines", f"{line}-{line}"],
capture_output=True, text=True, timeout=600)
self.assertEqual(p.returncode, 2, p.stdout + p.stderr)
self.assertIn("no tests ran", p.stderr)
code, out = self.run_source("import unittest\n")
self.assertEqual(code, 2, out)
self.assertIn("no tests ran", out)

def test_class_skipped_before_any_test_is_a_baseline_error(self):
code, out = self.run_source(textwrap.dedent('''\
import unittest

class T(unittest.TestCase):
@classmethod
def setUpClass(cls):
raise unittest.SkipTest("synthetic fixture unavailable")

def test_probe(self):
raise AssertionError("fixture is required")
'''))
self.assertEqual(code, 2, out)
self.assertIn("no tests ran", out)
self.assertNotIn("SURVIVED", out)


class RunTestEncoding(unittest.TestCase):
Expand Down
4 changes: 3 additions & 1 deletion tools/semantic_mutants.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,9 @@ def run_test(test_file: str, tools_dir: str, timeout: int) -> tuple[bool, str]:
encoding="utf-8", errors="replace", timeout=timeout)
except subprocess.TimeoutExpired:
return False, f"timed out after {timeout} s"
if p.returncode == 5: # unittest's exit status when it found no tests (Python 3.12+)
# unittest can exit successfully after a class or module fixture skips every test before it starts.
zero_tests = re.search(r"^Ran 0 tests in .+\n\nOK(?: \(skipped=\d+\))?$", p.stderr, re.M)
if p.returncode == 5 or (p.returncode == 0 and zero_tests):
return False, "no tests ran"
tail = ((p.stdout or "") + (p.stderr or "")).strip().splitlines()
return p.returncode == 0, tail[-1] if tail else ""
Expand Down
Loading