Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ Mike Lundy
Mike Ma
Milan Lesnek
minbang930
Minh Dau
Miro Hrončok
Mulat Mekonen
mrbean-bremen
Expand Down
1 change: 1 addition & 0 deletions changelog/6750.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix fixture argument discovery for custom collectors that pass bound methods to :class:`pytest.Function`.
1 change: 1 addition & 0 deletions src/_pytest/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def getfuncargnames(
# Not using `getattr` because we don't want to resolve the staticmethod.
# Not using `cls.__dict__` because we want to check the entire MRO.
cls
and not inspect.ismethod(function)
and not isinstance(
inspect.getattr_static(cls, name, default=None), staticmethod
)
Expand Down
46 changes: 46 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,52 @@ def k(self, /, arg1, *, arg2, arg3="hello"):
assert getfuncargnames(A().k) == ("arg1", "arg2")


def test_bound_method_fixtures_custom_collector(pytester: Pytester) -> None:
"""Custom collectors can supply already-bound methods (#6750)."""
pytester.makeconftest(
"""
import pytest

def pytest_pycollect_makeitem(collector, name, obj):
if isinstance(collector, pytest.Class) and name.startswith("test_"):
return pytest.Function.from_parent(
collector,
name=name,
callobj=getattr(collector.newinstance(), name),
)
"""
)
pytester.makepyfile(
"""
import pytest

@pytest.fixture
def value():
return 42

class TestBound:
def test_instance(self, value):
assert value == 42

def test_positional_only_receiver(self, /, value):
assert value == 42

def test_keyword_only_fixture(self, *, value):
assert value == 42

@classmethod
def test_classmethod(cls, value):
assert value == 42

@staticmethod
def test_staticmethod(value):
assert value == 42
"""
)
result = pytester.runpytest()
result.assert_outcomes(passed=5)


def test_getfuncargnames_staticmethod():
"""Test getfuncargnames for staticmethods"""

Expand Down
Loading