Skip to content

Commit 6238112

Browse files
committed
Fix self reference in function scoped fixtures
1 parent 3076522 commit 6238112

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ Raphael Castaneda
209209
Raphael Pierzina
210210
Raquel Alegre
211211
Ravi Chandra
212+
Robert Holt
212213
Roberto Polli
213214
Roland Puntaier
214215
Romain Dorgueil

changelog/2270.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``self`` reference in function scoped fixtures that are in a plugin class

src/_pytest/fixtures.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,10 @@ def resolve_fixture_function(fixturedef, request):
897897
# request.instance so that code working with "fixturedef" behaves
898898
# as expected.
899899
if request.instance is not None:
900+
if hasattr(fixturefunc, "__self__") and not isinstance(
901+
request.instance, fixturefunc.__self__.__class__
902+
):
903+
return fixturefunc
900904
fixturefunc = getimfunc(fixturedef.func)
901905
if fixturefunc != fixturedef.func:
902906
fixturefunc = fixturefunc.__get__(request.instance)

testing/python/fixtures.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3945,6 +3945,35 @@ def test_2(fix):
39453945
reprec = testdir.inline_run()
39463946
reprec.assertoutcome(passed=2)
39473947

3948+
def test_class_fixture_self_instance(self, testdir):
3949+
testdir.makeconftest(
3950+
"""
3951+
import pytest
3952+
3953+
def pytest_configure(config):
3954+
config.pluginmanager.register(MyPlugin())
3955+
3956+
class MyPlugin():
3957+
def __init__(self):
3958+
self.arg = 1
3959+
3960+
@pytest.fixture(scope='function')
3961+
def myfix(self):
3962+
assert isinstance(self, MyPlugin)
3963+
return self.arg
3964+
"""
3965+
)
3966+
3967+
testdir.makepyfile(
3968+
"""
3969+
class TestClass(object):
3970+
def test_1(self, myfix):
3971+
assert myfix == 1
3972+
"""
3973+
)
3974+
reprec = testdir.inline_run()
3975+
reprec.assertoutcome(passed=1)
3976+
39483977

39493978
def test_call_fixture_function_error():
39503979
"""Check if an error is raised if a fixture function is called directly (#4545)"""

0 commit comments

Comments
 (0)