Skip to content

Commit 0303d95

Browse files
Merge pull request #2531 from waisbrot/staticmethods
Allow staticmethods to be detected as test functions
2 parents 9b51fc6 + 9b9fede commit 0303d95

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,19 @@ def classnamefilter(self, name):
271271
return self._matches_prefix_or_glob_option('python_classes', name)
272272

273273
def istestfunction(self, obj, name):
274-
return (
275-
(self.funcnamefilter(name) or self.isnosetest(obj)) and
276-
safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None
277-
)
274+
if self.funcnamefilter(name) or self.isnosetest(obj):
275+
if isinstance(obj, staticmethod):
276+
# static methods need to be unwrapped
277+
obj = safe_getattr(obj, '__func__', False)
278+
if obj is False:
279+
# Python 2.6 wraps in a different way that we won't try to handle
280+
self.warn(code="C2", message="cannot collect static method %r because it is not a function (always the case in Python 2.6)" % name)
281+
return False
282+
return (
283+
safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None
284+
)
285+
else:
286+
return False
278287

279288
def istestclass(self, obj, name):
280289
return self.classnamefilter(name) or self.isnosetest(obj)

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. (Only for Python 2.7 and above. Python 2.6 will still ignore static methods.)

testing/python/collect.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,26 @@ 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+
if sys.version_info < (2,7):
155+
# in 2.6, the code to handle static methods doesn't work
156+
result.stdout.fnmatch_lines([
157+
"*collected 0 items*",
158+
"*cannot collect static method*",
159+
])
160+
else:
161+
result.stdout.fnmatch_lines([
162+
"*collected 1 item*",
163+
"*1 passed in*",
164+
])
165+
146166
def test_setup_teardown_class_as_classmethod(self, testdir):
147167
testdir.makepyfile(test_mod1="""
148168
class TestClassMethod(object):

0 commit comments

Comments
 (0)