Skip to content

Commit f360147

Browse files
authored
Avoiding looking upwards for parameter argnames when generating fixtu… (#5254)
Avoiding looking upwards for parameter argnames when generating fixtu…
2 parents 654d8da + 65bd1b8 commit f360147

File tree

4 files changed

+80
-4
lines changed

4 files changed

+80
-4
lines changed

changelog/5036.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issue where fixtures dependent on other parametrized fixtures would be erroneously parametrized.

src/_pytest/fixtures.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,18 +1129,40 @@ def __init__(self, session):
11291129
self._nodeid_and_autousenames = [("", self.config.getini("usefixtures"))]
11301130
session.config.pluginmanager.register(self, "funcmanage")
11311131

1132+
def _get_direct_parametrize_args(self, node):
1133+
"""This function returns all the direct parametrization
1134+
arguments of a node, so we don't mistake them for fixtures
1135+
1136+
Check https://github.com/pytest-dev/pytest/issues/5036
1137+
1138+
This things are done later as well when dealing with parametrization
1139+
so this could be improved
1140+
"""
1141+
from _pytest.mark import ParameterSet
1142+
1143+
parametrize_argnames = []
1144+
for marker in node.iter_markers(name="parametrize"):
1145+
if not marker.kwargs.get("indirect", False):
1146+
p_argnames, _ = ParameterSet._parse_parametrize_args(
1147+
*marker.args, **marker.kwargs
1148+
)
1149+
parametrize_argnames.extend(p_argnames)
1150+
1151+
return parametrize_argnames
1152+
11321153
def getfixtureinfo(self, node, func, cls, funcargs=True):
11331154
if funcargs and not getattr(node, "nofuncargs", False):
11341155
argnames = getfuncargnames(func, cls=cls)
11351156
else:
11361157
argnames = ()
1158+
11371159
usefixtures = itertools.chain.from_iterable(
11381160
mark.args for mark in node.iter_markers(name="usefixtures")
11391161
)
11401162
initialnames = tuple(usefixtures) + argnames
11411163
fm = node.session._fixturemanager
11421164
initialnames, names_closure, arg2fixturedefs = fm.getfixtureclosure(
1143-
initialnames, node
1165+
initialnames, node, ignore_args=self._get_direct_parametrize_args(node)
11441166
)
11451167
return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
11461168

@@ -1174,7 +1196,7 @@ def _getautousenames(self, nodeid):
11741196
autousenames.extend(basenames)
11751197
return autousenames
11761198

1177-
def getfixtureclosure(self, fixturenames, parentnode):
1199+
def getfixtureclosure(self, fixturenames, parentnode, ignore_args=()):
11781200
# collect the closure of all fixtures , starting with the given
11791201
# fixturenames as the initial set. As we have to visit all
11801202
# factory definitions anyway, we also return an arg2fixturedefs
@@ -1202,6 +1224,8 @@ def merge(otherlist):
12021224
while lastlen != len(fixturenames_closure):
12031225
lastlen = len(fixturenames_closure)
12041226
for argname in fixturenames_closure:
1227+
if argname in ignore_args:
1228+
continue
12051229
if argname in arg2fixturedefs:
12061230
continue
12071231
fixturedefs = self.getfixturedefs(argname, parentid)

src/_pytest/mark/structures.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,11 @@ def extract_from(cls, parameterset, force_tuple=False):
103103
else:
104104
return cls(parameterset, marks=[], id=None)
105105

106-
@classmethod
107-
def _for_parametrize(cls, argnames, argvalues, func, config, function_definition):
106+
@staticmethod
107+
def _parse_parametrize_args(argnames, argvalues, **_):
108+
"""It receives an ignored _ (kwargs) argument so this function can
109+
take also calls from parametrize ignoring scope, indirect, and other
110+
arguments..."""
108111
if not isinstance(argnames, (tuple, list)):
109112
argnames = [x.strip() for x in argnames.split(",") if x.strip()]
110113
force_tuple = len(argnames) == 1
@@ -113,6 +116,11 @@ def _for_parametrize(cls, argnames, argvalues, func, config, function_definition
113116
parameters = [
114117
ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
115118
]
119+
return argnames, parameters
120+
121+
@classmethod
122+
def _for_parametrize(cls, argnames, argvalues, func, config, function_definition):
123+
argnames, parameters = cls._parse_parametrize_args(argnames, argvalues)
116124
del argvalues
117125

118126
if parameters:

testing/python/fixtures.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3950,3 +3950,46 @@ def fix():
39503950

39513951
with pytest.raises(pytest.fail.Exception):
39523952
assert fix() == 1
3953+
3954+
3955+
def test_fixture_param_shadowing(testdir):
3956+
"""Parametrized arguments would be shadowed if a fixture with the same name also exists (#5036)"""
3957+
testdir.makepyfile(
3958+
"""
3959+
import pytest
3960+
3961+
@pytest.fixture(params=['a', 'b'])
3962+
def argroot(request):
3963+
return request.param
3964+
3965+
@pytest.fixture
3966+
def arg(argroot):
3967+
return argroot
3968+
3969+
# This should only be parametrized directly
3970+
@pytest.mark.parametrize("arg", [1])
3971+
def test_direct(arg):
3972+
assert arg == 1
3973+
3974+
# This should be parametrized based on the fixtures
3975+
def test_normal_fixture(arg):
3976+
assert isinstance(arg, str)
3977+
3978+
# Indirect should still work:
3979+
3980+
@pytest.fixture
3981+
def arg2(request):
3982+
return 2*request.param
3983+
3984+
@pytest.mark.parametrize("arg2", [1], indirect=True)
3985+
def test_indirect(arg2):
3986+
assert arg2 == 2
3987+
"""
3988+
)
3989+
# Only one test should have run
3990+
result = testdir.runpytest("-v")
3991+
result.assert_outcomes(passed=4)
3992+
result.stdout.fnmatch_lines(["*::test_direct[[]1[]]*"])
3993+
result.stdout.fnmatch_lines(["*::test_normal_fixture[[]a[]]*"])
3994+
result.stdout.fnmatch_lines(["*::test_normal_fixture[[]b[]]*"])
3995+
result.stdout.fnmatch_lines(["*::test_indirect[[]1[]]*"])

0 commit comments

Comments
 (0)