Fix self reference in function scoped fixtures

This commit is contained in:
Robert Holt 2019-08-19 15:57:39 -04:00
parent 307652202c
commit 62381125e7
No known key found for this signature in database
GPG Key ID: C0DF372F395D3586
4 changed files with 35 additions and 0 deletions

View File

@ -209,6 +209,7 @@ Raphael Castaneda
Raphael Pierzina Raphael Pierzina
Raquel Alegre Raquel Alegre
Ravi Chandra Ravi Chandra
Robert Holt
Roberto Polli Roberto Polli
Roland Puntaier Roland Puntaier
Romain Dorgueil Romain Dorgueil

View File

@ -0,0 +1 @@
Fix ``self`` reference in function scoped fixtures that are in a plugin class

View File

@ -897,6 +897,10 @@ def resolve_fixture_function(fixturedef, request):
# request.instance so that code working with "fixturedef" behaves # request.instance so that code working with "fixturedef" behaves
# as expected. # as expected.
if request.instance is not None: if request.instance is not None:
if hasattr(fixturefunc, "__self__") and not isinstance(
request.instance, fixturefunc.__self__.__class__
):
return fixturefunc
fixturefunc = getimfunc(fixturedef.func) fixturefunc = getimfunc(fixturedef.func)
if fixturefunc != fixturedef.func: if fixturefunc != fixturedef.func:
fixturefunc = fixturefunc.__get__(request.instance) fixturefunc = fixturefunc.__get__(request.instance)

View File

@ -3945,6 +3945,35 @@ class TestScopeOrdering:
reprec = testdir.inline_run() reprec = testdir.inline_run()
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
def test_class_fixture_self_instance(self, testdir):
testdir.makeconftest(
"""
import pytest
def pytest_configure(config):
config.pluginmanager.register(MyPlugin())
class MyPlugin():
def __init__(self):
self.arg = 1
@pytest.fixture(scope='function')
def myfix(self):
assert isinstance(self, MyPlugin)
return self.arg
"""
)
testdir.makepyfile(
"""
class TestClass(object):
def test_1(self, myfix):
assert myfix == 1
"""
)
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
def test_call_fixture_function_error(): def test_call_fixture_function_error():
"""Check if an error is raised if a fixture function is called directly (#4545)""" """Check if an error is raised if a fixture function is called directly (#4545)"""