Merge pull request #5768 from robholt/fixture-class-instance
Fix self reference in function scoped fixtures
This commit is contained in:
commit
bd57307a39
1
AUTHORS
1
AUTHORS
|
@ -211,6 +211,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
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fixed ``self`` reference in function-scoped fixtures defined plugin classes: previously ``self``
|
||||||
|
would be a reference to a *test* class, not the *plugin* class.
|
|
@ -900,6 +900,12 @@ 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:
|
||||||
|
# handle the case where fixture is defined not in a test class, but some other class
|
||||||
|
# (for example a plugin class with a fixture), see #2270
|
||||||
|
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)
|
||||||
|
|
|
@ -3946,6 +3946,38 @@ 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):
|
||||||
|
"""Check that plugin classes which implement fixtures receive the plugin instance
|
||||||
|
as self (see #2270).
|
||||||
|
"""
|
||||||
|
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)"""
|
||||||
|
|
Loading…
Reference in New Issue