fix classmethod not being discovered and add unit-test

This commit is contained in:
Marko Pacak 2022-11-30 23:30:09 +01:00
parent fd30759d94
commit e56898029b
2 changed files with 15 additions and 1 deletions

View File

@ -403,7 +403,7 @@ class PyCollector(PyobjMixin, nodes.Collector):
def istestfunction(self, obj: object, name: str) -> bool: def istestfunction(self, obj: object, name: str) -> bool:
if self.funcnamefilter(name) or self.isnosetest(obj): if self.funcnamefilter(name) or self.isnosetest(obj):
if isinstance(obj, staticmethod): if isinstance(obj, staticmethod) or isinstance(obj, classmethod):
# staticmethods need to be unwrapped. # staticmethods need to be unwrapped.
obj = safe_getattr(obj, "__func__", False) obj = safe_getattr(obj, "__func__", False)
return callable(obj) and fixtures.getfixturemarker(obj) is None return callable(obj) and fixtures.getfixturemarker(obj) is None

View File

@ -735,6 +735,20 @@ class Test_genitems:
assert s.endswith("test_example_items1.testone") assert s.endswith("test_example_items1.testone")
print(s) print(s)
def test_classmethod_is_discovered(self, pytester: Pytester) -> None:
"""Test that classmethods are discovered"""
p = pytester.makepyfile(
"""
class TestCase:
@classmethod
def test_classmethod(cls) -> None:
pass
"""
)
items, reprec = pytester.inline_genitems(p)
ids = [x.getmodpath() for x in items] # type: ignore[attr-defined]
assert ids == ["TestCase.test_classmethod"]
def test_class_and_functions_discovery_using_glob(self, pytester: Pytester) -> None: def test_class_and_functions_discovery_using_glob(self, pytester: Pytester) -> None:
"""Test that Python_classes and Python_functions config options work """Test that Python_classes and Python_functions config options work
as prefixes and glob-like patterns (#600).""" as prefixes and glob-like patterns (#600)."""