Skip to content

Commit

Permalink
Merge pull request #2531 from waisbrot/staticmethods
Browse files Browse the repository at this point in the history
Allow staticmethods to be detected as test functions
  • Loading branch information
RonnyPfannschmidt authored Jun 29, 2017
2 parents 9b51fc6 + 9b9fede commit 0303d95
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ Michael Droettboom
Michael Seifert
Michal Wajszczuk
Mike Lundy
Nathaniel Waisbrot
Ned Batchelder
Neven Mundar
Nicolas Delaby
Expand Down
17 changes: 13 additions & 4 deletions _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,10 +271,19 @@ def classnamefilter(self, name):
return self._matches_prefix_or_glob_option('python_classes', name)

def istestfunction(self, obj, name):
return (
(self.funcnamefilter(name) or self.isnosetest(obj)) and
safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None
)
if self.funcnamefilter(name) or self.isnosetest(obj):
if isinstance(obj, staticmethod):
# static methods need to be unwrapped
obj = safe_getattr(obj, '__func__', False)
if obj is False:
# Python 2.6 wraps in a different way that we won't try to handle
self.warn(code="C2", message="cannot collect static method %r because it is not a function (always the case in Python 2.6)" % name)
return False
return (
safe_getattr(obj, "__call__", False) and fixtures.getfixturemarker(obj) is None
)
else:
return False

def istestclass(self, obj, name):
return self.classnamefilter(name) or self.isnosetest(obj)
Expand Down
1 change: 1 addition & 0 deletions changelog/2528.feature
Original file line number Diff line number Diff line change
@@ -0,0 +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.)
20 changes: 20 additions & 0 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ class test(object):
"*collected 0*",
])

def test_static_method(self, testdir):
testdir.getmodulecol("""
class Test(object):
@staticmethod
def test_something():
pass
""")
result = testdir.runpytest()
if sys.version_info < (2,7):
# in 2.6, the code to handle static methods doesn't work
result.stdout.fnmatch_lines([
"*collected 0 items*",
"*cannot collect static method*",
])
else:
result.stdout.fnmatch_lines([
"*collected 1 item*",
"*1 passed in*",
])

def test_setup_teardown_class_as_classmethod(self, testdir):
testdir.makepyfile(test_mod1="""
class TestClassMethod(object):
Expand Down

0 comments on commit 0303d95

Please sign in to comment.