Skip to content

Commit beb63d2

Browse files
committed
allow staticmethods to be detected as test functions
Allow a class method decorated `@staticmethod` to be collected as a test function (if it meets the usual criteria).
1 parent 9b51fc6 commit beb63d2

File tree

4 files changed

+16
-0
lines changed

4 files changed

+16
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Michael Droettboom
118118
Michael Seifert
119119
Michal Wajszczuk
120120
Mike Lundy
121+
Nathaniel Waisbrot
121122
Ned Batchelder
122123
Neven Mundar
123124
Nicolas Delaby

_pytest/python.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,8 @@ def classnamefilter(self, name):
271271
return self._matches_prefix_or_glob_option('python_classes', name)
272272

273273
def istestfunction(self, obj, name):
274+
if isinstance(obj, staticmethod):
275+
obj = obj.__func__
274276
return (
275277
(self.funcnamefilter(name) or self.isnosetest(obj)) and
276278
safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None

changelog/2528.feature

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Allow class methods decorated as ``@staticmethod`` to be candidates for collection as a test function.

testing/python/collect.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,18 @@ class test(object):
143143
"*collected 0*",
144144
])
145145

146+
def test_static_method(self, testdir):
147+
testdir.getmodulecol("""
148+
class Test(object):
149+
@staticmethod
150+
def test_something():
151+
pass
152+
""")
153+
result = testdir.runpytest()
154+
result.stdout.fnmatch_lines([
155+
"*collected 1*",
156+
])
157+
146158
def test_setup_teardown_class_as_classmethod(self, testdir):
147159
testdir.makepyfile(test_mod1="""
148160
class TestClassMethod(object):

0 commit comments

Comments
 (0)