Skip to content

Commit

Permalink
Allow tests declared as @staticmethod to use fixtures
Browse files Browse the repository at this point in the history
Fix #2699
  • Loading branch information
nicoddemus committed Aug 17, 2017
1 parent 5c0c197 commit a993add
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
5 changes: 4 additions & 1 deletion _pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def num_mock_patch_args(function):
return len(patchings)


def getfuncargnames(function, startindex=None):
def getfuncargnames(function, startindex=None, cls=None):
if startindex is None and cls is not None:
is_staticmethod = isinstance(cls.__dict__.get(function.__name__, None), staticmethod)
startindex = 0 if is_staticmethod else 1
# XXX merge with main.py's varnames
# assert not isclass(function)
realfunction = function
Expand Down
6 changes: 1 addition & 5 deletions _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -957,11 +957,7 @@ def __init__(self, session):

def getfixtureinfo(self, node, func, cls, funcargs=True):
if funcargs and not hasattr(node, "nofuncargs"):
if cls is not None:
startindex = 1
else:
startindex = None
argnames = getfuncargnames(func, startindex)
argnames = getfuncargnames(func, cls=cls)
else:
argnames = ()
usefixtures = getattr(func, "usefixtures", None)
Expand Down
1 change: 1 addition & 0 deletions changelog/2699.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow tests declared as ``@staticmethod`` to use fixtures.
14 changes: 12 additions & 2 deletions testing/python/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,21 @@ class test(object):
])

def test_static_method(self, testdir):
"""Support for collecting staticmethod tests (#2528, #2699)"""
testdir.getmodulecol("""
import pytest
class Test(object):
@staticmethod
def test_something():
pass
@pytest.fixture
def fix(self):
return 1
@staticmethod
def test_fix(fix):
assert fix == 1
""")
result = testdir.runpytest()
if sys.version_info < (2, 7):
Expand All @@ -162,8 +172,8 @@ def test_something():
])
else:
result.stdout.fnmatch_lines([
"*collected 1 item*",
"*1 passed in*",
"*collected 2 items*",
"*2 passed in*",
])

def test_setup_teardown_class_as_classmethod(self, testdir):
Expand Down
6 changes: 6 additions & 0 deletions testing/python/fixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ class A(object):
def f(self, arg1, arg2="hello"):
pass

@staticmethod
def static(arg1, arg2):
pass

assert fixtures.getfuncargnames(A().f) == ('arg1',)
if sys.version_info < (3, 0):
assert fixtures.getfuncargnames(A.f) == ('arg1',)

assert fixtures.getfuncargnames(A.static, cls=A) == ('arg1', 'arg2')


class TestFillFixtures(object):
def test_fillfuncargs_exposed(self):
Expand Down

0 comments on commit a993add

Please sign in to comment.