Skip to content

Commit

Permalink
Let reorder_items use our new parameter key
Browse files Browse the repository at this point in the history
Fixes test reordering for indirect parameterization (see pytest-dev#8913). Prior
to this commit, reorder_items considered the parameter index to tell if
a parameter is "the same" and therefore can be shared.

Looking at the index causes trouble if there are multiple
parametrizations for the same fixture, basically because one index means
different things in different parameter lists. This is fixed here by
using the recently introduced parameter key as grouping criterion.

Caution: The parameter key ends up inside the key of another dict, and
therefore must be hashable. CallSpec2.param_keys is crafted
sufficiently, it guarantees to contain comparable and hashable values.
  • Loading branch information
Tobias Deiminger committed Jan 26, 2022
1 parent 2e39c82 commit 84b0afe
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/_pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,21 +249,21 @@ def get_parametrized_fixture_keys(item: nodes.Item, scope: Scope) -> Iterator[_K
pass
else:
cs: CallSpec2 = callspec
# cs.indices.items() is random order of argnames. Need to
# cs.param_keys.items() is random order of argnames. Need to
# sort this so that different calls to
# get_parametrized_fixture_keys will be deterministic.
for argname, param_index in sorted(cs.indices.items()):
for argname, param_key in sorted(cs.param_keys.items()):
if cs._arg2scope[argname] != scope:
continue
if scope is Scope.Session:
key: _Key = (argname, param_index)
key: _Key = (argname, param_key)
elif scope is Scope.Package:
key = (argname, param_index, item.path.parent)
key = (argname, param_key, item.path.parent)
elif scope is Scope.Module:
key = (argname, param_index, item.path)
key = (argname, param_key, item.path)
elif scope is Scope.Class:
item_cls = item.cls # type: ignore[attr-defined]
key = (argname, param_index, item.path, item_cls)
key = (argname, param_key, item.path, item_cls)
else:
assert_never(scope)
yield key
Expand Down

0 comments on commit 84b0afe

Please sign in to comment.