From fd0a5390753c24c92428b6beadd41b704a93cbb1 Mon Sep 17 00:00:00 2001 From: milk4sale <53350507+milk4sale@users.noreply.github.com> Date: Tue, 24 Nov 2026 16:36:12 -0600 Subject: [PATCH 1/2] tests: the CI registration scan counts the tests a TestCase inherits from a class of its module (TEST-CI-REG-LOCAL-MIXIN) tests/test_ci_registration.py fails on main since 540d5111f (the merge of #930) and passes on its parent: test_every_selected_file_exposes_a_test_to_its_runner reports tests/semantic/test_sub_co_same_operand.py as a file the semantic job collects nothing from. `python -m unittest discover -s tests/semantic -p test_sub_co_same_operand.py` runs 6 tests there. Its three TestCase classes have no test method of their own: they take them from a mixin class defined in the same file, one TestCase per wave size. defines_a_test looked at a TestCase's own body and at a base imported from another module, not at a base defined in the module. It now also counts the test* methods of the classes of the module a TestCase derives from, directly or through other classes of the module, and an imported base reached that way. unittest's loader lists a class's tests with dir(), so these are the tests it runs. The new test writes eight small modules, runs unittest discover on them once and holds the scan to what the runner collected: a mixin above the TestCase, two classes up, beside a TestCase base without tests, an imported mixin named directly and through a local class (a test each), and a base without tests, a mixin no TestCase derives from and a mixin without a TestCase (none). With main's defines_a_test put back, four of the five cases with a test fail, and so does the repository check. Over the repository at 51a6074d2: the scan lists the same 997 test files; of the 473 the semantic job selects, main's scan found a test in 472 and this one in 473, and test_sub_co_same_operand.py is the only file the two disagree on (none of the pytest job's 36). unittest's loader collects tests from all 473 (6191 tests, no import failure), so main's scan disagreed with the runner on that one file and this one on none. Co-Authored-By: Claude Fable 5.2 --- tests/test_ci_registration.py | 62 +++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/tests/test_ci_registration.py b/tests/test_ci_registration.py index 6dd23e93e7..cb01dee81a 100644 --- a/tests/test_ci_registration.py +++ b/tests/test_ci_registration.py @@ -219,9 +219,13 @@ def defines_a_test(source, require_test_case=False): ``unittest discover`` only collects TestCase subclasses, so the semantic job passes ``require_test_case=True``: a module with only ``def test_x(): ...`` runs zero cases there even though pytest would collect it. + + A TestCase runs the ``test*`` methods it inherits as well as its own (the loader lists them with ``dir()``), + so a TestCase with none of its own counts when a class of this module that it derives from defines one: the + mixin of ``tests/semantic/test_sub_co_same_operand.py``, shared by one TestCase per wave size. """ tree = ast.parse(source) - bases, case_aliases = {}, set() + bases, own_tests, case_aliases = {}, {}, set() # Recognize bare aliases introduced by a direct import; this is not a general name resolver. for node in tree.body: if isinstance(node, ast.ClassDef): @@ -229,6 +233,8 @@ def defines_a_test(source, require_test_case=False): "TestCase" if isinstance(b, ast.Name) and b.id in case_aliases else ast.unparse(b).rsplit(".", 1)[-1] for b in node.bases ] + own_tests[node.name] = any(isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) + and item.name.startswith("test") for item in node.body) case_aliases.discard(node.name) elif isinstance(node, (ast.Import, ast.ImportFrom)): for item in node.names: @@ -258,13 +264,29 @@ def is_test_case(name): stack.append(base) return False + def local_ancestors(name): + """The classes of this module `name` derives from, directly or through other classes of this module.""" + seen, stack = [], [name] + while stack: + for base in bases[stack.pop()]: + if base in bases and base not in seen: + seen.append(base) + stack.append(base) + return seen + for node in tree.body: if isinstance(node, ast.ClassDef) and is_test_case(node.name): if any(isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) and item.name.startswith("test") for item in node.body): return True - # a mixin imported from another module (tests/semantic/f16_unary_cases.py's UnaryF16) brings its tests - if any(b not in bases and b not in ("TestCase", "object") for b in bases[node.name]): + # the test* methods of the classes of this module it derives from are its tests too + ancestors = local_ancestors(node.name) + if any(own_tests[name] for name in ancestors): + return True + # a mixin imported from another module (tests/semantic/f16_unary_cases.py's UnaryF16) brings its tests, + # to the class that names it and to the classes of this module that derive from that one + if any(b not in bases and b not in ("TestCase", "object") + for name in [node.name] + ancestors for b in bases[name]): return True if not require_test_case and isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)) \ and node.name.startswith("test"): @@ -393,6 +415,40 @@ def test_direct_testcase_import_alias(self): with self.subTest(kind=kind): self.assertEqual(defines_a_test(source, require_test_case=True), expected) + def test_inherited_tests_count_where_unittest_discover_collects_them(self): + # The expectations are the runner's: unittest discover is run on these modules, and the scan has to find a + # test in exactly the ones it collects one from. + mixin = "class Mixin:\n def test_one(self): pass\n" + cases = ( + ("mixin_above", "import unittest\n" + mixin + "class Example(Mixin, unittest.TestCase): pass\n", True), + ("mixin_two_classes_up", "import unittest\n" + mixin + "class Middle(Mixin): pass\n" + "class Example(Middle, unittest.TestCase): pass\n", True), + ("mixin_beside_an_empty_case", "import unittest\n" + mixin + "class Base(unittest.TestCase): pass\n" + "class Example(Mixin, Base): pass\n", True), + ("imported_mixin", "import unittest\nfrom shared_cases import Cases\n" + "class Example(Cases, unittest.TestCase): pass\n", True), + ("imported_mixin_through_a_local_class", "import unittest\nfrom shared_cases import Cases\n" + "class Middle(Cases): pass\nclass Example(Middle, unittest.TestCase): pass\n", True), + ("base_without_tests", "import unittest\nclass Helper:\n def helper(self): pass\n" + "class Example(Helper, unittest.TestCase): pass\n", False), + ("mixin_no_case_derives_from", "import unittest\n" + mixin + + "class Example(unittest.TestCase): pass\n", False), + ("mixin_without_a_case", mixin + "class Example(Mixin): pass\n", False), + ) + with tempfile.TemporaryDirectory() as tmp: + sources = [("shared_cases.py", "class Cases:\n def test_one(self): pass\n")] + sources += [(f"test_{kind}.py", source) for kind, source, _ in cases] + for name, source in sources: + with open(os.path.join(tmp, name), "w", encoding="utf-8", newline="\n") as f: + f.write(source) + result = subprocess.run([sys.executable, "-m", "unittest", "discover", "-s", ".", "-v"], + cwd=tmp, capture_output=True, text=True, timeout=30, check=True) + for kind, source, expected in cases: + with self.subTest(kind=kind): + self.assertEqual(f"(test_{kind}.Example" in result.stderr, expected, result.stderr) + self.assertEqual(defines_a_test(source, require_test_case=True), expected) + self.assertIn("Ran 5 tests", result.stderr) + def test_every_selected_file_exposes_a_test_to_its_runner(self): # Selection is not execution: pytest imports a file it is handed and reports zero items without # failing, and unittest discover ignores a module with no TestCase. From a1274181a827762c9f6b8911851f9786e6f88f4b Mon Sep 17 00:00:00 2001 From: xel1uz <16402913+xel1uz@users.noreply.github.com> Date: Tue, 24 Nov 2026 23:43:54 +0100 Subject: [PATCH 2/2] Exclude property-only mixins from unittest registration counts AI-assisted: OpenAI Codex. --- tests/test_ci_registration.py | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/test_ci_registration.py b/tests/test_ci_registration.py index cb01dee81a..ba2a49f8a1 100644 --- a/tests/test_ci_registration.py +++ b/tests/test_ci_registration.py @@ -224,6 +224,20 @@ def defines_a_test(source, require_test_case=False): so a TestCase with none of its own counts when a class of this module that it derives from defines one: the mixin of ``tests/semantic/test_sub_co_same_operand.py``, shared by one TestCase per wave size. """ + def has_test_method(cls): + # unittest calls getattr on the class and checks callability. A property and its accessors + # yield a descriptor on the class, even if the underlying function is named test_*. + properties = {} + for item in cls.body: + if not isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)): + continue + outer = item.decorator_list[0] if item.decorator_list else None + getter = isinstance(outer, ast.Name) and outer.id == "property" + accessor = (isinstance(outer, ast.Attribute) and outer.attr in ("getter", "setter", "deleter") + and isinstance(outer.value, ast.Name) and properties.get(outer.value.id, False)) + properties[item.name] = getter or accessor + return any(name.startswith("test") and not prop for name, prop in properties.items()) + tree = ast.parse(source) bases, own_tests, case_aliases = {}, {}, set() # Recognize bare aliases introduced by a direct import; this is not a general name resolver. @@ -233,8 +247,7 @@ def defines_a_test(source, require_test_case=False): "TestCase" if isinstance(b, ast.Name) and b.id in case_aliases else ast.unparse(b).rsplit(".", 1)[-1] for b in node.bases ] - own_tests[node.name] = any(isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) - and item.name.startswith("test") for item in node.body) + own_tests[node.name] = has_test_method(node) case_aliases.discard(node.name) elif isinstance(node, (ast.Import, ast.ImportFrom)): for item in node.names: @@ -276,8 +289,7 @@ def local_ancestors(name): for node in tree.body: if isinstance(node, ast.ClassDef) and is_test_case(node.name): - if any(isinstance(item, (ast.FunctionDef, ast.AsyncFunctionDef)) and item.name.startswith("test") - for item in node.body): + if has_test_method(node): return True # the test* methods of the classes of this module it derives from are its tests too ancestors = local_ancestors(node.name) @@ -434,6 +446,13 @@ def test_inherited_tests_count_where_unittest_discover_collects_them(self): ("mixin_no_case_derives_from", "import unittest\n" + mixin + "class Example(unittest.TestCase): pass\n", False), ("mixin_without_a_case", mixin + "class Example(Mixin): pass\n", False), + ("property_only_mixin", "import unittest\nclass Mixin:\n" + " @property\n def test_label(self): return 'ready'\n" + " @test_label.setter\n def test_label(self, value): pass\n" + "class Example(Mixin, unittest.TestCase): pass\n", False), + ("staticmethod_in_mixin", "import unittest\nclass Mixin:\n" + " @staticmethod\n def test_one(): pass\n" + "class Example(Mixin, unittest.TestCase): pass\n", True), ) with tempfile.TemporaryDirectory() as tmp: sources = [("shared_cases.py", "class Cases:\n def test_one(self): pass\n")] @@ -447,7 +466,7 @@ def test_inherited_tests_count_where_unittest_discover_collects_them(self): with self.subTest(kind=kind): self.assertEqual(f"(test_{kind}.Example" in result.stderr, expected, result.stderr) self.assertEqual(defines_a_test(source, require_test_case=True), expected) - self.assertIn("Ran 5 tests", result.stderr) + self.assertIn("Ran 6 tests", result.stderr) def test_every_selected_file_exposes_a_test_to_its_runner(self): # Selection is not execution: pytest imports a file it is handed and reports zero items without