From e56898029bf0b6303182b4b48b48e51e68d133f8 Mon Sep 17 00:00:00 2001 From: Marko Pacak Date: Wed, 30 Nov 2022 23:30:09 +0100 Subject: [PATCH] fix classmethod not being discovered and add unit-test --- src/_pytest/python.py | 2 +- testing/test_collection.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/_pytest/python.py b/src/_pytest/python.py index 1e30d42ce..79cf0476b 100644 --- a/src/_pytest/python.py +++ b/src/_pytest/python.py @@ -403,7 +403,7 @@ class PyCollector(PyobjMixin, nodes.Collector): def istestfunction(self, obj: object, name: str) -> bool: 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. obj = safe_getattr(obj, "__func__", False) return callable(obj) and fixtures.getfixturemarker(obj) is None diff --git a/testing/test_collection.py b/testing/test_collection.py index 58e1d862a..d907244d5 100644 --- a/testing/test_collection.py +++ b/testing/test_collection.py @@ -735,6 +735,20 @@ class Test_genitems: assert s.endswith("test_example_items1.testone") 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: """Test that Python_classes and Python_functions config options work as prefixes and glob-like patterns (#600)."""