Skip to content
Open
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
85 changes: 80 additions & 5 deletions tests/test_ci_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,16 +219,35 @@ 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.
"""
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, 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):
bases[node.name] = [
"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] = has_test_method(node)
case_aliases.discard(node.name)
elif isinstance(node, (ast.Import, ast.ImportFrom)):
for item in node.names:
Expand Down Expand Up @@ -258,13 +277,28 @@ 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):
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)
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
if any(b not in bases and b not in ("TestCase", "object") for b in bases[node.name]):
# 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"):
Expand Down Expand Up @@ -393,6 +427,47 @@ 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),
("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")]
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 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
# failing, and unittest discover ignores a module with no TestCase.
Expand Down