File tree Expand file tree Collapse file tree 4 files changed +35
-4
lines changed
Expand file tree Collapse file tree 4 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,7 @@ Michael Droettboom
118118Michael Seifert
119119Michal Wajszczuk
120120Mike Lundy
121+ Nathaniel Waisbrot
121122Ned Batchelder
122123Neven Mundar
123124Nicolas Delaby
Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff line change 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.)
Original file line number Diff line number Diff 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):
You can’t perform that action at this time.
0 commit comments