Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix the scope behavior with indirect fixtures.
  • Loading branch information
aklajnert authored and Andrzej Klajnert committed Aug 29, 2019
commit 487659d8b13b6adfb1db23225c71a485cd0117bb
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Andras Tim
Andrea Cimatoribus
Andreas Zeidler
Andrey Paramonov
Andrzej Klajnert
Andrzej Ostrowski
Andy Freeland
Anthon van der Neut
Expand Down
1 change: 1 addition & 0 deletions changelog/570.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix the scope behavior with indirect fixtures.
7 changes: 5 additions & 2 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -859,7 +859,7 @@ def execute(self, request):
if argname != "request":
fixturedef.addfinalizer(functools.partial(self.finish, request=request))

my_cache_key = request.param_index
my_cache_key = self.cache_key(request)
cached_result = getattr(self, "cached_result", None)
if cached_result is not None:
result, cache_key, err = cached_result
Expand All @@ -877,6 +877,9 @@ def execute(self, request):
hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
return hook.pytest_fixture_setup(fixturedef=self, request=request)

def cache_key(self, request):
return request.param_index if not hasattr(request, "param") else request.param

def __repr__(self):
return "<FixtureDef argname={!r} scope={!r} baseid={!r}>".format(
self.argname, self.scope, self.baseid
Expand Down Expand Up @@ -913,7 +916,7 @@ def pytest_fixture_setup(fixturedef, request):
kwargs[argname] = result

fixturefunc = resolve_fixture_function(fixturedef, request)
my_cache_key = request.param_index
my_cache_key = fixturedef.cache_key(request)
try:
result = call_fixture_func(fixturefunc, request, kwargs)
except TEST_OUTCOME:
Expand Down
45 changes: 44 additions & 1 deletion testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,8 @@ def test_lookup_error(unknown):
"*ERROR at setup of test_lookup_error*",
" def test_lookup_error(unknown):*",
"E fixture 'unknown' not found",
"> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*", # sorted
"> available fixtures:*a_fixture,*b_fixture,*c_fixture,*d_fixture*monkeypatch,*",
# sorted
"> use 'py*test --fixtures *' for help on them.",
"*1 error*",
]
Expand Down Expand Up @@ -4009,3 +4010,45 @@ def test_fixture_named_request(testdir):
" *test_fixture_named_request.py:5",
]
)


def test_indirect_fixture(testdir):
testdir.makepyfile(
"""
from collections import Counter

import pytest


@pytest.fixture(scope="session")
def fixture_1(request, count=Counter()):
count[request.param] += 1
yield count[request.param]


@pytest.fixture(scope="session")
def fixture_2(request):
yield request.param


scenarios = [
("a", "a1"),
("a", "a2"),
("b", "b1"),
("b", "b2"),
("c", "c1"),
("c", "c2"),
]


@pytest.mark.parametrize(
"fixture_1,fixture_2", scenarios, indirect=["fixture_1", "fixture_2"]
)
def test_it(fixture_1, fixture_2):
assert fixture_1 == 1
assert fixture_2[1] in ("1", "2")

"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=6)