Skip to content

getfixturevalue does not correctly declare dependency with the calling fixture #4860

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
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
2 changes: 2 additions & 0 deletions changelog/1895.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix bug where fixtures requested dynamically via ``request.getfixturevalue()`` might be teardown
before the requesting fixture.
22 changes: 17 additions & 5 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,13 @@ def _compute_fixture_value(self, fixturedef):
# call the fixture function
fixturedef.execute(request=subrequest)
finally:
# if fixture function failed it might have registered finalizers
self.session._setupstate.addfinalizer(
functools.partial(fixturedef.finish, request=subrequest),
subrequest.node,
)
self._schedule_finalizers(fixturedef, subrequest)

def _schedule_finalizers(self, fixturedef, subrequest):
# if fixture function failed it might have registered finalizers
self.session._setupstate.addfinalizer(
functools.partial(fixturedef.finish, request=subrequest), subrequest.node
)

def _check_scope(self, argname, invoking_scope, requested_scope):
if argname == "request":
Expand Down Expand Up @@ -659,6 +661,16 @@ def __repr__(self):
def addfinalizer(self, finalizer):
self._fixturedef.addfinalizer(finalizer)

def _schedule_finalizers(self, fixturedef, subrequest):
# if the executing fixturedef was not explicitly requested in the argument list (via
# getfixturevalue inside the fixture call) then ensure this fixture def will be finished
# first
if fixturedef.argname not in self.funcargnames:
fixturedef.addfinalizer(
functools.partial(self._fixturedef.finish, request=self)
)
super(SubRequest, self)._schedule_finalizers(fixturedef, subrequest)


scopes = "session package module class function".split()
scopenum_function = scopes.index("function")
Expand Down
38 changes: 38 additions & 0 deletions testing/python/fixture.py → testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,44 @@ def test_func(something):
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)

def test_getfixturevalue_teardown(self, testdir):
"""
Issue #1895

`test_inner` requests `inner` fixture, which in turn requests `resource`
using `getfixturevalue`. `test_func` then requests `resource`.

`resource` is teardown before `inner` because the fixture mechanism won't consider
`inner` dependent on `resource` when it is used via `getfixturevalue`: `test_func`
will then cause the `resource`'s finalizer to be called first because of this.
"""
testdir.makepyfile(
"""
import pytest

@pytest.fixture(scope='session')
def resource():
r = ['value']
yield r
r.pop()

@pytest.fixture(scope='session')
def inner(request):
resource = request.getfixturevalue('resource')
assert resource == ['value']
yield
assert resource == ['value']

def test_inner(inner):
pass

def test_func(resource):
pass
"""
)
result = testdir.runpytest()
result.stdout.fnmatch_lines("* 2 passed in *")

@pytest.mark.parametrize("getfixmethod", ("getfixturevalue", "getfuncargvalue"))
def test_getfixturevalue(self, testdir, getfixmethod):
item = testdir.getitem(
Expand Down