Merge pull request #5798 from aklajnert/570-indirect-fixtures

Fix the scope behavior with indirect fixtures
This commit is contained in:
Bruno Oliveira 2019-08-30 12:29:03 -03:00 committed by GitHub
commit 4e594552eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 3 deletions

View File

@ -23,6 +23,7 @@ Andras Tim
Andrea Cimatoribus Andrea Cimatoribus
Andreas Zeidler Andreas Zeidler
Andrey Paramonov Andrey Paramonov
Andrzej Klajnert
Andrzej Ostrowski Andrzej Ostrowski
Andy Freeland Andy Freeland
Anthon van der Neut Anthon van der Neut

2
changelog/570.bugfix.rst Normal file
View File

@ -0,0 +1,2 @@
Fixed long standing issue where fixture scope was not respected when indirect fixtures were used during
parametrization.

View File

@ -859,7 +859,7 @@ class FixtureDef:
if argname != "request": if argname != "request":
fixturedef.addfinalizer(functools.partial(self.finish, request=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) cached_result = getattr(self, "cached_result", None)
if cached_result is not None: if cached_result is not None:
result, cache_key, err = cached_result result, cache_key, err = cached_result
@ -877,6 +877,9 @@ class FixtureDef:
hook = self._fixturemanager.session.gethookproxy(request.node.fspath) hook = self._fixturemanager.session.gethookproxy(request.node.fspath)
return hook.pytest_fixture_setup(fixturedef=self, request=request) 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): def __repr__(self):
return "<FixtureDef argname={!r} scope={!r} baseid={!r}>".format( return "<FixtureDef argname={!r} scope={!r} baseid={!r}>".format(
self.argname, self.scope, self.baseid self.argname, self.scope, self.baseid
@ -913,7 +916,7 @@ def pytest_fixture_setup(fixturedef, request):
kwargs[argname] = result kwargs[argname] = result
fixturefunc = resolve_fixture_function(fixturedef, request) fixturefunc = resolve_fixture_function(fixturedef, request)
my_cache_key = request.param_index my_cache_key = fixturedef.cache_key(request)
try: try:
result = call_fixture_func(fixturefunc, request, kwargs) result = call_fixture_func(fixturefunc, request, kwargs)
except TEST_OUTCOME: except TEST_OUTCOME:

View File

@ -449,7 +449,8 @@ class TestFillFixtures:
"*ERROR at setup of test_lookup_error*", "*ERROR at setup of test_lookup_error*",
" def test_lookup_error(unknown):*", " def test_lookup_error(unknown):*",
"E fixture 'unknown' not found", "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.", "> use 'py*test --fixtures *' for help on them.",
"*1 error*", "*1 error*",
] ]
@ -4009,3 +4010,55 @@ def test_fixture_named_request(testdir):
" *test_fixture_named_request.py:5", " *test_fixture_named_request.py:5",
] ]
) )
def test_indirect_fixture_does_not_break_scope(testdir):
"""Ensure that fixture scope is respected when using indirect fixtures (#570)"""
testdir.makepyfile(
"""
import pytest
instantiated = []
@pytest.fixture(scope="session")
def fixture_1(request):
instantiated.append(("fixture_1", request.param))
@pytest.fixture(scope="session")
def fixture_2(request):
instantiated.append(("fixture_2", 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_create_fixtures(fixture_1, fixture_2):
pass
def test_check_fixture_instantiations():
assert instantiated == [
('fixture_1', 'A'),
('fixture_2', 'a1'),
('fixture_2', 'a2'),
('fixture_1', 'B'),
('fixture_2', 'b1'),
('fixture_2', 'b2'),
('fixture_1', 'C'),
('fixture_2', 'c1'),
('fixture_2', 'c2'),
]
"""
)
result = testdir.runpytest()
result.assert_outcomes(passed=7)