Skip to content

Commit 79ff930

Browse files
Do the change
1 parent f74e947 commit 79ff930

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/_pytest/python.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,7 @@ def _genfunctions(self, name: str, funcobj) -> Iterator[Function]:
464464
if not metafunc._calls:
465465
yield Function.from_parent(self, name=name, fixtureinfo=fixtureinfo)
466466
else:
467+
metafunc._recompute_direct_params_indices()
467468
# Direct parametrizations taking place in module/class-specific
468469
# `metafunc.parametrize` calls may have shadowed some fixtures, so make sure
469470
# we update what the function really needs a.k.a its fixture closure. Note that
@@ -1131,6 +1132,8 @@ def __init__(
11311132
# Result of parametrize().
11321133
self._calls: list[CallSpec2] = []
11331134

1135+
self._params_directness: dict[str, Literal["indirect", "direct"]] = {}
1136+
11341137
def parametrize(
11351138
self,
11361139
argnames: str | Sequence[str],
@@ -1273,6 +1276,7 @@ def parametrize(
12731276
name2pseudofixturedef_key, default
12741277
)
12751278
arg_directness = self._resolve_args_directness(argnames, indirect)
1279+
self._params_directness.update(arg_directness)
12761280
for argname in argnames:
12771281
if arg_directness[argname] == "indirect":
12781282
continue
@@ -1445,6 +1449,12 @@ def _validate_if_using_arg_names(
14451449
pytrace=False,
14461450
)
14471451

1452+
def _recompute_direct_params_indices(self):
1453+
for argname, param_type in self._params_directness.items():
1454+
if param_type == "direct":
1455+
for i, callspec in enumerate(self._calls):
1456+
callspec.indices[argname] = i
1457+
14481458

14491459
def _find_parametrized_scope(
14501460
argnames: Sequence[str],

testing/example_scripts/issue_519.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ def checked_order():
2323
assert order == [
2424
("issue_519.py", "fix1", "arg1v1"),
2525
("test_one[arg1v1-arg2v1]", "fix2", "arg2v1"),
26-
("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"),
2726
("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"),
27+
("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"),
2828
("test_two[arg1v1-arg2v2]", "fix2", "arg2v2"),
2929
("issue_519.py", "fix1", "arg1v2"),
3030
("test_one[arg1v2-arg2v1]", "fix2", "arg2v1"),
31-
("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"),
3231
("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"),
32+
("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"),
3333
("test_two[arg1v2-arg2v2]", "fix2", "arg2v2"),
3434
]
3535

testing/python/fixtures.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4878,3 +4878,37 @@ def test_result():
48784878
)
48794879
result = pytester.runpytest()
48804880
assert result.ret == 0
4881+
4882+
4883+
def test_reordering_in_multiple_parametrization(pytester: Pytester) -> None:
4884+
pytester.makepyfile(
4885+
"""
4886+
import pytest
4887+
@pytest.mark.parametrize("arg2", [3, 4])
4888+
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
4889+
def test1(arg1, arg2):
4890+
pass
4891+
4892+
def test2():
4893+
pass
4894+
4895+
@pytest.mark.parametrize("arg1", [0, 1, 2], scope='module')
4896+
def test3(arg1):
4897+
pass
4898+
"""
4899+
)
4900+
result = pytester.runpytest("--collect-only")
4901+
result.stdout.re_match_lines(
4902+
[
4903+
r" <Function test1\[0-3\]>",
4904+
r" <Function test3\[0\]>",
4905+
r" <Function test1\[0-4\]>",
4906+
r" <Function test3\[1\]>",
4907+
r" <Function test1\[1-3\]>",
4908+
r" <Function test3\[2\]>",
4909+
r" <Function test1\[1-4\]>",
4910+
r" <Function test1\[2-3\]>",
4911+
r" <Function test1\[2-4\]>",
4912+
r" <Function test2>",
4913+
]
4914+
)

0 commit comments

Comments
 (0)