-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Allow ovewriting a parametrized fixture while reusing the parent fixture's value #7736
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
nicoddemus
merged 2 commits into
pytest-dev:master
from
nicoddemus:extend-fixture-parametrize-1953
Sep 12, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Fix error when overwriting a parametrized fixture, while also reusing the super fixture value. | ||
|
||
.. code-block:: python | ||
|
||
# conftest.py | ||
import pytest | ||
|
||
|
||
@pytest.fixture(params=[1, 2]) | ||
def foo(request): | ||
return request.param | ||
|
||
|
||
# test_foo.py | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def foo(foo): | ||
return foo * 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,7 @@ | |
from _pytest.config import Config | ||
from _pytest.config.argparsing import Parser | ||
from _pytest.deprecated import FILLFUNCARGS | ||
from _pytest.mark import Mark | ||
from _pytest.mark import ParameterSet | ||
from _pytest.outcomes import fail | ||
from _pytest.outcomes import TEST_OUTCOME | ||
|
@@ -1529,34 +1530,49 @@ def sort_by_scope(arg_name: str) -> int: | |
return initialnames, fixturenames_closure, arg2fixturedefs | ||
|
||
def pytest_generate_tests(self, metafunc: "Metafunc") -> None: | ||
"""Generate new tests based on parametrized fixtures used by the given metafunc""" | ||
|
||
def get_parametrize_mark_argnames(mark: Mark) -> Sequence[str]: | ||
args, _ = ParameterSet._parse_parametrize_args(*mark.args, **mark.kwargs) | ||
return args | ||
|
||
for argname in metafunc.fixturenames: | ||
faclist = metafunc._arg2fixturedefs.get(argname) | ||
if faclist: | ||
fixturedef = faclist[-1] | ||
# Get the FixtureDefs for the argname. | ||
fixture_defs = metafunc._arg2fixturedefs.get(argname) | ||
if not fixture_defs: | ||
# Will raise FixtureLookupError at setup time if not parametrized somewhere | ||
# else (e.g @pytest.mark.parametrize) | ||
continue | ||
|
||
# If the test itself parametrizes using this argname, give it | ||
# precedence. | ||
if any( | ||
argname in get_parametrize_mark_argnames(mark) | ||
for mark in metafunc.definition.iter_markers("parametrize") | ||
): | ||
continue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noticed this should be |
||
|
||
# In the common case we only look at the fixture def with the | ||
# closest scope (last in the list). But if the fixture overrides | ||
# another fixture, while requesting the super fixture, keep going | ||
# in case the super fixture is parametrized (#1953). | ||
for fixturedef in reversed(fixture_defs): | ||
# Fixture is parametrized, apply it and stop. | ||
if fixturedef.params is not None: | ||
markers = list(metafunc.definition.iter_markers("parametrize")) | ||
for parametrize_mark in markers: | ||
if "argnames" in parametrize_mark.kwargs: | ||
argnames = parametrize_mark.kwargs["argnames"] | ||
else: | ||
argnames = parametrize_mark.args[0] | ||
|
||
if not isinstance(argnames, (tuple, list)): | ||
argnames = [ | ||
x.strip() for x in argnames.split(",") if x.strip() | ||
] | ||
if argname in argnames: | ||
break | ||
else: | ||
metafunc.parametrize( | ||
argname, | ||
fixturedef.params, | ||
indirect=True, | ||
scope=fixturedef.scope, | ||
ids=fixturedef.ids, | ||
) | ||
else: | ||
continue # Will raise FixtureLookupError at setup time. | ||
metafunc.parametrize( | ||
argname, | ||
fixturedef.params, | ||
indirect=True, | ||
scope=fixturedef.scope, | ||
ids=fixturedef.ids, | ||
) | ||
break | ||
|
||
# Not requesting the overridden super fixture, stop. | ||
if argname not in fixturedef.argnames: | ||
break | ||
|
||
# Try next super fixture, if any. | ||
|
||
def pytest_collection_modifyitems(self, items: "List[nodes.Item]") -> None: | ||
# Separate parametrized setups. | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.